【问题标题】:How to log particular classes to another log file in Spring Boot如何在 Spring Boot 中将特定类记录到另一个日志文件
【发布时间】:2015-08-11 12:06:23
【问题描述】:

我在 application.properties 中有简单的日志设置:

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

我有一个包裹co.myapp.notifier。我希望这个包的所有类都登录到logs/notifier.log。我试过 https://stackoverflow.com/a/9652239https://stackoverflow.com/a/728351 没有运气。 在所有情况下,消息都会发送到我的 debug.log

【问题讨论】:

  • 默认情况下,spring 使用 logback,因此 log4j 的解决方案显然不起作用。如果你想改变记录的地方然后添加你自己的logback.xml文件来配置logback,你不能使用Spring Boot中的基本配置支持来做到这一点。

标签: java logging spring-boot


【解决方案1】:

如果您需要这样做,您将需要自己的 logback.xml 文件。

<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>

如果您需要控制台日志,您可能还需要添加它。 Here 是文档,也请阅读此 question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多