• log4j2 自定义配置文件,java载入


    http://logging.apache.org/log4j/2.x/faq.html#separate_log_files

    How do I reconfigure log4j2 in code with a specific configuration file?

    See the below example. Be aware that this LoggerContext class is not part of the public API so your code may break with any minor release.

    1. // import org.apache.logging.log4j.core.LoggerContext;
    2.  
    3. LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
    4. File file = new File("path/to/a/different/log4j2.xml");
    5.  
    6. // this will force a reconfiguration
    7. context.setConfigLocation(file.toURI());
    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration> 
         status="debug" monitorInterval="1800"
         <properties>  
            <property name="LOG_HOME">E://stats_collector_logs</property>  
            <!-- 日志备份目录 -->  
            <property name="BACKUP_HOME">{LOG_HOME}/backup</property>  
            <property name="STAT_NAME">log</property> 
        </properties>  
         
        <Appenders>  
            <Console name="Console" target="SYSTEM_OUT">  
               <PatternLayout pattern="%m%n"/>
            </Console>  
            <File name="MyFile" fileName="/stats">  
                 <PatternLayout pattern="- %msg%n" />
            </File>  
            
             <!-- STB发来的播放日志 -->  
            <RollingRandomAccessFile name="playLogAppender"  
                fileName="${LOG_HOME}/play_${STAT_NAME}_"  
                filePattern="${LOG_HOME}/play_${STAT_NAME}_%d{yyyy_MM_dd_HH_mm}.log">  
                <PatternLayout  
                    pattern="%msg%n" />  
                <Policies>  
                    <TimeBasedTriggeringPolicy interval="1"  
                        modulate="true" />  
                </Policies>  
            </RollingRandomAccessFile>  
    
             <!-- STB发来的播放日志 -->  
            <RollingRandomAccessFile name="viewLogAppender"  
                fileName="${LOG_HOME}/view_${STAT_NAME}_"  
                filePattern="${LOG_HOME}/view_${STAT_NAME}_%d{yyyy_MM_dd_HH_mm}.log">  
                <PatternLayout  
                    pattern="%msg%n" />  
                <Policies>  
                    <TimeBasedTriggeringPolicy interval="1"  
                        modulate="true" />  
                </Policies>  
            </RollingRandomAccessFile>  
            
       <!--interval,integer型,指定两次封存动作之间的时间间隔。单位:以日志的命名精度来确定单位,比如yyyy-MM-dd-HH 单位为小时,yyyy-MM-dd-HH-mm 单位为分钟-->
       <!--modulate,boolean型,说明是否对封存时间进行调制。若modulate=true,则封存时间将以0点为边界进行偏移计算。比如,modulate=true,interval=4hours,那么假设上次封存日志的时间为03:00,则下次封存日志的时间为04:00,之后的封存时间依次为08:00,12:00,16:00,。。。-->
          <!-- 程序员调试日志 -->  
            <RollingRandomAccessFile name="DevLog" fileName="${LOG_HOME}/${SERVER_NAME}"  
                filePattern="${LOG_HOME}/${SERVER_NAME}.%d{yyyy-MM-dd-HH}.log">  
                <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />  
                <Policies>  
                    <TimeBasedTriggeringPolicy interval="1" modulate="true" />  
                </Policies>  
            </RollingRandomAccessFile>  
            <!-- 游戏产品数据分析日志 -->  
            <RollingRandomAccessFile name="ProductLog"  
                fileName="${LOG_HOME}/${SERVER_NAME}_${STAT_NAME}"  
                filePattern="${LOG_HOME}/${SERVER_NAME}_${STAT_NAME}.%d{yyyy-MM-dd-HH}.log">  
                <PatternLayout  
                    pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />  
                <Policies>  
                    <TimeBasedTriggeringPolicy interval="1"  
                        modulate="true" />  
                </Policies>  
            </RollingRandomAccessFile>  
            
             <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
             <File name="log" fileName="log/test.log" append="false">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
            </File>
            
            <!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
            <RollingFile name="RollingFile" fileName="logs/app.log"
                         filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
                <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
                <SizeBasedTriggeringPolicy size="50MB"/>
            </RollingFile>
            
              <File name="ERROR" fileName="logs/error.log">
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
                <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            </File>
            
        </Appenders>  
        <Loggers>  
            <Logger name="PLAY_LOG" level="trace" additivity="false">  
                <AppenderRef ref="playLogAppender" />  
            </Logger>  
            <Logger name="VIEW_LOG" level="trace" additivity="false">  
                <AppenderRef ref="viewLogAppender" />  
            </Logger>  
           <Root level="debug">
                        <AppenderRef ref="Console"/>
                </Root>
        </Loggers>  
    </Configuration>  
  • 相关阅读:
    JavaWeb_(Jar)使用fastjson解析json和序列化对象
    JavaWeb_JSTL标签数据的存储
    JavaWeb_EL表达式存储数据及获得项目路径
    JavaWeb_(MVC)管理员后台商品查询demo
    JavaWeb_(设计模式)单例模式
    JavaWeb_客户端相对/绝对路径和服务器端路径
    JavaWeb_Servlet生命周期
    控制导航条最后一个标签样式的三种方法
    JS跳转到顶部的方法
    数学对象Math ceil()、floor()、round()方法
  • 原文地址:https://www.cnblogs.com/bigben0123/p/6434671.html
Copyright © 2020-2023  润新知