Search code examples
javalogginglogback

java logback custom logger name


I want to create the following logback configuration:

<configuration>
    <appender name="INCOMING_REQUESTS_LOG" class="ch.qos.logback.core.FileAppender">
        <file>./logs/incoming_requests.log</file>
        <append>true</append>
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%date %-4relative [%thread] %-5level %logger{35} | %msg%n</pattern>
        </encoder>
    </appender>



    <logger name="mysuperduperlogger" level="DEBUG" additivity="false">
            <appender-ref ref="INCOMING_REQUESTS_LOG"/>
    </logger>
</configuration>

Can I use

logger name="mysuperduperlogger"

as logback logger name and how to access it in my code? In documentation I could found only loggers like "com.mypackage.myclass.aa1"


Solution

  • When you create a instance of Logger you can do so in two ways.

    1. By Class
    2. By String

    If you use by Class like

    private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);
    

    and the class name including the package is org.example.Test then to configure logs for this logger the name can be org.example.Test

    If you use by String like

    private static final Logger LOGGER = LoggerFactory.getLogger("TestLogger");
    

    then to configure logs for this logger the name can be TestLogger

    So if you want to use mysuperduperlogger as logger name you have to create a Logger instance as follows.

    private static final Logger LOGGER = LoggerFactory.getLogger("mysuperduperlogger");