How to Generate Log Files in Selenium using Log4j API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome again surveys today I am going to cover a very important but simple topic that is how to generate lakhs with the help of log4j APA in selenium and a lot of people they are asking that okay how do you generate the logs and everything and people they might ask at a time of interview also that you know some basic couple of questions about log4j but it's very simple and it's like a very good enhancement in your framework that you can generate some good amount of logs and how to implement the log4j api and you have to think it's very simple in fact developers also Java developers also they use the same API to generate the logs right through generator logs on the Tomcat or any any application server so they always most of the time they are using log4j api it's very simple so quickly let's start how to do that let me check the recording is on perfect okay so what I will do simple I will go to my Eclipse I will create one simple project okay and simple maven project I will create and I'll go to project and how to generate the projects for newcomers please see how to generate the with how to create the maven project and everything I have covered so many times in fact in maven tutorials also in page of your tutorials also you can refer so let's see my group ID is generate logs and artifact ID is also generate logs and click on finish and what I'll do that I'll write my selenium code as well as test in G code so I have to add a dependency in my form go to XML file so I will remove this j-unit dependency first and let me add the dependency for for selenium n this guy ok selenium and test changer dependency I have already added and the moment you save it automatically maven dependencies will be added over here now to generate the logs I have to add one more dependency guys that is called log4j dependency so you can download the log4j jar file or you can search on google simple that log4j maven go to one point two one seven this is the latest version of log4j just copy this thing copy this dependency and add it over here okay simple the moment you save it and you will see that log4j jar file also will be added over here over here right now we are good to good to write the code and in SRC main job is our three tests our these dummy sample packages I will delete let me delete this package also I will create my one package SRC test Java and right click on it go to new and create a package that seemed a package name is comm dot test package okay click on finish and I'll create simply I will creat a simple test class for free CRM dot-com application that we take already taken examples in the previous tutorials so let's see my file name is a login test ok click on finish in this login test what I'll do simple I'll write my simple test ng code ok so this is a site I will show you for newcomers I think where it is just a minute this is so this is the free CRM dot-com so on this login page we will write to that this logo is available in the title is available that's it only two test cases I don't want to waste my time on writing the different test cases because here we are just covering that how to generate the logs ok so simple login test and then quickly I'll write my code that driver driver I'll initialize method drive not initially I just define the webdriver reference imported from selenium and I'll write add the rate before a method and simple ID right public void setup method great and in this ad read before method is coming from text in G so this attack method I launch my browser and chrome and setup the property for chromedriver dot exe and entering the URL so that code is already available we have written so many times so just a minute yeah so I'm not wasting time writing the same thing again and again okay so this is the thing and this chrome driver that we have to import it from selenium time unit is also we have to import it from Java you okay Java util package and add the rate after method also I'll add this is a head read after method to quit the browser okay and to test cases I will be writing two test cases I will be writing guys my two test cases will be these two test cases that okay so these two test cases and be writing just import all the things only from test ng test change a notation okay fine I have added two test cases that free CR and title test and free see our logo test that I am getting a title and am asserting over here and getting the logo I mean the like checking the logo is displayed or not and asserting over here very simple two test cases I have written I am NOT discussing how exactly I ever done these cases because so many times you have already discussed this tutor is only an only four locks now couple of interview questions that I have written over here that can explain so what is in the log C gets log means to join to capture the information what is going on at the time of execution when you execute your program so JVM Java also executing something selenium also executing something test ng also executing something it's a lot of AP they're working together and doing their respective work you want to know what is going on on each and every step okay what are a different execution what are different calls in the background going on right so that you can understand the the execution properly that what exactly is going on let's you're running your execution or program or selenium code on some remote machine so every time you cannot see the that what is going on on the remote machine that every time you have to go to that VM machine or remote machine and login into the system and check what is going on no you can monitor the logs directly from your local system also that what is going on inside the logs right so logs it's very important in terms of debugging the code in terms of what is of what are the different failures okay at the runtime okay to check the exceptions and I need and pointer exceptions or any kind of exceptions are coming what is the reason behind that or maybe some file is missing or some jar is missing easily you can track it from logs so the what is log log means capturing were in for activities at the time of program execution sorry this is a very straightforward definition of logs there are different types of logs that info warning error and fact this is a level level of logs info warning error and facted okay so they will ask you a time of interpreter to give water a different four levels of locks so these are the four levels like info funding error and factor okay sorry guys how to generate a logs could generate the logs in Java we use apache log4j APA okay we use apache log4j APA we use to generate the logs so what is the mechanism that how exactly it works it works it reads a lot forger configuration from log4j dot properties file so this file is the most important file to generate the logs so log4j jar file we have already added a lot for change our file we have already added in our dependency and we have to create now this log4j dot properties file we're to create guys this is the most important thing we're to create on this file create inside the resources folder okay you have to create this log folder or properties file inside the resources folder okay inside the resort source folder so how to create this resources sort folder just simple right click on it go to new and create a source folder okay and then simple right SRC main sources okay SRC main sources you have to create just a minute let's not say main these resources you have to create our sources and rename it so the resources you have to use okay create this SRC main resources and inside this SRC main resources source folder go to file and create a new file and the file name is compulsory guys log4j dot properties this is the file name that you to create log close your dot properties don't write any property or something and don't change the name that ABC door properties or something which compels you to write log4j el small log for Chittoor properties click on finish right and here you have to write define some properties in the form of key and value pair format because properties means key and value player format and that key and value pair format I will give it to you this is the nori to remember but I will explain you what are different information you have to provide different levels you have to provide over here simple you just need to copy paste and provide this information in my great repository also plus in the description also you will find this or in the comment also I will provide this log4j tor properties okay now here different kind of levels we have the first level is that what kind of Road category I want to generate it inside the debug mode inside the console mode as well as I want to generate my logs in the in a particular file also so console means this is the console on the console also I want to generate the logs and the file also I want to generate a lot okay for console configuration that we have to use console appender okay so what do you mean by appender appender is the responsible thing that in which is provided by it okay which is provided by the log4j which right on the console which rights to console consuming this console or maybe through terminal also you want to generate you want to execute your program in the console and the terminal also in the command line also you will command prompt also you will get the output proper knocks coming over there what is a pattern layout this is the pattern layout that this is the pattern layout I want to you and this is my pattern that I want to generate my logs on the basis of M and D dy by the date Y is number of ours minutes and seconds and this is the one variation don't change anything over here things okay this is the standard configuration towheaded by log4j tor properties same thing how exactly you want to generate a file so my file is like again for file they provide a rolling file appender which writes to a file the rolling file appender is the appender name and console appender is the offender name for console and rolling fine defender is the appender fault file to write logs into the file and this is my file name you can give any file name let's say I give application dot log lets you like this application dot log is a file name okay and now we have to define some properties for file that let's see for 10 MB I'll define that 10 MB is a maximum file size after 10 MB is done then create the index okay for 10 it means first file is done let's see file size is full right then automatically it will create the backup for 10 times it can create the backup you can you know this is configurationally tomorrow let's you want to make it for a hundred you can make it hundred also I do want to increase the size for the logs and you can log file you can just increase the file size also but I think 10 MB is more than more enough for you is because in selenium generally we don't use so much of logs and everything ok and then what kind of pattern layout this is a pattern layout at ISO 8601 it's at will into that generate the you know ISO 8601 is the standard dates okay according to this date please generate the date format and everything but this is a standard concept that we have to use okay standard configuration that we to use one more configuration I will tell you that is called log4j appended dot file dot append is equal to 2 I'll tell you what do you mean by append is equal to true or false ok so this is a just a configuration guys we have to use now if you run this login test dot Java okay now if you run it log into this dot Java run is simple like this see if you see that it's generating some logs like this right see it's reading some logs the default logs like this in the console why this console is getting such kind of output because of this log4j door properties that we have defined the console appender ok and if you see maybe some desk is going fill listen let it be if you see the pattern layout that mmddyy today is whatever the date may be I don't know why it is taking 11th of January 2017 maybe fine so we will see what is the date issue we will check but you can see it's generating some locks like this ok and if you refresh your generate logs now I want to see that it's actually creating this file application dot log or not it is a refresh your project if they should and you will see that application dot log file them we just open this file you will see that logs are getting generated over here also okay oh sorry it's mmdd violence sorry so Monday is 11th is November 1st November 2017 so like this it is generating the logs ok they have given mmdd violent so an application log also see if you can see the application log see now each and every whatever then the background is going on each and everything it will be captured it is sending the request to this localhost this and making the connection with this the selenium is making the connection it's sending the post request as I told you I remember that webdriver architecture sending the post request and making the JSON and interacting with the browser and see the dessert capabilities in the background we don't see the desired capabilities okay an actual console without log food so it's actually sending this information that we are using the Chrome browser but all the name is equal to chrome and then it's interacting with the browser and everything in making the connection with the browser and then right see all the information is coming over here this is the session ID got generated this is the post session ID got generated and after the session ID got generated then see it is trying to maximize the window okay see see I told you I'd everything is happening through wire protocol the number of webdriver architecture will be discussed and these are the different headers we are passing internally selenium is passing headers in the form of rest api so each and everything you can check it over here guys this is like something very amazing to log4j you can generate it now so guys as you can see that we are getting the blocks inside the console as well as inside the application dot log file so we are getting all the logs right so this is very simple that simple this is how we generate the logs okay now we will see why our test case were getting failed because of some timing issue because this page is taking a lot of time to load see this page could not be loaded and we have given the page load time mode for 20 seconds so lets me increase to 30 seconds and then again we will run and then we will see in the console how exactly it's generating the locks and then I'll show you the concept of a pendant now see each and every information it's taking taking and for each and every time also at first November at 12:30 9:40 here in Germany time is around 12:40 and you can see that each and every activity see it's launching the URL like this sending this HTTP request through the HTTP wire protocol and see it's generating the URL and it's still loading I think this site is today it's very very slow now see it's launching the okay launching the URL and close the browser and see getting the response and two test cases got passed see both the test cases got passed and the complete application I mean complete error logs and everything we are getting over here not error logs I would say locks and inside the application dot log also you can see all the logs are getting generated nowadays if you see one thing that I executed my test cases at 12:40 right the last execution was on 12:40 right on this date what I want I have one property this property name is append equal to true or false append to means whatever the previous logs were there if you want to append with those logs let's see I have this log ABC right in a second execution also if you want to generate the logs right with the append equal to true so what will happen after ABC the next logs will be generated like this ABC XYZ right now if you write append equal to false so what will happen let's see first time you were executing logs and it's generating the logs inside the file ABC next time if you're making append equal to false and then if you are running what exactly it will do it will overwrite ABC first it will remove the entire logs it will clear application dot log file then it will write the new log like this so is a property of a pending that append equal to 2 means append with the previous locks now let's say right now current time is 12 41 now if I start my execution with append equal to true I'll execute my code again and you will see that so application dot log see it's after 1240 that was a line number 978 after 20 or 40 its appending C immediately after that it's appending the new logs right after 12 for thr pending now let me terminate it and I'll execute my test cases again and what I'll do log4j property this append property I'll make it false it means don't append with the previous logs okay don't offend with the previous locks now what exactly it will do it will finish first it will clear this application dot log and from the first line you will see that it will show you that 1242 1242 it will show you see it's a pending from 1151 then from 1151 to twelve forty two and thirty nine twelve forty it's a pending right now if you execute it now let's executed see an application dot log see automatically at 12:40 and what executed right so append equal to false means just remove the previous locks append equal to 2 means append with the previous locks don't overwrite append it will to false means just overwrite the previous locks ok don't append it so this is the property they might ask you at a time of interview also right and now what I'll do I'll execute through maven on the console on the terminal so well go to this directory copy this tree and I will launch my terminal this is my terminal and what I will do and go to the directory ok here that of SRC folder and everything is over here and this simple right maven install let's see its see it's building that all the things it's running my tests key to see in the console also you can see the logs right in the console itself you can see the logs see full the full logs are coming in the console also so right let's see now you are you're executing through selenium grid on some different machine you just need to SSH into that machine you just need to remote log it into that machine or maybe SSH from your laptop from your local machine and then you can check the logs what are different things are going on from your laptop crate so you can see the log from the console also like this to maven command if you're executing maybe some some failure issues so maybe some other way again timeout issues are coming because of this thing because of the application let's forget about okay so like that we are getting okay now I think that's all for this thing in log4j okay so this is how we generate the records now one thing let's see deliberately like if you want to put some information your own logs you want to generator what I want that okay after this new Chrome what I want I want to generate my own logs for how to do that I want to write my own long line also how to do that so at the class level you just define one logger one class is there logger log is equal to like this logger dot get logger method is there okay guys and you have to import it from test NGA's logger dot get logger.log 4j APA and all that you have to use and what is the plus this is my class name you have to write the class name dot plus okay and and before method what happened yeah now with this long reference object okay we can use let's see I want to generate log dot method is there long gone info so long do you know what kind of message you want to write let's see my message name is that launching Chrome browser okay and then after that I want that log dot in for simple I'll write entering application URL like this so you can write your own information also right you can print the title you can print a title like long dot info and you can write like this login page title is plus title right with the variable name also you can write just like system dot out dot print and you just need to write log dot info like this right if we want to print any warning or error so you have to use a couple of things okay so let's see some warning or info let's you you want to write log dot we have different as I told you one okay log dot one is a method to generate the warning messages this is simple alright hey this is just a warning message okay if we want to print some error which we log dot info log dot title okay some final message you want to print simple okay this is just a fatal error message like this so if you see that if you deliberately want to print some warning or maybe some error message or something like that in that case vice you can do that log dot let me check what are the different other things like we have one more method that'll debug if you want to put any debuggers and simple debug message you can write like this is debug message like this so these are the different of info warning Fattal debug these are the different levels okay okay like this this is a debug not air like this fertilizer chilies air okay so like this these are the four different levels of logs info warning debug pattern like this you can print accordingly your own logs also plus along with the these logs will be generated so if you run this program let's run it and you will see there to keep the along with the different logs these four lines and launching Chrome browser everything is also getting printed over here let's you want to write at a log simple dot info I want something like this some good printing like this that right and my test case name I want to paint like this this is my free CRM like this right and I'll try it like this log dot info starting test case like this then same thing for I will write it for second test case this test case and I have once the test case is over you can write like this sorry so I'm go to the graphics so that you will see in the console that that ending test case and this stress keys will be ended and same thing we can write for this guy right like this or you can call you can create inside a matter these two line you can call that method directly so like this right and once the test cases are done driver has quit let's see I will write some info over here that [Music] okay or browser is closed something like this either so whatever the information you want to provide rate you can write like this and let's say the starting of my this thing also I am writing the starting text this is execution something like this okay what are the information you want to provide so I one I'm printing all these stars start strands like so that I can see because in the console if you see you will be confused at what is going well so many logs are there but with some your own annotation if you are writing like an all these stars if your pattern you are providing then you can easily find out okay this is my log like that okay now let's run it and let's see how its printing in the pencil you see that in the console see see starting - his execution and then it's doing okay you have to check your log that okay so let's see see starting test execution and the first test case I think still it's launching okay like that it's taking again this site is so slow today so let's see in the console again see time out exception is coming but guys you guys can check so whatever that pattern you are printing it it will print this particular pattern over here same thing lets me try with the Maven me and clean installed and then if you run through maven and it will generate the pattern see starting edge case is right all the console information it's coming over here all the logs on the console sending this at this point of time it's stuck because of the application is very slow today all the page load time mode we have given 20 but it's crossing 22nd also today it's very slow maybe in Germany it's very slow now you can omit any you can try on your domain so I think now it's coming so let's see it's very slow today I don't know I think a lot of people hitting this application today or maybe gaining time out exceptions so I did not provide any this thing again timeout exception close connection okay but case it's okay you can check and how exactly it's printing in the console through maven and everything like this you can design the pattern like this okay so I think that's all for today and for this session and I'll share this code into my integrate repository okay you can okay see in this I will create in Devine animation 20 a git repository in this account I will create another okay another package over there so you can see over here that lets you log for generation something like that alle create and you can refer the log4j properties over there and everything I'll provide the the URL inside the description also okay in the description itself I'll provide you can refer it and guys if you really like the video so please subscribe to the channel and okay share with your friends and okay and press the bell icon so a lot of videos are coming in to each other you will be notified accordingly thanks for watching guys please subscribe to the channel thank you
Info
Channel: Naveen AutomationLabs
Views: 115,916
Rating: undefined out of 5
Keywords: Selenium, WebDriver, Log4j, Logs, Generate Logs, how to generate logs in selenium webdriver, selenium log file, log4j properties in selenium webdriver, testng log4j configuration, download log4j jar for selenium, log4j jar for selenium webdriver, selenium webdriver console log, log4j learn automation, How to Generate Log Files in Selenium Webdriver
Id: C7TEuhgVDYo
Channel Id: undefined
Length: 32min 49sec (1969 seconds)
Published: Wed Nov 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.