Log4j2 logging in Selenium Java Framework using Log4j2 Properties & Log4j2 xml

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back I'm ragwan Mr and in this video we will be configuring log4j2 logging and selenium framework from a scratch using log project to XML and log project to properties file and learn logger levels and then use logger and selenium script and we will see how logs are written into file and also on the console so before we start just keep in mind that the log 4G 1.x is duplicated and no longer supported and maintained so whenever we are choosing the logging Library uh now we should go with log4j2.x log4j2 is the new and improved version of classic log 4G framework so all right with that uh let's create a project and see how log4j2 can be implemented and used in selenium okay so I'll go ahead and create a Maven project in my eclipse again I will give some name to that project so let's give logger demo and click on finish I'll go ahead and expand the project open the form.xml and I have added certain I have put I have copied certain dependencies from me and depositories here so I will add it to my Bomb Dot XML over here and just save it so after saving it all the dependencies from the maven repository will be downloaded to the local repositories so you can see all your jars of log4j selenium and test engine whatever we have kept in surveyor whatever we have are saved inside this pom.xml has been added to the your uh build path of your project you can validate them by checking log 4G see log 4G API and log 4G core has been added to the uh a local reposit right into the build path so it will be made available across the project okay in all the files all these Scripts so you you would have noticed that I have added the dependencies of log 4G core and block 4J API and I have chosen two point X version okay and along with this you can add uh rest of the dependencies which are required for your framework right development so I have added a selenium Java I have added test NG and I have added web driver manager dependencies also you can include as many dependency as uh required for your framework okay so for now to demonstrate or for demonstration I mean like um these many dependencies are enough okay so you can go to the mvn repository and search for log4j over here and you see the log 4G core which you have to copy and you have to copy log 4G API okay uh this is the duplicated One log 4g1 so you don't need to choose this one okay here since yours is Maven project so you have to copy Maven dependency okay there are multiple differences available so don't go with the Gradle and all just go ahead and take this and add it to the form.exe which I have already done okay with that uh I will go ahead with the test script now so I'll create a class file called test logger so instead of writing each and every line I'll just save my time by you know going to my YouTube channel and click on the louder link over here I have created a Blog for for log 4G configuration for selenium Frameworks you can go through this blog and get the simple Java I mean this particular class test class copied and just paste it into your it is it can be any any script right uh of yours so you you you know right so looking at my videos previous video you can uh just create any uh number of you know class files as you want and then uh if you want logger to be implemented in your framework or your your project right so you can have those dependencies downloaded and in one of your script or any script or in your you know uh project or in your automation suit you can Implement you can uh invoke or you can create the instance logger instance okay using these two lines okay so consider this as a your script and then you add two more lines so things are quite simple here we get the logger instance by using the log 4G to log manager class and it's getlogger method so once we have that we use the info and error method of the log to write log records okay so here I can pass some info don't worry if we task for so many argument you just need to pass whatever you want uh whatever information you want a logger to capture so I will be writing that Chrome drivers setup is complete okay then I can add a log info here saying implicit weight of implicitly beta 10 second okay so I want these information uh to be captured in the logger framework I mean to the log files maybe in the upender or maybe on the console okay based on like how what upender we choose I mean those these messages will be printed over there either on the console or on the file I'll talk about that later so just uh this is the flow this is one end to end uh you know flow where I'm just opening uh Mantra site I'm just accessing one element I'm expecting this web element to be present on the myntra home home page if not then if this element is not found then it will go to the catch and it will actually throw one exception so that exception we want logger to capture okay so we are actually uh I mean uh creating a new exception and we are passing element not found exception to that exception and a whole a lot of a whole uh your message is to be captured or to a logger through this error method of log402 okay and then we come to the final block and we are actually printing this message we are cutting the driver and we are printing this creating the driver passes to the log okay so I'll just um run this program and we will see how it goes if that flow is proper or not so that flow is proper but we don't get anything logged over here okay we have we don't have so until we add any system.out.printel and message we won't get anything over here okay printed and neither we have defined any log4z2.xml log 4g2 dot properties and because of that is missing it is not printing uh the log we want okay these log we want to see now how we can achieve that so we can achieve them through uh some customized uh customized log which I have just mentioned over here so you can go to this again and get the log 4G to XML okay so this is the sample which I have added so we are customizing the log 4G to configuration over here so I'll just copy this and put it in our project okay so where we have to put this we have to put it inside SRC main resources okay so log4j2 will be looking for these properties and XML yaml or Json file okay and it will be very particular about that uh file name so you have to give log 4 J2 okay and then you can either give XML or based on like notation or you know syntax you you are using inside so I have copied this one and just save it so you can see I have copy pasted over here this the content of this file okay which I have copied from this particular blog which I have created for you so here you can see configuration you can see appender then you see console you can see this file attribute then you see logger so these are the main element okay and then you have a label okay so these are the things which you have to keep in mind so configuration this is the root element this offender basically this will um the soap log force it basically provides a Pender object which are you know primarily uh responsible um for printing logging messages to different uh destination as for uh possible Right such as console okay files anti-log events swing components JMS remote Unix syslog demons socket rdbms and Etc okay so this is very important right you where you want your lock to be printed or to be shown right you use appended for that reason for matter that matter and longer you can go ahead and customize the root level logger or package wise login okay and this additivity so which actually tells you have I would have added that information over here so additivity is set to True by default that is children inherit the offender of their ancestors by default if it is set to true so which means the children is inheriting the properties of their ancestor by default okay inherit the appenders properties append us basically of their ancestor so whatever is defined in the root level will be inherited okay by in the inside the packages also and the um finally this root level which is set to the info okay and a printer references file okay now let's come to the appender once again so we are actually we are having two appenders over here so console and file so file name is file and file name basically the file name is file Capital file and the file name is here you have to give the path where you want your log for log file to be generated okay so I'm choosing this on this particular path Windows attempt inside the C drive you see and then I want this log to be printed over here so this is case insensitive right so if even if you go with small it will work doesn't matter on Windows right actually so this particular log 4G DOT log will be generated inside this nothing is here over here right you can see and then this is the pattern okay which is timestamp and the Lockport and file levels and then masses and here also matches uh to be printed followed by this as timestamp okay and now here we are choosing original log uh label root level we are choosing info and the appender difference we are choosing as file okay so this file we are particular we want this information to be you know logged this particular log to be printed into the file which is nothing but this file okay and in this path along with this information this pattern so this cannot be run individually rather we have to I mean log 4G will be looking for this file and wherever we are actually passing these messages these will be captured by the logger log 4 J2 framework okay so let me run this there are some issue you can see connection reset okay doesn't matter uh we'll see if the log file got generated or not yeah this got generated you see and we are successfully printing the messages Chrome driver settings complete implicit weight of 10 second logged into mint Trend cutting the driver you can see everything is printed as a info and this is the timestamp and you are inside main method okay and this is the info and this is the message which we have passed to the log version okay you may see this connection research issue uh doesn't matter and it is not harmful as such but you can fix it also later let me run it again and see if that connection reset is again you know coming or not so once in a while you can see because of so many issues with the network also and see it is gone this time and we have not got anything over here all right so let me open and you see we have set the appender equal to True maybe because of that it is actually appending okay it is not overwriting see everywhere we have set append equal to true so which is why it is actually appending into the log file if it is set to false then it will be overwriting that file again and it will be printing only one message whatever message are there inside the test log okay so it will not be worrying about the earlier messages which was there in the log file okay okay we have seen one log Fourier two dot XML example now I will just delete it and maybe I can just rename it okay to something original so that Now log 4G will not be I will just clear the log and it will not be printing now okay because log 4G will not be finding this file see Let me refresh Maybe see nothing is written over here though the class I mean the execution script execution is complete okay but we have not got anything because uh now uh log 4G 2 has not found this particular you know class so which is why I mean we have just changed the extension to or is and it didn't find that XML which is uh it was looking for okay no worries intentionally we did that now we'll go back to this and use log 4J to properties fine okay so this is that properties file so I have written two examples over here so you can go with the console appender and there is example for console and file appender both so you can go ahead and choose this example okay and put it in one file called log fold just create one file called log4j2 properties file log 4 J to dot properties okay just paste the content over here again the same thing so this is the printer we have chosen uh console and log and this is the file name which we want our log to be printed inside so this might this will be our folder okay in this example so you can see like there is no folders with logs created so far okay and we are giving this as a property so we want our logs to be you know created inside the file name which is nothing but this you can see okay so I have added two appenders over here appended console and a Pender file and the pattern layout we have already discussed and this console this is the type and this is the console name and this is the type and this is the file name okay and this is actual file name you see over here so we want our file to be generated inside this particular directory which is logs if it is not created then it will create one and print the log if it is already created then it doesn't matter you go and create this log logs DOT log inside the logs directory okay and this is the pattern again same thing which we have discussed in log 4g2 this example all right and now we are choosing debug as a root logger level and we are choosing file appender and we are taking the uh giving the reference of log file okay which is this that file name okay with that we are all set and we can now expect that this particular you know debug message this time okay now you might be okay let me run it so again we can't run this program okay so we have to go to the class file and run it yeah so that's done and now if you refresh this project you will get logs folder see and you can open this lock version so it is printing all the debug statement which might be required in your framework okay as in when you want any information uh to be captured like okay so you can set the logger label basically okay so based on the logger level set it will be printing those you know statement to the logger okay it will be uh passing those messages to the logger and logger will be actually redirecting based on the offender used okay quickly we can discuss like what all should be printed and why we are able to capture the uh this one debugger statement right and when uh on what those standard uh levels should be chosen okay so as in when like you said the root lever to debug or infrared so info um info along with above levels will be actually above blocks will be captured into the loggers okay can we passed on if we are choosing a debug so debug along with info one error fatal of these will be supported by the logger okay so this is that hierarchy if you are uh you know to uh chosen or selected the war as a root level so you can't expect that debugger statement will work okay so it will not be printing okay I'll show you the example so just in case if I choose info it will be printing info and uh these uh you know messages so these methods will be supported okay so you have seen right so we and we have got the logs debug logs inside the locks dot uh log file okay see everything is over here everything is over here now if I actually want to print this into the console I will quickly show you like how that can be done so instead of this debug uh Let It Be debug and I will choose console this time and so rather than you know printing on to the file it will printing everything over here now okay I will run this class again and you can expect that everything all debug statement are printing inside the console see everything is printed over here see cool isn't it so yeah so that is beauty of logger log 4G XML whatever we want to print information right we want to capture in customized label right everything will be seen so that will it will be easy for uh you know developer to trace the script right to zero down the problem or they can easily get into the um issue and fix it what is the problem so looking at the logs it will be easy for them to you know find now we might be wondering wondering every time it is catching it is catching this exception and we are not coming we are not showing anything on this logger rate I mean whatever is written inside cache block so how can we fail we can deliberately fail that particular you know statement so that the control will come to the cache block and we should be able to get something for logger okay as a error so we have log uh log for the error method and we are actually passing this particular you know uh uh message to the error method of log 4G 2. okay now let's see I have deliberately failed this by changing the X path of that element now it the control should come here here okay and we have chosen debug as a root label okay so the error will also be captured okay we have already seen that okay so debug if we chose the root level as a debug right so error by default it will actually be captured okay if you are choosing a warning so you can't expect info will be actually uh captured okay so it will be disregarded let me see if I choose warning maybe uh here if I choose one okay so those statement whatever we have logged as a you know info will not be captured by them it should not be captured by the log version let's see so we are touching upon each and every uh possibilities so that uh you know it will be easy for you to understand like what is the flow and how it is working okay see so it has not captured anything but error it has captured you got it right you understood right so since we have chosen log 4J level as a warning okay so warming will allow actually whenever we are choosing warning so these error will be this error statement these error method will be actually recorded by the log 4J a framework but the info whatever we have given inside that test script will not be honored so these will be disregarded so if we are choosing this standard level so this info is the most popular one you should go ahead with the info okay until you want to do something okay and mind you it prints whole lot of things into the files so if you are choosing all so you can expect that whole lot of information will be passed on to the files which might be you know taking a lot of space in your system so you should be very careful while choosing the uh standard level also for the logs okay so info is the best one to pick okay info and uh debug and warning okay so this is the hierarchy and let me put this to info again and see we have also shown you like how the information will be captured by the error will be captured by the logger so it has shown that element not founded whatever we have written over here element not found so this is the exception new accession which we have passed along with the exception occurred so it would have printed exception occurred then it would have printed the it has printed the element not found in the Java lag exception okay in in line number 31. 31 this is the line which is here okay okay if you click here it will be actually taking you to this it will be highlighting that line cool isn't it so again I'm setting uh I've said the logger to info again and I will be executing then we will bind up this session okay this time I'm not um printing all those debug statement so it will be printing element not found this should have printed weight info we have uh given and we are giving a standard output let me give debug was it saved or not okay let me rerun and then see okay let it close element not found yeah so XPath and then yeah yes yes yes it is actually printing but we have to be uh very careful because we have chosen uh some of the exception uh you know customized uh thing okay uh I mean like method methods like when as in when you choose info it will be printing those statement over here somewhere we have chosen a debug this time so there are a lot many things to print right so you don't expect that okay right after uh it has printed debugger statement then it will print in for no so it has printed debug then it has printed your informations like interested weight of 10 second you can copy everything over notepad plus plus and see like how actually it is working and what all it has printed right so it will make your life easy okay as a developer so we are quitting the driver at last okay so just take the logs out into the notepad plus plus and see what all has been failed and what was the expectation if it is fulfilled or not so it will lock 4J or two will is going to be very helpful in if you are Developer and if you have you you are a automation developer or you know framework developer and if you have got into any any trouble and looking into the issue so this is the kind of you know mechanism which is going to help you out on various aspects of you know framework development so you should be choosing log4j2 as an option as I said like log401 has been deprecated and no longer supported so you should always go with log4j2.x okay so uh I hope you have liked the explanation and learned from this video please do like and subscribe to my channel and share it with your friends also share the feedback in comment section and thanks for watching I will see you next video till then keep learning
Info
Channel: SDET Adda For QA Automation
Views: 17,868
Rating: undefined out of 5
Keywords: log4j2, log4j, java, Basic Log4j2 Configuration, log4j2 Configuration Example, Log4J2 How It Works, Log4j2 Example Tutorial, Configuring Log4j 2, Log4j62, Apache Log4j 2, log4j2 tutorial, log4j2 configuration, log4j2 java, log4j2 properties file, log4j2 xml, log4j2 configuration location, log4j2 example, log4j logging levels, log4j appdenders, log4j file appdenders, log4j2 selenium java framework, log4j2 selenium, log4j2 file appdenders, log4j2 tutorial in java, log4j2.properties
Id: RkXTLd0gxMg
Channel Id: undefined
Length: 28min 24sec (1704 seconds)
Published: Fri Feb 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.