• Logback动态生成Log文件


    1.logback.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration  scan="true" scanPeriod="60 seconds" debug="false">
        <property name="APP_NAME" value="xiabu" />
        <contextName>${APP_NAME}</contextName>
        <property name="LOG_PATH" value="/choice/logs/${APP_NAME}"/>
        <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } [%t] [%-40.40logger{39}:%line] %m%n"/>
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>${LOG_PATTERN}</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
        <appender name="LOG-FILE" class="ch.qos.logback.classic.sift.SiftingAppender">
            <discriminator class="com.choice.pr.commons.LoggerNameDisriminator"/>
            <sift>
                <appender name="LOG-FILE-${LOG_NAME}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                        <pattern>${LOG_PATTERN}</pattern>
                        <charset>UTF-8</charset>
                    </encoder>
                    <filter class="ch.qos.logback.classic.filter.LevelFilter">
                        <level>INFO</level>
                        <onMatch>ACCEPT</onMatch>
                        <onMismatch>DENY</onMismatch>
                    </filter>
                    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                        <FileNamePattern>${LOG_PATH}/${APP_NAME}-${LOG_NAME}-info-%d{yyyy-MM-dd}.log</FileNamePattern>
                        <MaxHistory>15</MaxHistory>
                    </rollingPolicy>
                </appender>
            </sift>
        </appender>
        <!--FILE-->
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>${LOG_PATTERN}</pattern>
                <charset>UTF-8</charset>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOG_PATH}/${APP_NAME}-info-%d{yyyy-MM-dd}.log</FileNamePattern>
                <MaxHistory>15</MaxHistory>
            </rollingPolicy>
        </appender>
    
        <!--FILE ERROR-->
        <appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>${LOG_PATTERN}</pattern>
                <charset>UTF-8</charset>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOG_PATH}/${APP_NAME}-error-%d{yyyy-MM-dd}.log</FileNamePattern>
                <MaxHistory>15</MaxHistory>
            </rollingPolicy>
        </appender>
    
        <logger name="org.springframework" level="ERROR"/>
        <logger name="com.choice.pr.commons" level="DEBUG"/>
    
        <logger name="alipay" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="yzpay" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="yzmessage" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="business" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="json" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="database" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="tenPay" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="tenPayRefund" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="mqSendOrder" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="callwebservice" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="rocketmq" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="oauth2" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="ar" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="dg" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="codepay" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="payNotify" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="mealSocket" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="alipay_refund" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="wx_refund" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="refund" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="openoauth2" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <logger name="mqtt" level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="LOG-FILE"/>
        </logger>
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="FILE"/>
            <appender-ref ref="FILE-ERROR"/>
        </root>
    </configuration>

    2.LoggerNameDisriminator

    package com.choice.pr.commons;
    
    import ch.qos.logback.classic.spi.ILoggingEvent;
    import ch.qos.logback.core.sift.AbstractDiscriminator;
    /**
     * 独立日志文件实现
     * 在logback.xml中引用
     */
    public class LoggerNameDisriminator extends AbstractDiscriminator<ILoggingEvent> {
    
        @Override
        public String getDiscriminatingValue(ILoggingEvent iLoggingEvent) {
            return iLoggingEvent.getLoggerName();
        }
    
        @Override
        public String getKey() {
            return "LOG_NAME";
        }
    }

    3.log4j 向logback转换,依赖

    <!-- 日志文件管理包 -->  
            <!-- log start -->  
            <dependency>  
                <groupId>log4j</groupId>  
                <artifactId>log4j</artifactId>  
                <version>${log4j.version}</version>  
            </dependency>
            <!-- logback -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>log4j-over-slf4j</artifactId>
                <version>1.7.12</version>
            </dependency>

    4.logUtil

    public static void writeToTxt(String logtype, String content) {
            LoggerFactory.getLogger(logtype).info(content);
        }
  • 相关阅读:
    python学习第十五天
    python学习第十三、十四天
    python学习第十二天
    python学习第j十一天
    python学习第十天
    ViewController push的自定义动画
    iOS 判断设备是否越狱
    iOS
    OBJC字面量
    ios8 share Extension 分享扩展
  • 原文地址:https://www.cnblogs.com/czwangzheng/p/8780941.html
Copyright © 2020-2023  润新知