Using slf4j with logback
程序员文章站
2022-07-03 16:23:12
...
slf4j是一款非常不错的关于log的开源框架,logback是依托于log4j的api,但是它提供了更多的属性,不管是在性能、内存损耗上,都有显著提高,并且它能够提供自动加载、过滤以及其它的属性特征。
第一步,建立pom.xml
<properties>
<slf4j.version>1.6.4</slf4j.version>
<logback.version>1.0.1</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
第二步,创建配置文件,Logback支持两种方式,传统方式是xml以及Groovy DSL style,命名指定为logback.xml放到classpath
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.lordofthejars.foo" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<!-- Strictly speaking, the level attribute is not necessary since -->
<!-- the level of the root level is set to DEBUG by default. -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
通过上述的配置文件,一目了然能够看明白,它的日志级别,以及需要追加的日志的目录,如果在根目录中已经打印log,而子目录也被授予print,那么可以通过追加属性这是false,它只会打印一次。
第三步:建立一个类,介绍下如何使用
public class BarComponent {
private static final Logger logger = LoggerFactory.getLogger(BarComponent.class);
public void bar() {
String name = "lordofthejars";
logger.info("Hello from Bar.");
logger.debug("In bar my name is {}.", name);
}
}
它会根据{}这一对,用name参数进行替代,是不是比较好用,好像有点C的语法。
如果你对于groovy属性,同样也可以采用DSL Style方式
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
<scope>runtime</scope>
</dependency>
类同于刚才的logback.xml的语法
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import static ch.qos.logback.classic.Level.DEBUG
import static ch.qos.logback.classic.Level.INFO
appender("STDOUT", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} Groovy - %msg%n"
}
}
logger("com.lordofthejars.foo", INFO)
root(DEBUG, ["STDOUT"])
结论:
logback比logg4j好用,性能指标也比较高,值得推荐的一款日志的框架。
下一篇: 浏览器滚动条默认样式修改
推荐阅读
-
MySQL错误:using expire_logs_days without log_bin crashes the s
-
优化-mysql使用roll up后出现Using temporary,Using filesort
-
log4 slf4j使用(一)
-
Using Go with MariaDB_MySQL
-
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
-
linux安装数据时报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo.
-
Classify your own images using Amazon SageMaker
-
SRAGN: Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
-
MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEM
-
@Slf4j的使用