Groovy web console

subscribe to the feed Subscribe
to this
site

LOG4J2-3017-repro

Published 2 months ago by vy with tags log4j
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.junit.jupiter.api.Test

class Log4jGroovyTest {

    private static final Logger LOGGER = LogManager.getLogger(Log4jGroovyTest.class)

    @Test
    void test_simple() {
        int number = 1
        String gstringMessage = "number: ${number}"
        LOGGER.info(gstringMessage)
    }

    @Test
    void test_speed() {
        int number = 1
        String gstringMessage = "number: ${number}"
        long gstringDurationNanos = benchmarkNanos(() -> {
            LOGGER.info(gstringMessage)
        })
        String stringMessage = "number: " + number
        long stringDurationNanos = benchmarkNanos(() -> {
            LOGGER.info(stringMessage)
        })
        LOGGER.warn("gstringDurationNanos: ${gstringDurationNanos}")
        LOGGER.warn("stringDurationNanos: ${stringDurationNanos}")
    }

    // gstringDurationNanos: 151582040
    // stringDurationNanos:   28581232

    private static long benchmarkNanos(Runnable body) {
        long startInstantNanos = System.nanoTime();
        for (int i = 0; i < 1_000; i++) {
            body.run()
        }
        return System.nanoTime() - startInstantNanos
    }

}