Logback使用

Logback使用

如何使用

使用流程

  1. maven项目中引用logback依赖
    例如:

    1
    2
    3
    4
    5
    6
    使用时具体版本,可以到网上查看
    <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
    </dependency>
  2. java代码中使用
    例如:

    1
    2
    3
    4
    5
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    // 当写下面这句给类 MonitorTaskInfo.class 创建logger的java代码时,会自动引入上面两个相关的包,
    // 但是写的时候LoggerFactory无法自动提示getLogger的方法,这个我很疑惑,不知道具体原因
    private static final Logger logger = LoggerFactory.getLogger(MonitorTaskInfo.class);
  3. 在项目的classpath中的目录下添加一个logback.xml的配置文件,配置日志的具体打印方式,日志文件路径等内容

  4. maven中如何引用相关类?

Logback-classic module requires the presence of slf4j-api.jar and logback-core.jar in addition to logback-classic.jar on the classpath.
- 官方文档说,如果向使用Logback-classic模块,classpath中,除了需要logback-classic.jar外,还需要引入 slf4j-api.jar和 logback-core.jar
- 其中logback-*.jar 文件是logback发行版的一部分,而slf4j-api.jar是SLF4J项目中的
(不过,在实际使用中发现,maven项目中仅引入logback-class.jar的时候,会自动将logback-core.jar和slf4j-api.jar两个包也引入到项目中,猜测可能是自动引入依赖的原因)

将修改后的logback.xml文件导出来放到gitee上,作为之后使用的一个模板文件使用
- 写上使用的要求和配置的方法

http://logback.qos.ch/manual/introduction.html
更多logback项目的使用说明,请参考上面的官方网站,(甚至可以学习一下该项目的源码,有很多值得学习的地方)

  1. logback.xml 文件配置模板,参考目录下的同名文件即可,更详细内容,请参考官网

这是为什么topic-process的压缩代码日志一直会打印logback内部信息的原因,

我的logback.xml文件有问题

If any of these two conditions is not fulfilled, Joran cannot interpret the debug attribute since the configuration file cannot be read. If the configuration file is found but is malformed, then logback will detect the error condition and automatically print its internal status on the console.

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2023 ligongzhao
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信