日志框架 logback SPI 机制 SPI 全称 Service Provider Interface,是 Java 提供的一套用来被第三方实现或者扩展的 API,它可以用来启用框架扩展和替换组件。他是一种服务发现机制。它通过在 ClassPath 路径下的 META-INF/services 文件夹查找文件,自动加载文件里所定义的类
1 2 3 4 public static <S> ServiceLoader<S> load (Class<S> service, ClassLoader loader) { return new ServiceLoader<>(service, loader); }
手动模拟 SPI 机制
1 2 3 4 5 public interface Driver { public void getConnection () ; }
1 2 3 4 5 6 public class MyDriver implements Driver { @Override public void getConnection () { System.out.println("SPI 机制" ); } }
META-INF.services.com.dream.driver.Driver
1 com.dream.mydriver.MyDriver
然后将改项目打包 加入 Logback 中
1 2 3 4 5 6 public static void main (String[] args) { ServiceLoader<Driver> load = ServiceLoader.load(Driver.class); load.forEach(Driver::getConnection); }
这个时候就会通过 SPI 机制读取到配置文件中的类实现
logback 编程式配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public class Myconfigurator extends ContextAwareBase implements Configurator { public Myconfigurator () { } @Override public void configure (LoggerContext lc) { addInfo("Setting up default configuration." ); ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>(); ca.setContext(lc); ca.setName("console" ); LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>(); encoder.setContext(lc); FileAppender<ILoggingEvent> fileAppender = new FileAppender<>(); fileAppender.setContext(lc); fileAppender.setName("file" ); fileAppender.setFile("D://logs/logback.log" ); PatternLayout patternLayout = new PatternLayout(); patternLayout.setContext(lc); patternLayout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" ); patternLayout.start(); fileAppender.setEncoder(encoder); fileAppender.start(); PatternLayout layout = new PatternLayout(); layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" ); layout.setContext(lc); layout.start(); encoder.setLayout(layout); ca.setEncoder(encoder); ca.start(); Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.addAppender(ca); rootLogger.addAppender(fileAppender); } }
1 2 3 4 5 6 7 8 9 10 private final static Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class); @Test public void testLogback () { LOGGER.error("error" ); LOGGER.warn("warn" ); LOGGER.info("info" ); LOGGER.debug("debug" ); LOGGER.trace("trace" ); }
三大组件
1、appender,输出源,一个日志可以后好几个输出源
2、encoder,一个 appender 有一个 encoder,负责将一个 event 事件转换成一组 byte 数组,并将转换后的字节数据输出到文件中。
Encoder 负责把事件转换为字节数组,并把字节数组写到合适的输出流。因此,encoder 可以控制在什么时候、把什么样的字节数组写入到其拥有者维护的输出流中。Encoder 接口有两个实现类,LayoutWrappingEncoder 与 PatternLayoutEncoder。 注意:在 logback 0.9.19 版之前没有 encoder。 在之前的版本里,多数 appender 依靠 layout 来把事件转换成字符串并用 java.io.Writer 把字符串输出。在之前的版本里,用户需要在 FileAppender 里嵌入一个 PatternLayout。
3、layout,格式化数据将 event 事件转化为字符串,解析的过程
4、filter 过滤器
占位符 %-5level
级别
%d{yyyy-MM-dd HH:mm:ss.SSS}
日期
%c
类的完整名称
%M
为 method
%L
为 行号
%thread
线程名称
%m
或者%msg
为 信息
%n
换行
logback 配置文件配置 1 2 3 4 5 6 7 8 9 10 11 <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-classic</artifactId > <version > 1.2.3</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > 1.7.30</version > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <configuration > <jmxConfigurator /> <appender name ="console" class ="ch.qos.logback.core.ConsoleAppender" > <layout class ="ch.qos.logback.classic.PatternLayout" > <Pattern > %date [%thread] %-5level %logger{25} - %msg%n</Pattern > </layout > </appender > <appender name ="file" class ="ch.qos.logback.core.FileAppender" > <file > D://logs/lobackconfig.log</file > <layout class ="ch.qos.logback.classic.PatternLayout" > <Pattern > %date [%thread] %-5level %logger{25} - %msg%n</Pattern > </layout > </appender > <logger name ="com.dream.xiaobo.LogbackTest" level ="warn" > <additive > false</additive > <appender-ref ref ="console" /> <appender-ref ref ="file" /> </logger > <root level ="debug" > <appender-ref ref ="console" /> </root > </configuration >
logback日志拆分与滚动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 <?xml version = "1.0" encoding = "UTF-8"?> <configuration > <property name ="pattern" value ="[ %-5level ] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n" /> <appender name ="console" class ="ch.qos.logback.core.ConsoleAppender" > <target > System.err</target > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${pattern}</pattern > </encoder > </appender > <property name ="log_dir" value ="D://logs" /> <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 > <maxHistory > 3</maxHistory > <maxFileSize > 1MB</maxFileSize > </rollingPolicy > </appender > <root lever ="ALL" > <appender-ref ref ="console" > </appender-ref > <appender-ref ref ="rollFile" > </appender-ref > </root > </configuration >
logback异步日志配置
1 2 3 4 AsyncAppender asyncAppender = new AsyncAppender(); asyncAppender.addAppender(ca); asyncAppender.addAppender(fileAppender);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 <?xml version = "1.0" encoding = "UTF-8"?> <configuration > <property name ="pattern" value ="[ %-5level ] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n" /> <appender name ="console" class ="ch.qos.logback.core.ConsoleAppender" > <target > System.err</target > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${pattern}</pattern > </encoder > </appender > <property name ="log_dir" value ="D://logs" /> <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 > <maxHistory > 3</maxHistory > <maxFileSize > 1MB</maxFileSize > </rollingPolicy > </appender > <appender name ="ASYNC" class ="ch.qos.logback.classic.AsyncAppender" > <appender-ref ref ="console" /> <appender-ref ref ="rollFile" /> </appender > <root lever ="ALL" > <appender-ref ref ="ASYNC" /> </root > </configuration >
logback-access的使用 logback-access模块与Servlet容器(如Tomcat和Jetty)集成,以提供HTTP访问日志功能。我们可以使 用logback-access模块来替换tomcat的访问日志
步骤
1、现将tomcat的config文件添加如下内容
1 <Valve className ="org.apache.catalina.valves.AccessLogValve" directory ="logs" prefix ="localhost_access_log." suffix =".txt" pattern ="common" resolveHosts ="false" />
catalina 日志
1 <Valve className ="ch.qos.logback.access.tomcat.LogbackValve" />
logback.access 日志
这两个二选一
参数介绍
className
想配置访问日志?这就必须得写成这样。
directory
这个东西是日志文件放置的目录,在tomcat下面有个logs文件夹,那里面是专门放置日志文件的,当然你也可以修改,我就给改成了D:\
prefix
这个是日志文件的名称前缀,我的日志名称为localhost_access_log.2007-09-22.txt,前面的前缀就是这个localhost_access_log
suffix
这就是后缀名啦,可以改成别的
pattern
这个是最主要的参数了,具体的咱们下面讲,这个参数的内容比较丰富
resolveHosts
如果这个值是true的话,tomcat会将这个服务器IP地址通过DNS转换为主机名,如果是false,就直接写服务器IP地址啦
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?xml version="1.0" encoding="UTF-8"?> <configuration > <statusListener class ="ch.qos.logback.core.status.OnConsoleStatusListener" /> <property name ="LOG_DIR" value ="${catalina.base}/logs" /> <appender name ="FILE" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${LOG_DIR}/access.log</file > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > access.%d{yyyy-MM-dd}.log.zip</fileNamePattern > </rollingPolicy > <encoder > <pattern > combined</pattern > </encoder > </appender > <appender-ref ref ="FILE" /> </configuration >
本篇文章 仅供自己学习整理 方便阅读 参考b站某UP主 如有侵权 通知即删
你知道的越多 你不知道的越多 嘿 我是小博 带你一起看我目之所及的世界……