Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log particular classes to another log file in Spring Boot

I have simple log setup in application.properties:

logging.file = logs/debug.log
logging.level.org.hibernate.SQL = DEBUG
logging.level.org.hibernate.type = TRACE

I have a package co.myapp.notifier. I want all classes of this package to log to logs/notifier.log. I tried https://stackoverflow.com/a/9652239 and https://stackoverflow.com/a/728351 with no luck. In all cases the messages goes to my debug.log

like image 678
PaintedRed Avatar asked Aug 11 '15 12:08

PaintedRed


People also ask

Can you control logging with Spring boot?

Spring Boot's default configurations provides a support for the use of Java Util Logging, Log4j2, and Logback. Using these, we can configure the console logging as well as file logging. If you are using Spring Boot Starters, Logback will provide a good support for logging.

Which is the default logging file in Spring boot?

26.3 File Output By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a logging. file or logging. path property (for example, in your application.


1 Answers

If you need to do that, you will need your own logback.xml file.

<configuration>

<!-- Normal debug log appender -->
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>debug.log</file>

    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

<appender name="virtuallab" type="ch.qos.logback.core.rolling.RollingFileAppender">
   <file value="Logs/virtuallab.log"/>
   <appendToFile value="true"/>
   <maxSizeRollBackups value="5"/>
   <maximumFileSize value="5MB"/>
   <rollingStyle value="Size"/>
   <staticLogFileName value="true"/>
   <encoder>
     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
   </encoder>
</appender>

<!-- Setup the root category, add the appenders and set the default level -->
  <root level="debug">
    <appender-ref ref="FILE" />
  </root>

<!-- Specify the level specific to co.myapp.notifier -->
<logger name="co.myapp.notifier">
  <level value="ALL" />
  <appender-ref ref="virtuallab" />
</logger>

</configuration>

If you need a console log, you may need to add it as well. Here is the docs and read this question also.

like image 148
Maleen Abewardana Avatar answered Oct 09 '22 04:10

Maleen Abewardana