Log4j Tutorial #5 - How to write Log4j Logs to a File

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back in this log forger tutorial we will understand how you can print these log messages or send these log messages to the log files now usually when you are doing the configuration or setting up the configuration of the logs or logging within your automation framework or development environment the point of logging is that you can send it to the file and you can refer them back usually if you are configuring it on the console it will be displayed on the console that's good but once you re-execute there is no saved sort of history that you can go back and refer where the issue happened right so when you are logging the logs into the log files it will help you to understand which of your test case or what exactly failed into your test case when you are writing selenium with java automation code so this is the xml file the configuration that we have done let's quickly understand what extra you need to do in here in this file to send these log messages that we have added here into the file okay so if we simply go to the documentation and search for rolling file right and let's see there is a xml tab okay so it's already there right so this is basically what we need to add so i'll simply copy the scrolling file all of the details here and then we'll customize it okay so within the appenders we have mentioned that i want to log the messages on the console till now right now in order to add the logs to a file this is what the tag you will be needing right so rolling file and then you have to specify the name of that particular file so let me quickly format this xml so source format okay and i will start with some of the explanation of this particular file here right this particular tag right so now in the rolling file we need to specify the name first okay so i can simply say this is my file okay so the name of the appender okay so i can specify as here in the console i can specify any name to this appender for the file i can specify any name here but i have specified file okay then in the file name i have specified this dollar and the file name right this value here okay now this is the basically referring to the variable all right now this variable i'll explain in a moment so this is what specifies what will be the file name all right so the next here tag is basically not the tag the the value here within the tag is the file pattern so what will be the pattern of the file say for example the size based triggering policy so what exactly this says is if your file has been reached a particular size level then it will create a new file right so that's what this tag will do so here i can specify say for example just for a demo i will simply say as soon as my file reaches 1 kb i want to create a new file and that file pattern is what will be followed whatever you specify here okay so for the file pattern what i'll do is i'll simply provide some of the dynamic details there so i'll say file name whatever name is there followed by log demo right and then followed by hyphen and for the file pattern i can simply say store my file right as or format the file using this hour minute second and then also include milliseconds there okay and then followed by dot log okay so this is how once the file size has reached this is how the new file should appear okay so the location wherever the file will be so in the file name we can specify so this is the variable we can specify the location wherever the file will be created and then the file name will be something like that so log demo and then depending on the hour minute and second and millisecond will create a log file and store it in the pattern let's keep the pattern same as we used to have here okay so this is basically how the messages are going to be logged into the file right so i have just replaced the pattern here the same as it was there in the console okay and size based triggering policy is 1 kb so i've reduced it so that i can show you when the new file gets created the naming of that pattern or the file pattern that we have specified here okay once you are done with this another important tag that needs to be added is about the configuration because we have specified this file name but we need to add the configuration so if you'll scroll a little bit on this document you will see that this properties okay so this properties need to be added there where you need to specify the name of the property that you are referring to okay so we'll simply copy this and this was you know outside of the appenders right so we'll simply paste it here below the configuration and the name is file name and in the file name what we need to do is we'll remove everything here now what we want to do is now usually in your automation framework you will be basically creating a logs folder right so i want to store all my logs into at the root level or basically at the project level hierarchy so a log folder should get created within that this dot log file should get created so in the file name what we can specify here as the property is we can simply say just create a logs directory okay so we'll simply say dot slash logs and now the file name contains this particular property so wherever you will say dollar file name that will be basically replaced with dollar or this logs right so when we say dot slash logs it's basically saying that at the root level project root check for this logs right folder if not then should create it okay so in the file name now it will simply specify up in the file name we haven't specified any of the file name right so we'll simply say for the first time just create the file with the same name or you can just say if first file you do not want to specify the time stamping there i can simply say log demo dot log but if i want the time stamping i can also provide this whole thing there okay something like that okay so let's quickly just leave it as is as of now so what it will do is this particular file name what it will do is this will refer to the file name property here which is basically saying that in the project root create a logs folder and within that create this logdemo.log file and append or push all the logs that are there into any of these you know log demo or these files into that particular file okay so let's quickly run this and see that everything is working as expected okay so here you can see that we if we go to this direct demo one which has this second file let me quickly okay so there are a couple of messages there let's run this and see that the file gets created in the logs folder so you can see in on the console it has been printed as well okay if i refresh this you will see logs folder got created and log demo file got created if i open this oh there are no logs okay so let's see what is the configuration here so reg demo is info right oh so appender reference you'll see so appender reference is still abc so what we are saying is we have specified the appender as file right but we haven't referred that appender anywhere so still we are just saying for this reg demo package log on the console right so what we need to do if we want to log at both places then we have to in the appender reference we have to specify that log into the console as well and the file as well okay if you want to log only on the file remove the console okay so now for this particular package the class that we are going to run it will only log to the file okay so if i run this now and open this log file you will see all the messages being logged okay if i change the log4j file and say that i want to log on the console as well as on the file okay so this is the logger the custom logger which is for a specific package in the root level as well i can say that log on the console as well as the file right now if i run this oh sorry so you'll see that it has basically logged on the console and then it has logged should be logging in the file as well okay you'll see that it has logged in the file as well let me go to the file again let's run again and see what was the error so it was main error unable to rename file logs to all right okay so there is some issue in the configuration because it is not able to rename the file okay so let's see it should actually append the file as soon as the size has been reached which is not happening let's try to run again and let's analyze the error it says error unable to rename this file invalid path okay illegal character at index 54 character okay so it looks like it's not right okay so it's not able to re name the file based on this character okay all right so let's quickly go ahead and here instead of this let's change it to hyphen okay so it should be fine because we are dealing with the files that might be the issue and if i run this now yeah okay so now it's fine with no errors and if i refresh this now you'll see that as soon as this file has reached a particular level uh then the old all the data has been basically moved there and in this file you will see that 17 21 28 is what the time is uh that has been logged okay so let me change the seconds here as well so we'll add the second so we can milliseconds basically so we can see the granular details there and if i run this couple of let's copy so it fills the file quickly and and see that the new file getting created all right you'll see that this is 1720 to 19 right and then 17 20 to 19. so any as soon as the file reaches a particular 1kb level it creates a new file right it it pushes the logs there and creates the new file with that timestamp and those logs are being stored right so that's what will happen in this particular case when you specify the size right so if you refresh again if i run again you'll see that it has run again and 17 22 51 354 milliseconds and the rest messages are being displayed here and all the old messages if you'll see they are post there okay so this is basically what this tag is all about size based trigger policy so depending on what size you want to specify you can specify the size here and as soon as the file the log file reaches a particular level it will create a copy so that you can refer those logs later based on the timestamp all right so this is a brief about how you can customize this particular log 4j xml and send the logs into the file right so we have understood a lot details here we have understood how you can add the logging for a particular package level with different log level at a root level what exactly it is then sending the logs to the console sending to console and files you just need to add these references right so file and file and console appenders so whatever name we have specified here that's what we are specifying here and when you are specifying this rolling file thing you have to make sure if you are using any of the variable here right so you specify that in the property and then use it here right so the the benefit of that is that here in the file name i specified which folder needs to get created there and then i can refer that right so if i'll simply say dot logs dot forward slash logs it will go to that particular project group location create the folder and then i can specify the file so you don't have to you know basically hard code any of the values here if you simply say that in the file name c colon forward slash details like that which is specific to your computer and you share your code to someone else they might not have that exact location right so that's where you should be using these properties and this is you know some of the best best practices that you should follow when you are doing the test automation development so that's pretty much all about the log forge and how you can send the files or how you can send logs to the file in log4j in the next tutorial we'll understand some of the more concepts about log4j so that's all i hope this was helpful thank you very much for watching
Info
Channel: Software Testing Mentor
Views: 24,032
Rating: undefined out of 5
Keywords: how to write log4j logs to file, write log4j logs to file, log4j logging to file, log4j logging, log4j logging tutorial, how to use log4j in java, logging using log4j, logging to file, log4j, log4j java, log4j tutorial, log4j2, log4j2 tutorial in java, log4j tutorial in java, log4j in selenium testng, log4j configuration, log4j tutorial in selenium, log4j file appender example in java, log4j in selenium framework
Id: r8x45Oy4IQc
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Wed Nov 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.