Selenium Framework for Beginners 14 | What is Log4j | How to add Log4j in java project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session I'm raghav and today we are going to learn log4j from scratch so if we do not know anything about log4j do not worry we are going to go very basic step by step and learn what is log for G and how to implement log4j in a java project we will also look at configuration files like log4j dot properties and log4j dot XML and we will look at some useful tips so let's get started and log4j is a Java based logging framework so this is written in Java and used to log information and it is a very simple flexible reliable and fast logging framework and we configure log4j through configuration files which can be XML file properties file and it can also be a UML file and JSON file and so on we will look into it in more details so let us very quickly look at a practical example and the very first step is you have to add log4j libraries in your java project so let me go to my Eclipse and here I have this maven project already created so to get log4j libraries you can go to your browser and go to google log4j and enter and the very first side that you will get is login dot apache dot o-r-g this is the official website for log4j if you go here you will find this download here this download section go here and here you can get all this log4j libraries so you can get the zip file unzip it in and then add the jars in your project so in case you are getting the charge you can do a right click on your project and then go to build path configure build path and go to libraries and then add external jars and then browse your jars and add it here but in our case because we have a maven project so we will add the dependencies maven dependencies in form dot xml so if you go here and if you are search for this maven there is a section for maven if you click here you will find this maven dependencies that you can add in your pom dot XML so I will just copy the dependency part from here I will copy it from here and go to my project go to palm dot XML and at the bottom I will just add these dependencies and two identical press ctrl-a if you are on Mac you can press command a and then ctrl I or command I and it will identity and you can see these other two dependencies and as soon as you save your project or you can auto build also it will add the dependency so you can just go to your maven dependencies and search you should get log4j dependencies here and yes you can see here we have this log4j APN log4j code here okay and you can follow this in if you have any proper project so although I am doing this in one of the projects that I have been creating or selenium project but even if you are having some different project you can follow this we are going to go very basic step-by-step here so this is step one we have added log4j libraries and then we will create a reference for log4j logger class in the class wherever we want to add the log statements so let me do this thing I will go to my source main Java package here and create a new package and I will call this as demo and set finish and here I will create a new class I will say this is I will say this is log4j demo and I will just hit this create a main method so that it will automatically generate a main method for us and you can see this is our class okay now here what I will do is I will create our instance for the logger class and I will say logger and I can use any object name I will just say logger equals logger manager so let me just see let me just first import this logger and if I hover over this it is asking it has given me several options so make sure that you take it from this Apache logging log4j should be log manager and yes we have log manager and please make sure that you take it from this Apache log in log for G and not from java.util or any other log manager and here you will say dot get logger and in the brackets I will mention my class so I will say this is log4j demo dot class okay so this is we are saying that we have to create a logger for this particular class and now this logger variable is created and using this I can log the log command so for example let me just create a simple safe out statement here I will say sys or press control spacebar on my keyboard and autocomplete this and I am using I am just saying hello world a simple program and I will press back slash hand to go to the next line and in the starting also I will press back slash n to go to the next line so I'm just printing hello world here and here now we can use this logger and if you say logger dot you will find a lot of options now it is not taking this locker I think we will have to make it as static so I can make it as static because we are referring it in a static method and I can also make it as private which is optional so now if I say logger here you can see we are getting a lot of options and you can have different log levels so let me say we can have info and you can log any message here and I will say this is information message and then I can also say lager dot error I will use this constructor which is for heaven only a message and I will say this is an error message and so on we can have multiple other levels so let me also show you some other levels I will say copy here and paste and I will use one and I will say this is warning message also we have levels like fettle and I will say this is a federal message now in this case I am just showing you a demo in real life what you will do is you will add this logger statements wherever you want it within your project or within your test or within your script okay and at the end I will just say as fire so control spacebar so that I get this auto-completed and I will say completed okay okay so now I will just save this and now if you run this I will do a right click and I will see it on this java application now if you go to your console let me just show you if you go here you can see it is printing HelloWorld it is printing error message and fettle message and not other messages like info input is not printing it is not printing warning only error and fettle and also it is giving us an error so the saying no log4j to configuration file found so now we have to add a configuration file and if you do not add a configuration file by default it will log on the console from the error level so error and above levels will be logged on the console so if you just want to know the levels I will tell you in a moment but now we have to add a log4j configuration file and this is our next step so we have to create either a log4j dot XML file or log4j dot properties now these are configuration files in log4j where we have some key value pairs to provide the settings and what is the difference between these files we will discuss in a later session but for now let me just show you log4j properties which is most commonly used and one of the major differences here is or in log4j dot XML if you created a an XML file you get some more options there are some options which you cannot configure in our properties file so people who do deep level of coding that want deep level of logging they use XML files and I will tell you in the next session about XML files but today let us discuss on log4j properties which is very simple so here you have to create a file I will go to this resources folder SRC main resources I will do a right-click go to new and I am finding our option for file here if you do not find it here you can go to others and then you will find our option for file go to file and here say log4j to dot properties now remember you have to say log4j to not log4j dot properties because log4j 1 is now no longer used and it is now known no longer updated or supported so we have to use log4j to dot properties and say finish so we have got this file here now what to write here so let us again go to our browser and I will say log4j dot properties file example so yes this is one good example from this side let me also see some other examples so see you can also get it from here which is the Apache official site so if I search for log4j properties file example Java there was a very good article I just saw let me just see yes I think this is the one this explained it very beautifully and in a very simple way yes this is from Spring Framework guru I will also provide the link in the nodes description section of this video so if you go down here we have done all these things we have done the maven dependencies and everything let us go little more down and here yes so this is the section that explains in a very good a way and in a very simple way to create our log4j properties file so I will just copy it from here and go to my project and paste it here and I think it copied two times so yeah this is it so you see this is how we create the first thing the first section is we say name properties config we say properties dot file name locks and then append us so in case you want to log the information both in the console which is this console output and then also to a file you can say console comma file so this is the first section and then the next section is here so here I will copy this again copy this from here go to my project and paste it yes so now you see this second section this is this is the settings for the console log so we are logging on the console and on the file for the console these are the settings so we are saying the console type console STD out and then the pattern here and then for the file these are the settings so be a sync file we lock file here you give the file name so here you are seeing he is taking it from this variable file name which is coming from here so it will create a folder called locks and then create a file called properties logs dot log in case you do not want that you can also give some direct name here for example I say my logs dot log so it will be created here directly in the project level but let us just use this file name and name of course I will change here I will say my logs dot log and then the pattern again and this was the third section and then again let me go here and let me again go a little more down and this is the last section so here okay so here you can see root logger level then Duke logger append reference and all these things so this is the level that we are setting here and there was one thing missing here if I go down yes there was this file logger section missing I will copy this again from here go to my project and here add this okay so here we are saying loggers file and here in the logger dot file dot name you have you can give your package name so in my case this is demo I will say this is demo here ok and then all this is fine so now we have this log4j dot properties file and now I will go back again to our log4j demo class I will do a right click and let's just seaweed as of now we don't have any log folder here and no log file I will say run as java application again and here if you see the console you can see everything is logged here and we all should not have any errors and now if i refresh the project you can press f5 or I will go right click and refresh it do you see we now have this logs folder created and if you go inside this you can see my logs and you can see the same information is logged here so you can see this is information message error warning feta and also if I go back here so suppose if I'm saying a trace level so there's also a trace level here so here let me just also create a trace level logger and here I will say this is trace message and I will just explain you about these levels and if I run this again let me again to remove this file and save it and I will run it again run as java application so here do you see we are not getting the message for the trace level here as well as if you go to your file so if you go to your my log store block even here you are not seeing the trace message so that is because in our log4j dot properties if you see here the level so you can see the level here so here it is debug so it will only log debug level and above so if I say trace here and I will say here also root logger level as trace and let me see if I have the level here for console I don't think there is a console so it will take from the root okay and I will save it and let me delete this file and go back to my class and run as java application and I will save it and now let us see so you can see we are getting the trace message as well here and the same thing let me just verify from my file we are getting the taste message here as well okay so step number four was to run invalidate that we have already done and here are the log levels so if you want to have a look you can pause this video and read about the log levels we have different kind of log levels here and also I will provide all these nodes and links in the description or note section of this video and now let us look at some very quick useful tips so you can configure to append or override in your log file so for example as of now it is overwriting and if you just want that it should not override but just append then you can say here a panda dot file dot append equals true and save it and let me run it again so if I run this now run as java application and i will refresh my project I will press f5 and go to my locks and now you can see it is now appending so all this message it was the first information it was locked from the first one and from here it is the locks from the second and so it now it will go on appending in the same log files the other thing is you can also set login to air off when not required now you may want to set logging off in case you are doing very heavy logging and in case it is making your execution a little slower or due to any other reason you might want to make it as off so again you can go to your log 4G properties and here so wherever you have this level make it as off so I will make it off here I can make it off as here as well okay and just to confirm I am getting this off I will delete this file and see if this gets created again I will delete this log file save this and run this again run as java application and okay now do you see here we don't have any locks getting logged here and also if i refresh my project let me just check and if you see my locks is created but there is no data in the log file so you can also make it as off so just a quick information on log4j configuration file this will be useful in your interview questions or just for knowledge log4j is configured through a configuration file that we just saw and it can be a XML JSON ml or properties file we just saw a properties file and then it is a set of key value pairs that we have seen and in this file there are three main components which are loggers appenders and layouts and that we have seen here if you see so we have loggers appenders and then we have layouts so these are the loggers are to log information pandas are to tell whether to go on a console or to go on a file and then layouts are some formats also configuring logging via a file has the advantage that logging can be turned on or off without modifying the application that uses log for G which we have already seen and there are some efficacy again this will be very useful in interview questions and also useful for basic knowledge log4j is written in Java so it is cross-platform it is a part of Apache logging services which is a part of Apache foundation so it is open-source and it was initially released in January 2001 so this is the official repository link on github I will provide this in the description or notes section as well and this is the website that we have already seen and as I have already discussed log4j one is now no longer used and we have to upgrade to log4j 2 in case you are still using log4j one then please upgrade to log4j 2 so let us do a very quick recap today we learnt what is log4j and how to implement log4j in a java project in a very basic step-by-step manner i hope this session was very useful for you if you liked it please hit the like button and also share it with all your groups and share with others and don't forget to subscribe to this channel and hit the bell icon so that you can keep receiving notifications on new videos I will meet you in the next episode thank you for watching
Info
Channel: Automation Step by Step
Views: 82,285
Rating: 4.9075499 out of 5
Keywords: what is log4j, how to add log4j in java project, how to add log4j in selenium java project, selenium what is log4j, how to do logging in selenium, log4j how to create log4j.properties file, how to create log4j.xml file, log4j configuration file, what is the differenc between log4j.properties and log4j.xml file, how to configure log4j in java, log4j beginner tutorial, log4j from sratch, log4j step by step, log4j properties file beginner tutorial
Id: rbuR9Q_55h4
Channel Id: undefined
Length: 21min 18sec (1278 seconds)
Published: Tue Aug 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.