DEV Community

Gaurav Singh
Gaurav Singh

Posted on • Originally published at automationhacks.io on

How to do logging integration with logback and testng in report portal

Image description

Image Attribution: Report portal logo,logback logo,Gradle logo - wikipedia

Hi there, friend 👋,

Report portal is a quite unique open-source reporting solution available and seamlessly integrateswith the test runner of youR choice

I’ve been using Report portal in my current company and have quite recently started looking into itsfeatures in more detail.

If you new to Report portal and want to get started with setting up a basic instanceon docker and play around then you might find my earlier post on this topic useful

Check out: How to setup ReportPortal on a local dockerinstance

Once you have setup Report portal, and have started pushing some test runs into it, you would wantto get more detail about the test execution. While report portal will display the stack trace for afailed assertion, it won’t really give you all the console logs of the sequence of action thathappened above

As a simple example, let’s say an API request fails wit a 500 and you have written an assertion onthe response code, while Report portal will let you know about the mismatch, it won’t give you theAPI request ,etc even though that might have been printed into the console with a TestNGReporter.log() message

So how do we push logs into Report portal? Let’s see how to set this up.

Setup logging using logback

I write my tests in Kotlin and run them using Gradle/TestNG. However, the process should be verysimilar if you are on the JVM stack (Java etc)

On a high level, we need to do 4 steps

  1. Add desired logger dependency into gradle
  2. Add listener in your tests job in gradle
  3. Add .xml properties file with the ReportPortalAppender specified
  4. Add the class level logger and then write some messages to it.

Add Maven/Gradle dependencies

We will use logback as the logger of choicehowever there is support even for log4j ifyou prefer that.

Add below dependencies in your build.gradle file

compile group: 'com.epam.reportportal', name: 'agent-java-testng', version: '5.0.7'
compile group: 'com.epam.reportportal', name: 'logger-java-logback', version: '5.0.3'
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

Enter fullscreen mode Exit fullscreen mode

Add Report portal listener to you tests job

While you must have added this (if you followed my last post), ensure that report portal listener isadded in your test gradle job, Below is an example, where we have runTests gradle test task,where we have added the listener classes path

task runTests(type: Test) {
    useTestNG {
        testLogging.showSt)ndardStreams = true
        useDefaultListeners = false
        listeners << 'com.epam.reportportal.testng.ReportPortalTestNGListener'
        listeners << 'listeners.ExtentReporterNG'
        includeGroups System.getProperty('tag', 'NONE')
    }
}

Enter fullscreen mode Exit fullscreen mode

Add a logging configuration file

Logging frameworks would either come with a configuration XML file or a properties file to allow youto configure them. Since we are using logback, we need to use add a logback.xml file anywhere onour classpath

Logback basically has different destinations that it could write logs to called appenders

Here is how a sample configuration file would look like:

This has 3 appenders defined

  • STDOUT which uses ConsoleAppender from logback to write any logs to the command line
  • (Optional) FILE which uses RollingFileAppender from logback to write logs to a physical file andthen rotates these in a daily basis so that every new day, you would get a new log file while alsokeeping a history of max 30 days
  • ReportPortalAppender which pushes logs to the Report portal

To understand how logback works in detail, refer to this excellent guide onBaeldung

Add some loggers

The last piece of the puzzle is to add some loggers into your source code. Let’s assume you want towrite some logs in a test class, here is a basic Class and function in Kotlin to demonstrate that

@Test(groups = [TestGroups.ALL])
class LogbackTests {
    private val logger: Logger = LoggerFactory.getLogger(this::class.java)

    // This test can be flaky and non deterministic
    // Done on purpose to see variation in report portal history
    @Test
    fun simpleLoggingTest() {
        logger.info("Example log from ${this::class.java.simpleName}")
        val random = ThreadLocalRandom.current().nextInt(30000)
        val limit = 20000

        logMessage(random, limit)
        Assert.assertTrue(random > limit, "No was greater than $limit")
    }

    private fun logMessage(random: Int, limit: Int) {
        if (random > limit) {
            logger.info("$random is > $limit")
        } else {
            logger.info("$random is < $limit")
        }
    }

Enter fullscreen mode Exit fullscreen mode

Basically we want to add static final logger as a class member

private val logger: Logger = LoggerFactory.getLogger(this::class.java)

Enter fullscreen mode Exit fullscreen mode

And then use any of the available methods to log at different log levels

Below are the available log levels that you can use

@Test
fun differentLogLevelsTest() {
    logger.trace("OMG trace level detail")
    logger.debug("Minute level detail")
    logger.info("This is just an info")
    logger.warn("Something bad happened")
    logger.error("Something catastropic happened")
}

Enter fullscreen mode Exit fullscreen mode

Once you run this test you would see logs show up in report portal under the log message section,you can also filter between the level of detail that you need for your logs using the slider controlgiven

Report portal launches Report portal logs

Summary

In this post, we learned how can we enhance our reporting in Report portal by adding logback as alogging framework of choice.

If you found this post useful, Do share it with a friend or colleague. Until next time. HappyTesting/Coding ☮️

References

Here are some further links that you can refer to:

Oldest comments (0)