Skip to main content

Implement slf4j logger in automation projects

  1. Check for latest plugins from:

    • https://mvnrepository.com/artifact/org.projectlombok/lombok

      Search for 'slf4j' (https://mvnrepository.com/search?q=slf4j) and 'slf4j-simple'

    • https://mvnrepository.com/artifact/org.slf4j/slf4j-simple

  2. From the latest version page, copy the maven dependencies and add them on your pom.xml file. The dependencies would be like this:

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.12</version>
                    <scope>test</scope>
                </dependency>

            <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                    <version>1.7.30</version>
                    <scope>test</scope>
                </dependency>

  3. Saving your pom.xml file will download the required dependency files.

  4. Add 'lombok' plugin in your IDE. (Settings> Plugin> Add new> Search for 'lombok'> Install). You need to restart your IDE to complete the installation.

  5. Add  the following in your files 'import' section:

    import lombok.extern.slf4j.Slf4j;
  6. Add '@Slf4j' annotation just before the class.

  7. Print log as log.info("Your log text herer"); on any test / action methods

  8. In the project directory- src/main/resources, create new file named logback.xml

  9. Add maven dependency on pom.xml file:

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>


    * Declares logback-classic, it will pull in the logback-core and slf4j-api
  10. Content of the logback.xml file is as follows:

    <?xml version="1.0"?>
    <configuration>
        <appender name="STDOUT"
                  class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>
                    %cyan(%d{HH:mm:ss.SSS}) %highlight(%.-5level) %yellow(%logger{50}) %highlight(%msg) %n
                </pattern>
            </encoder>
        </appender>

        <root level="info">
            <appender-ref ref="STDOUT" />
        </root>

        <logger name="Test Automation Framework logger configuration file" level="info" />
    </configuration>

  11. Note: If you use logback-slf4j in your project, you may need to remove the following 
    dependency from your project pom.xml file.
    "slf4j-simple"


        Notes:

        References:
  • https://medium.com/faun/tweaking-your-ui-automation-logs-5620b051969b

  • https://mkyong.com/logging/logback-xml-example/

  • https://mkyong.com/logging/slf4j-logback-tutorial/

  • https://www.baeldung.com/logback

  • https://javabydeveloper.com/lombok-slf4j-examples/

  • https://projectlombok.org/features/log

  • http://www.slf4j.org/manual.html

  • https://www.javaguides.net/2019/03/project-lombok-logging-slf4j-annotation-example.html

  • https://projectlombok.org/setup/intellij

  • https://projectlombok.org/api/lombok/extern/slf4j/Slf4j.html

  • http://www.slf4j.org/

  • https://stackoverflow.com/questions/62431593/can-not-use-log-info-method-under-test-methods-while-using-slf4j/

Comments