发布于2020-11-20 09:42 阅读(921) 评论(0) 点赞(6) 收藏(0)
简介
Logback旨在作为流行的log4j项目的后继者,从log4j离开的地方接手。
Logback的体系结构足够通用,可以应用在不同的情况下。目前,logback分为三个模块:logback-core,logback-classic和logback-access。
logback-core模块为其他两个模块奠定了基础。logback-classic模块可以与log4j的显着改进版本同化。此外,经典的logback原生实现了SLF4J API,因此您可以轻松地在logback和其他日志框架(例如log4j或java.util.logging(JUL))之间来回切换。
logback-access模块与Servlet容器(例如Tomcat和Jetty)集成,以提供HTTP访问日志功能。请注意,您可以轻松地在logback-core之上构建自己的模块。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>log-study</artifactId>
<groupId>com.lxcourse</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>logback-demo</artifactId>
<dependencies>
<!-- slf4j日志门面 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
</dependency>
<!-- logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.lxcourse.logback;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackDemo {
public static final Logger LOGGER = LoggerFactory.getLogger(LogbackDemo.class);
@Test
public void testQuick(){
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info");
LOGGER.debug("debug");
LOGGER.trace("trace");
}
}
输出:
14:16:51.106 [main] ERROR com.lxcourse.logback.LogbackDemo - error
14:16:51.109 [main] WARN com.lxcourse.logback.LogbackDemo - warn
14:16:51.109 [main] INFO com.lxcourse.logback.LogbackDemo - info
14:16:51.109 [main] DEBUG com.lxcourse.logback.LogbackDemo - debug
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
</root>
</configuration>
具体配置参考官网教程
执行LogbackDemo.testQuick(),输出如下:
[ERROR] 2020-05-29 14:59:38.233 com.lxcourse.logback.LogbackDemo testQuick [main] error
[WARN ] 2020-05-29 14:59:38.236 com.lxcourse.logback.LogbackDemo testQuick [main] warn
[INFO ] 2020-05-29 14:59:38.237 com.lxcourse.logback.LogbackDemo testQuick [main] info
[DEBUG] 2020-05-29 14:59:38.238 com.lxcourse.logback.LogbackDemo testQuick [main] debug
[TRACE] 2020-05-29 14:59:38.238 com.lxcourse.logback.LogbackDemo testQuick [main] trace
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<property name="log_dir" value="/Users/doubledumbao/logs"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!--将日志输出到文件-->
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
<appender-ref ref="file" />
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<property name="log_dir" value="/Users/doubledumbao/logs"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!--将日志输出到文件-->
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!-- 将日志输出为html格式-->
<appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.html</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
</layout>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
<appender-ref ref="file" />
<appender-ref ref="htmlFile" />
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<property name="log_dir" value="/Users/doubledumbao/logs"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!--将日志输出到文件-->
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!-- 将日志输出为html格式-->
<appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.html</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
</layout>
</encoder>
</appender>
<!-- 日志拆分和归档压缩的appender对象 -->
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}/roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
<maxFileSize>1MB</maxFileSize>
</rollingPolicy>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
<appender-ref ref="rollFile" />
</root>
</configuration>
@Test
public void testRollingFile(){
for (int i = 0; i < 10000; i++) {
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info");
LOGGER.debug("debug");
LOGGER.trace("trace");
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!--将日志输出到文件-->
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!-- 将日志输出为html格式-->
<appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.html</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
</layout>
</encoder>
</appender>
<!-- 日志拆分和归档压缩的appender对象 -->
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}/roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
<maxFileSize>1MB</maxFileSize>
</rollingPolicy>
<!-- 日志级别过滤器 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 日志过滤规则 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
<appender-ref ref="rollFile" />
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!--将日志输出到文件-->
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!-- 将日志输出为html格式-->
<appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.html</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
</layout>
</encoder>
</appender>
<!-- 日志拆分和归档压缩的appender对象 -->
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}/roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
<maxFileSize>1MB</maxFileSize>
</rollingPolicy>
<!-- 日志级别过滤器 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 日志过滤规则 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 异步日志 -->
<appender name="async" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="rollFile"/>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
<appender-ref ref="async" />
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>
<property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!--将日志输出到文件-->
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<!-- 将日志输出为html格式-->
<appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}/logback.html</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
</layout>
</encoder>
</appender>
<!-- 日志拆分和归档压缩的appender对象 -->
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}/roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
<maxFileSize>1MB</maxFileSize>
</rollingPolicy>
<!-- 日志级别过滤器 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 日志过滤规则 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 异步日志 -->
<appender name="async" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="rollFile"/>
</appender>
<!--root Logger-->
<root level="ALL">
<appender-ref ref="STDOUT"/>
<appender-ref ref="async"/>
</root>
<!-- 自定义logger对象 additivity="false" 自定义logger对象是否继承rootLogger -->
<logger name="com.lxcourse" level="info" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
原文链接:https://www.cnblogs.com/zhaoran8775/p/12990760.html
作者:java小王子
链接:http://www.javaheidong.com/blog/article/4994/ea17d1ada380c0679a19/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!