【Log】【Logback】【1】简单介绍
前言:
1,log4j和logback都是日志组件,且作者是同一人
2,官方网站: http://logback.qos.ch
正文:
1,引入jar包(Maven配置)
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <logback.version>1.1.7</logback.version> <slf4j.version>1.7.21</slf4j.version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies>
2,配置日志相关参数,logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--> <property name="LOG_HOME" value="/home" /> <!-- 控制台输出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <!-- 按照每天生成日志文件 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件输出的文件名--> <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern> <!--日志文件保留天数--> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> <!--日志文件最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!-- 日志输出级别 --> <root level="INFO"> <appender-ref ref="STDOUT" /> </root> </configuration>
3,java代码
public class App { private final static Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { logger.info("logback info"); logger.error("logback error"); logger.debug("logback debug"); } }
参考博客:
1,logback的使用和logback.xml详解
相关推荐
JavaWinner 2020-03-04
zhangxiaocc 2020-02-23
丨Fanny丨Cri 2020-02-01
sdaq 2020-01-10
wangruiling 2020-01-07
sdaq 2019-12-28
JavaWinner 2019-12-09
MrLiar 2016-05-15
SailingAptech 2019-09-05
狼烟烽火 2016-01-23
wanmeishijie 2016-05-15
lzz0 2019-07-08
LeeEmmmm 2019-07-01
zhangyou00 2019-06-30
sdaq 2016-08-04
SailingAptech 2019-06-30
zhangyayun0 2019-06-30
warmor 2015-12-14
SEG的命名空间 2015-02-11