如何在Spring Boot中将特定类记录到另一个日志文件中 [英] How to log particular classes to another log file in Spring Boot

查看:223
本文介绍了如何在Spring Boot中将特定类记录到另一个日志文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一个包 co.myapp.notifier 。我希望这个包的所有类都登录到 logs / notifier.log 。我试过
https://stackoverflow.com/a/9652239

https://stackoverflow.com/a/728351
没有运气。
在所有情况下,消息都会发送到我的debug.log

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

推荐答案

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

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.

这篇关于如何在Spring Boot中将特定类记录到另一个日志文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆