Dheeraj Madan
Dheeraj Madan

Reputation: 23

How to print logs for 2 classes in 2 different files in spring boot

I have set logging.file=C:/usr/local/tomcat/logs/hib.log in application.properties and setting like below in class

private static final Logger logger = LogManager.getLogger(ChargeMasterController.class);

    logger.info("Total time taken for  is ---------------------------" + time  + " ms");

Logs are getting printed fine in the path mentioned as logging.file.

Now I want to print my logs in 2 different files for 2 different classes(In same package), how can I set 2 logging.file in application.properties

Upvotes: 2

Views: 6101

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44745

You can't do that with the configuration Spring provides. But since spring-boot-starter-logging uses Logback by default, you can use a Logback configuration in stead and define multiple appenders, for example:

<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
    <file>log1.log</file>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
</appender>
<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
    <file>log2.log</file>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
    </layout>
</appender>

After that, you could use the FILE1 appender for your first class, FILE2 appender for your second class, and STDOUT for all other classes. This can be done by defining the loggers:

<logger name="com.example.pkg.ClassName1" additivity="false" level="info">
    <appender-ref ref="FILE1" />
</logger>
<logger name="com.example.pkg.ClassName2" additivity="false" level="info">
    <appender-ref ref="FILE2" />
</logger>
<root level="INFO">
    <appender-ref ref="STDOUT" />
</root>

As you can see, there are two loggers next to the root logger, called com.example.pkg.ClassName1 and com.example.pkg.ClassName2. These should match the names of the classes that should log to separate files.

The additivity="false" is important to make sure that logs that go to log1.log or log2.log aren't also sent to the console since they would also match the root logger.

This configuration can be placed within a logback.xml file on your classpath (eg. src/main/resources). Alternatively, you can also configure the location by using the logging.config property within application.properties. For example:

logging.config=file:/path/to/logback.conf

Or if you want to use an arbitrary location on your classpath:

logging.config=classpath:my/logback.conf

Upvotes: 3

Related Questions