can't we configure logs when using lombok?

Viewed 781

I have a clean maven project and using lombok with this dependencies in my pom:

Edit: here is a sample project with the configurations I'm listing so you can take a look at it.

        <!--region Lombok Configuration -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.8.0-beta2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <type>jar</type>
        </dependency>
        <!-- endregion -->

And 4 duplicated files placed in /src and /src/main/resources each:

  1. slf4j.properties=
  2. log4j.properties:
log4j.rootLogger=ERROR,stdout
log4j.logger.com.myorg=ERROR
log4j.logger.com.myorg.internal.myproj=ERROR

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
  1. simplelogger.properties
org.slf4j.simpleLogger.defaultLogLevel=ERROR
  1. logback.xml
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<root level="error">
    <appender-ref ref="STDOUT" />
</root>
</configuration>

I put all the logs in ERROR level so I will be able to just verify that the configuration files are being read, but when printing a single log for EVERY level, the console keeps printing the default level (INFO, WARN and ERROR)

I assume that the no configuration file is being read/detected.

Which configuration file should I be using, and where should I place it for it to identify it? Or maybe I'm confused and we are not able to configure logs with this configuration and I should be looking into another implementation of lombok? I'm kind of lost in here.

2 Answers

It's enough to put just a single logback.xml to your classpath (e.g. /src/main/resources). It's a doc page about how Logback lookups the configuration file. But for some libraries, you have to use bridges to redirect "old" loggers to Logback.

Related questions: common-logging, JUL

This is not a direct answer but it is a solution to the main question. Instead of using logback-classic dependency we can switch to a log4j-slf4j-impl implementation of slf4j and use the log4j2.properties file to configure levels and Format/Layout Pattern as shown in the sample repository to obtain this kind of logs:

enter image description here

Related