首页 > 学院 > 开发设计 > 正文

logback 日志分离技术

2019-11-10 17:57:47
字体:
来源:转载
供稿:网友

依赖jar包

<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> </dependency>

logback.xml 日志文件

<?xml version="1.0"?><configuration> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n </pattern> </encoder> </appender> <appender name="test" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>c:/data/log/test.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>c:/data/log/test-%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>1024MB</maxFileSize> </triggeringPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%-20(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>c:/data/log/info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>c:/data/log/info-%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%msg%n</pattern> </encoder> </appender> <!-- 定义的日志基本打印位置,可以定义 日志基本level,如果定义 就打印响应的日志基本,不定义就是所有--> <logger name="monitorLog" additivity="false"> <appender-ref ref="info" /> <appender-ref ref="console" /> </logger> <!-- 没有定义的日志名称,的打印日志的级别 --> <root level="INFO"> <appender-ref ref="test" /> <appender-ref ref="console" /> </root></configuration>

测试代码

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import ch.qos.logback.classic.LoggerContext;import ch.qos.logback.core.util.StatusPRinter;public class TestLog { private static Logger log = LoggerFactory.getLogger(TestLog.class); private static Logger monitorLog = LoggerFactory.getLogger("monitorLog"); public static void main(String[] args) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); StatusPrinter.print( lc ); log.error("zhong1"); monitorLog.info("zhong"); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表