通过配置logback将不同的日志写入不同的日志文件中

简单的需求:通过配置logback将不同的日志写入不同的日志文件中

logback配置:

xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <FileNamePattern>./logs/ask-consumer.log.%d{yyyy-MM-dd}FileNamePattern>
            
            <MaxHistory>30MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
    appender>
    <appender name="daGuanThread_1"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <FileNamePattern>./logs/1/daGuanThread_1.log.%d{yyyy-MM-dd}FileNamePattern>
            
            <MaxHistory>30MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
    appender>
    <appender name="daGuanThread_2"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <FileNamePattern>./logs/2/daGuanThread_2.log.%d{yyyy-MM-dd}FileNamePattern>
            
            <MaxHistory>30MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MBMaxFileSize>
        triggeringPolicy>
    appender>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
    appender>
    
    <logger name="org.hibernate" level="ERROR" />
    <logger name="com.ibatis" level="DEBUG" />
    <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" />
    <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" />
    <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" />
    <logger name="java.sql.Connection" level="DEBUG" />
    <logger name="java.sql.Statement" level="DEBUG" />
    <logger name="java.sql.PreparedStatement" level="DEBUG" />
    
    <root level="@logging.level.root@">
        <appender-ref ref="FILE" />
        <appender-ref ref="console" />
    root>
    <logger name="daGuanThread_1" level="INFO" additivity="false">
        <appender-ref ref="daGuanThread_1">appender-ref>
    logger>
    <logger name="daGuanThread_2" level="INFO" additivity="false">
        <appender-ref ref="daGuanThread_2">appender-ref>
    logger>
configuration>

java代码:
private final static Logger logger = LoggerFactory.getLogger(DaGuanAskTopic.class);
private final static Logger daGuanThread_1 = LoggerFactory.getLogger("daGuanThread_1");
private final static Logger daGuanThread_2 = LoggerFactory.getLogger("daGuanThread_2");
 
  
使用:
this.writeLogger(loggerLevel,"帖子: "+i+" 请求失败:"+e.getMessage(),false);
 
  
this.writeLogger(loggerLevel,"帖子id"+i+" 插入成功=====================",true);

 
  
/**
 * 记录日志
 * @param loggerLevel
 * @param loggerInfo
 */
public void writeLogger(int loggerLevel, String loggerInfo,boolean isInfo) {
    if (loggerLevel == 1) {
        if (isInfo) {
            daGuanThread_1.info(loggerInfo);
        } else {
            daGuanThread_1.error(loggerInfo);
        }
    } else if (loggerLevel == 2) {
        if (isInfo) {
            daGuanThread_2.info(loggerInfo);
        } else {
            daGuanThread_2.error(loggerInfo);
        }
    }
}
效果图:
通过配置logback将不同的日志写入不同的日志文件中_第1张图片
 
  
 
  

你可能感兴趣的:(通过配置logback将不同的日志写入不同的日志文件中)