• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Reporting / how to create log files in selenium webdriver using Log4J

how to create log files in selenium webdriver using Log4J

March 15, 2015 by Mukesh Otwani 77 Comments

log file in Selenium

Hello welcome to Selenium Tutorials, in this post we will see how to create log files in selenium webdriver

In Automation, reporting plays an important role so we can generate the different type of HTML reports, XSLT report, Log files etc.

 

If you want to create enhanced report and with log attached to HTML report you can use extent report (I use this report) as well 

 

Here is the YouTube video for the same

 What is Log File?

The log file is a just simple file, which keeps track of the record or event or info when any event happens or any software run. This whole process is known as logging. We can create a log file as simple log file as well as HTML format.

 

Note- Any file whose extension is .log will be considered.

 

Sample Log file
1- Simple log file in text format

log file in Selenium
log file in Selenium

 

2- Log files in HTML format

 

how to create log files in selenium
how to create log files in selenium

Why is it required in Selenium?

We can create a log file for our simple script also so we can track or debug our script easily if anything goes wrong in the script. For example, if our script is failing at some point then we can track back what went wrong.

 

What is log4J- Log4j is free open source tool given by Apache foundation for creating log files It helps us to generate a log file in various output target.

How to create log files in selenium

Step 1- Download log4j jar file

Click on the link to download http://mirrors.ibiblio.org/pub/mirrors/maven/log4j/jars/log4j-1.2.15.jar

 

Step 2- Add log4j to your current project

Select your project > Right click > Click on build path> Click Configure build path> Go to library section > Add external jar file > select the log4j > click on save

 

Step 3- Open notepad and copy the below code and save the file as log4j.properties.

Note- Please create the log folder inside home directory and while saving use “” to save file

 

// Here we have defined root logger
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC

// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender

// Here we define log file location
log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/application.html

// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true

Once you save file it will look like

 

how to create log files in selenium
how to create log files in selenium

 

Step 4- Write test case

import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Google {
    public static void main(String[] args) {
      
    // Here we need to create logger instance so we need to pass Class name for 
     //which  we want to create log file in my case Google is classname
         Logger logger=Logger.getLogger("Google");
        

       // configure log4j properties file
       PropertyConfigurator.configure("Log4j.properties");


        // Open browser
        WebDriver driver = new FirefoxDriver();
        logger.info("Browser Opened");
      
        // Set implicit wait
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        logger.info("Implicit wait given");
      

        // Load application
        driver.get("https://www.google.co.in/");
        logger.info("Url opened");
      

        // type Selenium
        driver.findElement(By.name("q")).sendKeys("Selenium");
        logger.info("keyword type");           
    }
}

 

Step 5- Execute  your test case and verify the output and log files as well

Finally, here are three different log files.

 

how to create log files in selenium
how to create log files in selenium

 

Kindly Share this post with more friends and Spread the Knowledge 

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: Advance Reporting, Advance Selenium Tagged With: Log files

Reader Interactions

Comments

  1. Pankaj says

    July 16, 2022 at 8:18 PM

    Hi Mukesh,
    good ,i am beginner is selenium but you explain very well about loggers. My question is that can we make excel file of log instead of normal text file??

    Reply
    • Mukesh Otwani says

      July 22, 2022 at 1:07 PM

      Hi excel is preferred for test data not for logs.

      Reply
  2. saran says

    November 15, 2020 at 9:28 AM

    Hi Mukesh Sir,

    Will the above steps in “How to create log files in selenium” will solve in linux machine ??

    Reply
    • Mukesh Otwani says

      November 15, 2020 at 9:42 AM

      Hi Saran,

      Yes, this will work in linux environment too.

      Reply
  3. Rajesh says

    July 24, 2019 at 4:26 PM

    Hi Mukesh,

    Is it possible to catch test steps, results into log file without calling log in test steps

    Reply
    • Mukesh Otwani says

      July 24, 2019 at 9:55 PM

      Hi Rajesh,

      Yes, it is possible using Listeners. You can use either TestNG listeners or WebDriverEventListener or both as per your required granularity.

      Reply
  4. Hare Krishna says

    July 17, 2019 at 5:54 AM

    I left the same comment under your video about how to generate log files. In case you see this first please help.

    Mukesh, thank you a lot. I am learning like in a good school from you. Would you know how to add clickable line # in Intelije? I used the same and tried other log4j file content and I am getting this:

    0 [main] INFO RandomPractice – Google is launched
    17 [main] INFO RandomPractice – Search field is located
    143 [main] INFO RandomPractice – Hare Krishna is entered
    2126 [main] WARN RandomPractice – Searching for the entered text

    Whereas I am aiming for getting what you have in the video. I ‘ve heard Inlelije works differently. I have my “log4j.properties” file, may be you can advise what I can add to get the clickable line # or may be there is another way. So far after going to so many blogs, I haven’t figured that out. Thank you very much. All the best to you!

    Reply
    • Mukesh Otwani says

      July 18, 2019 at 2:40 PM

      Hi Krishna,

      Please check this link https://www.jetbrains.com/help/idea/setting-log-options.html

      Reply
  5. nikita says

    June 13, 2019 at 5:07 PM

    contents are not overriding

    Reply
    • Mukesh Otwani says

      June 13, 2019 at 5:33 PM

      Hi Nikita,

      Kindly use log4j.appender.S.Append=false in log properties file

      Reply
  6. Sourabh says

    May 16, 2019 at 1:13 PM

    I have implemented this and its working fine but I want to override the existing log file whenever I run the code but currently it appends the new logs in the previous log file

    Reply
    • Mukesh Otwani says

      May 16, 2019 at 8:27 PM

      Hi Sourabh,

      Add log4j.appender.LOGFILE.Append=false into your log4j.properties and let me know.

      Reply
  7. suyash says

    April 18, 2019 at 9:24 AM

    Hi,
    I am getting this
    log4j:ERROR Ignoring configuration file [Log4j.properties].
    log4j:WARN No appenders could be found for logger
    log4j:WARN Please initialize the log4j system properly.

    Reply
    • Mukesh Otwani says

      April 18, 2019 at 11:31 AM

      Hi Suyush,

      These messages come because log4j has not been implemented fully. As of now, it is fine. Just check whether you are getting logs properly or not…

      Reply
    • Hare Krishna says

      July 17, 2019 at 5:55 AM

      Add this line and the issue will be fixed:

      BasicConfigurator.configure();

      Reply
    • Priya says

      October 3, 2019 at 5:08 PM

      Place log4j.properties file under resources folder.

      Reply
  8. divya says

    February 17, 2017 at 11:22 AM

    Hai Mukesh,
    Im starter in automation selenium, ur posts are very useful for step by step procedure..Thanks for ur effort..

    Reply
    • Mukesh Otwani says

      February 17, 2017 at 11:58 AM

      Hi Divya,

      Thanks for your appreciation.

      Reply
  9. Debanjan Bhattacharjee says

    February 16, 2017 at 2:24 PM

    Wonderful Video Article on Log generation in Selenium. Thanks a ton !!!

    Reply
    • Mukesh Otwani says

      February 16, 2017 at 3:20 PM

      Thanks Debanjan 🙂 I am glad you liked all concepts.

      Reply
  10. Sanjay Pradhan says

    January 29, 2017 at 2:57 AM

    Very helpful and easy to follow.
    Thanks so much!!!

    Reply
    • Mukesh Otwani says

      January 30, 2017 at 9:14 AM

      Hi Sanjay,

      I am happy to see that my post helped you to implement your requirement.

      Reply
  11. shalaka says

    January 25, 2017 at 3:42 PM

    Hi Mukesh,
    Very helpful post for generating logs. I have a question that when the code in eclipse is big enough, it is tedious job to write logger.info(“”); statement after every line. Is there a way out?

    Reply
    • Mukesh Otwani says

      January 28, 2017 at 6:07 PM

      Hi Shalaka,

      As of now this is the real way to implement log file. For your simplicity, create a method which accepts arguments for logging as parameter.

      Reply
  12. falak garg says

    January 13, 2017 at 12:48 PM

    hey mukesh..
    how can i do logging in selenium webdriver using python binding?

    Reply
    • Mukesh Otwani says

      January 13, 2017 at 2:27 PM

      Hi Falak,

      I never worked much in python. Apologies for not helping you.

      Reply
  13. Nikhil says

    January 9, 2017 at 12:02 PM

    Mukesh how do we configure properties file for Log4j, because it varies for project to project. Is there any site we can learn from

    Reply
    • Mukesh Otwani says

      January 10, 2017 at 9:42 AM

      Hi Nikhil,

      Could you please explain your scenario.

      Reply
  14. Nirupa says

    October 26, 2016 at 10:42 AM

    Hi,
    How to resolve the error:” Unable to instantiate the class[org.apche.log4j.patternLayout]”

    Reply
    • Mukesh Otwani says

      October 26, 2016 at 11:49 AM

      Hi Nirupa,

      Kindly go through advance logs with HTML reports http://learn-automation.com/advance-selenium-reporting-with-screenshots-2/

      Reply
  15. chandrika says

    October 19, 2016 at 6:46 PM

    Hi Mukesh.

    I have tried to generate logs its generating in console only not into file.
    When i tried to save save log4j.properties file with double quotes its saved as log4j.properties not like log4j.
    Please help me in this.

    Reply
    • Mukesh Otwani says

      October 20, 2016 at 1:56 PM

      Hey Chandrika,

      You can try this advance logs with good reporting http://learn-automation.com/advance-selenium-reporting-with-screenshots-2/

      Reply
  16. siva says

    September 26, 2016 at 9:58 PM

    Hello Mukesh sir,

    How to use debug,fatal,error,warn methods in log4j.

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 11:02 AM

      Hi Siva,

      you can use in below manner

      logger.info(“Url opened”);
      logger.debug(“Url opened”);
      logger.error(“Url opened”);

      Reply
  17. Nikhil says

    September 14, 2016 at 11:44 PM

    Mukesh very good explananation. How do we learn how to configure log4j properties file?

    Also what is this line meaning – PropertyConfigurator.configure(“Log4j.properties”);

    Reply
    • Mukesh Otwani says

      September 15, 2016 at 4:40 PM

      It means we are specifying the file where all settings are defined.

      Reply
  18. Pratikshya says

    August 23, 2016 at 11:50 PM

    Hi Mukesh

    Very nice Explanation. Thank you . I have one doubt . I have used Logger.info in my selenium script and it works fine when I run in local eclipse. But when I run the same program in jenkins , it gives warning for Logger File. Why the same Logger configuration didnt work in Jenkins ?

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:11 PM

      Hi Pratikshya,

      Ideally it should not give any warning. Have you tried with advance report http://learn-automation.com/advance-selenium-reporting-with-screenshots-2/

      Reply
  19. suresh says

    August 4, 2016 at 2:12 PM

    hi mukesh,

    I am using extent report with attach screenshot but how can I specify the path of failure screenshot.
    screenshot like gmail.08082016121213.png
    it will be generating at failure time and how, I want to take that one’s path as specify in report path

    Reply
    • Mukesh Otwani says

      August 5, 2016 at 10:36 AM

      Hi Suresh,

      I covered the same in below post
      http://learn-automation.com/advance-selenium-reporting-with-screenshots-2/

      Reply
  20. Ajay Somni says

    April 26, 2016 at 8:47 PM

    Hi Mukesh, Thanks for your valued contribution to the tech community.
    Kindly provide a tutorial on new backlog. Thanks

    Reply
    • Mukesh otwani says

      April 26, 2016 at 11:55 PM

      Hi Ajay,

      Thanks . Can you please explain for more about backlog tutorial ?

      Reply
      • Ajay Somni says

        April 27, 2016 at 2:47 PM

        Sorry it was LogBack logger….
        http://logback.qos.ch/

        Reply
  21. ganesh says

    March 22, 2016 at 2:29 PM

    how can i append the error code in exception block to the log file. logger.info is not working in catch block.

    Reply
    • Mukesh Otwani says

      March 26, 2016 at 2:02 PM

      Hi Ganesh,

      I would recommend please use extent report.

      http://learn-automation.com/advance-selenium-reporting-with-screenshots/

      Reply
  22. ganesh says

    March 17, 2016 at 2:20 PM

    how to generate a new report every time when i run a program because its showing all previous reports.

    Reply
    • Mukesh Otwani says

      March 20, 2016 at 7:44 PM

      Hi ganesh,

      use extent report in this case.

      Reply
  23. Sireesha says

    February 25, 2016 at 5:26 PM

    Where we have to copy the file? what is home directory?

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:11 PM

      Hi Sireesha,

      Home directory mean where project resides.

      To get home directory Right click on project > Click on properties > get the location

      Reply
  24. Prasanna says

    February 22, 2016 at 11:26 AM

    I want to genrate seprate log files for these indepent pice of code

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 5:10 PM

      Hi Prasanna,

      In this case try extent report with Logs http://learn-automation.com/advance-selenium-reporting-with-screenshots-part-2/

      Reply
  25. Ratna says

    February 13, 2016 at 10:08 AM

    Explained very well. Excellent teaching method.
    Thank you so much for sharing.

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 5:29 PM

      Thank you Ratna 🙂

      Reply
  26. Siddharth says

    February 9, 2016 at 8:55 AM

    Hi Mukesh

    Right now I am unable to download the log4j jar file from the above mentioned url given by you.

    Can you please check this once.

    Reply
    • Mukesh Otwani says

      March 7, 2016 at 11:10 AM

      Hi Sid,

      Thank you for correcting. Please find below link for jar

      https://logging.apache.org/log4j/1.2/download.html

      I would recommend for extent report which create advance logs https://www.youtube.com/watch?v=dBj7pxhZXhY

      Reply
  27. pooja says

    January 23, 2016 at 1:13 AM

    very helpful videoas . thanks a lot . please can you explain about how to automate when the images in the application keeps moving.” http://store.demoqa.com/” website link

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 5:12 PM

      Hi Pooja,

      Thank you.

      Can u please explain your requirement in brief? Do u want to check the image presence or url of images ?

      Reply
  28. rahul says

    January 15, 2016 at 2:29 AM

    awesome Explanation …….

    Reply
    • Mukesh Otwani says

      January 16, 2016 at 11:48 PM

      Thanks Rahul 🙂

      Reply
  29. Prabhakar Reddy Gollapudi says

    January 12, 2016 at 2:28 PM

    Hi Mukesh,
    I am following you from last 2 weeks and i found your blog very useful,completely different from other one.
    Keep posting new things and make the people know more 😛

    Reply
    • Mukesh Otwani says

      January 12, 2016 at 6:39 PM

      HI Prabhakar,

      Thank you so much. Keep visiting I have so many post in pipeline.

      Reply
  30. Sandeep says

    January 11, 2016 at 11:11 PM

    Where do you save the log4j.properties, i mean what folder does this needs to be saved to

    Reply
    • Mukesh Otwani says

      January 12, 2016 at 6:39 PM

      Home directory

      Reply
  31. shubham says

    January 5, 2016 at 6:40 PM

    Thanks it worked for me…able to see logs for console,text and html.

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 12:50 AM

      Cheers Shubham

      Reply
  32. yaswanth says

    January 4, 2016 at 10:49 PM

    HI Mukesh I want to store the logs in excel file .please tell me how to do this.

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 12:59 AM

      Hi Yeswanth,

      I will suggest you not use excel for logs.

      Reply
  33. Rajiv says

    December 10, 2015 at 12:04 PM

    Hello Mukesh,

    I am using maven and try to save the log results in log file. Results are printing on console but not same in log files files: Following is the console out put.
    ————————————————————–
    Dec 10, 2015 11:58:13 AM pageTests.log4j main
    INFO: Browser Opened
    Dec 10, 2015 11:58:25 AM pageTests.log4j main
    INFO: Implicit wait given
    Dec 10, 2015 11:58:25 AM pageTests.log4j main
    INFO: Url opened
    Dec 10, 2015 11:58:28 AM pageTests.log4j main
    INFO: keyword type
    —————————————————————-

    How can I save logs under log file in maven. I have log4j included dependencies in pom.xml file.

    —
    Rajiv

    Reply
    • Mukesh Otwani says

      December 12, 2015 at 3:57 PM

      Hi Rajiv,

      Can u please share the log4j property then I can check the exact issue.

      I would recommend to use extent report which have log with report itself.

      Give a try http://learn-automation.com/advance-selenium-reporting-with-screenshots-part-2/

      Reply
  34. Matt W says

    November 12, 2015 at 12:25 AM

    How can I catch an error using this. I’m hitting or using the Firefox driver with C#. What is happening for me is that my tests run successfully in Visual Studio or from a command line outside of our TFS Build process, but during the build, the same test case keeps failing. I’m able to capture a screenshot on error, but it isn’t very helpful.

    I’d like to see if a control can’t be found, it times out, etc. Would this be the tool for me.

    Any help to catch errors in the log if this tool will work for me would be greatly appreciated!!

    Reply
    • Mukesh Otwani says

      November 12, 2015 at 9:44 PM

      Hi Matt,

      I have not worked on C# so not sure can you please check below link http://www.toolsqa.com/selenium-webdriver-tutorials-in-c-selenium-tutorial-in-c/

      Reply
  35. premkumar says

    October 28, 2015 at 2:09 PM

    its works thanks a ton MUKESH OTWANI

    Reply
    • Mukesh Otwani says

      October 28, 2015 at 2:50 PM

      Great Cheers Prem 🙂

      Reply
  36. AthenaTietjen9 says

    October 4, 2015 at 7:00 AM

    Hi there, just became aware of your blog through Google, and found that it is really informative. I’m going to watch out for brussels. I’ll be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers!

    Reply
    • Mukesh Otwani says

      October 4, 2015 at 12:20 PM

      Thank you 🙂 Yes will continue with the same.

      Reply
  37. TyreeKeener28 says

    September 26, 2015 at 9:21 AM

    Nice post. I was checking continuously this blog and I am impressed! Extremely useful info particularly the last part 🙂 I care for such information much. I was seeking this certain information for a long time. Thank you and best of luck.

    Reply
    • Mukesh Otwani says

      September 26, 2015 at 1:11 PM

      Thanks 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?