How to log particular classes to another log file

2019-04-30 06:55发布

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

1条回答
趁早两清
2楼-- · 2019-04-30 07:47

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.

查看更多
登录 后发表回答