Log4j Tutorial #3 - How to setup Log4j Configuration with Log4j.xml

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back in this log4j tutorial we are going to understand about the configuration file or the xml configuration file in log4j and how you can customize the output of the logs into a file or into a console right so till now we have seen about the logging and this was the default logger that we saw so we haven't had any of the configuration as such let me remove this says out statement and now you can see these are the logs so if i run this particular program it will log these messages right now here only error is being logged what is the reason why only error is being logged and why not info and debug messages right now this is because at the moment if you do not specify any of the configuration by default log4j goes back to the default configuration that is there okay so if you go to the documentation here right and here in the configuration section so log4j we are in the version two in the configuration if you will see this is how the configuration basically is looked for so at the moment we are in the automatic configuration what it means is basically if you see this last line here if there is no configuration file that could be located then it will this will cause the logging output to go to the console right so it goes to the console and it will use the default configuration which is basically the configuration that logs the error messages and anything above okay so let's see what are the log levels okay so if i just search for levels right so we'll see custom levels right here these are the standard levels that are built in levels so you'll see that default configuration will log anything which is above which is error and above so if i'll use fatal there so error and fatal will be logged but not anything below right so you'll see these are the levels so trace debug info warning error and fatal right so these are basically what the default levels are and you can log the messages based used on the based on these default levels so i can if i just add one more line here and i say that this is a factor right this is fatal error and i'll change it to fractal here and if i run this now you will see that now it will log error and fatal right on the console okay so if you want to customize this default configuration and want to log the messages based on your requirements say for example here in this package you want to see this is one of the package right so in this package you want to log anything which is info and above so you can customize that using the xml file or json file there are many configurations available um in in log4j if there is another package wherein you just want to log the messages which are fatal then you can have those configurations done let's quickly see how you can do the configuration okay so if we go back to the documentation this is the key source where you should be referring to and now please understand this is very vast website lots and lots of functionality so do not get into you know too much detail of things right so stick to if you are new beginner stick to the things that i am telling you in this particular video and then if you want you can build your capabilities further on by reading this documentation okay so if we go to the introduction section again right or just the configuration so here you will see that you can configure based on the properties yaml json right and xml okay so you can see you can configure log4j in one of the four ways so xml json yaml you can configure by programmatically creating the configuration factory you won't need it this is what you will be needing and xml is very commonly used and you will be using either xml or properties or you can use any of these most of them are similar but once you understand one you will be able to replicate others as well okay so let's quickly understand this xml configuration and have the custom messages which logs the info and above okay or trace and above okay so if you scroll down here you will see this is one of the xml right so this is the xml that you can directly use copy paste and use it you just have to change the level right but let's quickly understand this particular xml so i'll copy it and let's see how you can use it and configure it okay so here let's create a new folder and i will name it as resources and here i'll create a new xml file so make sure you name this xml file as log4j2.xml right and paste all the code there okay now what you need to understand in this particular file is the key sections are the appenders right so the appender and the loggers okay so at the moment you will see that the logger is the root logger what exactly is the root logger now when we say root logger if you understand so taking an example of a tree right so tree has the roots and roots provide the nutrition to all the branches and leaves of the tree right so same analogy if you apply here root logger is the logger which you can correlate with the roots and if you configure the root logger say for example at the root logger level i configured it as an info then that logger will be applicable to all the classes and all the classes basically all the tests that are there within the package within the project okay wherever you will use this particular logger so that's the concept of the root logger so then you can also have the custom logger so basically the package level logger which you can define below this logger itself which will cover in a bit okay so this is what the root logger is and in the loggers tag so if you see this is the common xml you know line that is used to ensure basically that this is the xml version and the encoding level then the tag configuration is where all these appenders and loggers details will go and this file can become as complex as you want but this is the simplest format form of it okay so in the configuration we have this status there then you have the appender so what exactly appenders are so appenders will specify where to append these logs right so where to basically add these logs so you'll see that appenders at the moment is console so it will show the logs on the console so within appenders you can specify whether you want to log on the console or into the file so appenders will have the tag console because you want to log on the console then the target and the pattern layout so pattern layout is in what pattern so you what way you want to format so this is more of a formatting of the log messages you can see that this is more of a standard that is being provided but you can customize it so if you want to customize this just go to the uh you know documentation you will be able to find how you can specify the different custom patterns to customize it for learning log 4j and for selenium automation i think this is good enough unless until there is specific need by by your client or by the customer so this is good enough this pattern is all good we'll just keep it and if you understand this this is just you know like logging by the hour minute second and milliseconds there then you have this you know uh if you see this percentage t it is mostly saying where exactly or where the logger is coming from so basically it will log uh that particular method and then the level uh along with the basic messages and the new line right so it will then move to the new line so some some of the details that you need to understand what exactly this pattern is all about okay then you have once you have defined the appenders then loggers will have the logger so at the moment we'll just keep it as a root logger level okay so at the moment the level of the logger is error so if now i want to log the messages that are info or trace and above so everything which is trace level trace if i specify trace anything trace and above will be logged okay so i have changed it to trace this is the only thing that you have to do create a folder into your project resources or any name there whichever is convenient for you specify or create a file log4j2.xml paste that code here change the level to whatever level you want to specify right so here we have seen there are the levels default level so let's see where it is um levels custom log levels so if i specify trace then it will log anything that is trace and anything above right you can specify whatever default level standard level you want to specify okay now the next thing is in the project level you have to basically now this particular package doesn't know that there is a lot 4j configuration xml so you have to add this xml location into the class path right so go to the properties or build path right in java build path you have to in the source you have to make sure that the source folders in the build path this resources folder is there so that whenever you are trying to build this resources folder xml file can be referenced to so click on add folder and add this resources folder okay and click on apply and close right so now you will see that resources folder has been added okay now if i go to this particular logging demo first dot java okay i have info i have error debug and factor so now because i have specified the log 4j route level to trace it should log all these four messages previously it wasn't logging right so let's quickly run it and see if everything is working now you can see all the four messages have been logged into the console right so this is the basic very basic of configuring the logs and you'll see the messages here right so you'll see the formatting so it ha it is the hours minutes seconds milliseconds and then it is specifying from which method basically it is coming the log level the the the package name and the class name followed by the message that you have specified right so that's what this pattern defines right so if you want if i don't want this milliseconds there i can simply remove it and save it and all my messages will now won't be having any of the milliseconds there okay you will see now it only has this hour minute and second right so this is what this lock 4j configuration xml is all about and this is the simplest form of it but start with the simple form if you're absolutely new and understand this xml right so key thing to understand here is simply the configuration tags within the configuration tags the appenders and the loggers okay so loggers will tell you basically where you want to log right so here i want to you know sorry appenders will tell you where you want to lock so in the appenders i have mentioned that i want to append it in the console and then in the loggers you specify that in the appender reference i am trying to mention that with the root level logger go and log this type of pattern the log pattern into the console right so this is a brief about how you can use the log4j xml to configure the messages you can change it to say for example here i have error debug and fatal okay so i can change it to just say let's say changing it to error okay i just want to show you that or just say factor so once i change it to fatal and run this it should only display the fatal message right so now this logging is being controlled by this log4j2.xml okay so i hope this tutorial was easy and helpful to understand thank you very much for watching
Info
Channel: Software Testing Mentor
Views: 35,339
Rating: undefined out of 5
Keywords: log4j xml configuration, log4j xml tutorial, log4j xml file explained, log4j.xml explained, log4j xml file in selenium, log4j2 xml configuration, log4j2.xml tutorial in java, log4j beginner tutorial, apache log4j 2, log4j in java tutorial, log4j.xml tutorial in java, log4j2 xml configuration explained, log4j tutorial, log4j tutorial in java, log4j2 tutorial, log4j2 tutorial in java, how to setup log4j configuration with log4.xml
Id: ViJxlwdL9m4
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Tue Nov 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.