Log4j2实现不同线程不同级别日志输出到不同的文件中

Log4j2实现不同线程不同级别日志输出到不同的文件中

log4j2.xml


<configuration status="OFF">
    <appenders>
 <Routing name="Routing">
            <Routes pattern="$${ctx:ROUTINGKEY}">
                
                <Route key="special">
                    <RollingFile name="Rolling-${ctx:ROUTINGKEY}" fileName="logs/special-${ctx:ROUTINGKEY}.log"
                                 filePattern="./logs/${date:yyyy-MM}/${ctx:ROUTINGKEY}-special-%d{yyyy-MM-dd}-%i.log.gz">
                        <PatternLayout>
                            <Pattern>%d{ISO8601} [%t] %p %c{3} - %m%nPattern>
                        PatternLayout>
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                            <SizeBasedTriggeringPolicy size="10 MB"/>
                        Policies>
                    RollingFile>
                Route>
                
                <Route key="$${ctx:ROUTINGKEY}">
                    <RollingFile name="Rolling-default" fileName="logs/default.log"
                                 filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
                        <PatternLayout>
                            <pattern>%d{ISO8601} [%t] %p %c{3} - %m%npattern>
                        PatternLayout>
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                            <SizeBasedTriggeringPolicy size="10 MB"/>
                        Policies>
                    RollingFile>
                Route>
                
                <Route>
                    <RollingFile name="Rolling-${ctx:ROUTINGKEY}" fileName="logs/other-${ctx:ROUTINGKEY}.log"
                                 filePattern="./logs/${date:yyyy-MM}/${ctx:ROUTINGKEY}-other-%d{yyyy-MM-dd}-%i.log.gz" append = "false">
                        <PatternLayout>
                            <pattern>%d{ISO8601} [%t] %p %c{3} - %m%npattern>
                        PatternLayout>
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                            <SizeBasedTriggeringPolicy size="10 MB"/>
                        Policies>
                    RollingFile>
                Route>
            Routes>
        Routing>
     

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        Console>
         <File name="ERRORFile" fileName="logs/error.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} [%t] %-5level %class{36} %L %M - %msg%xEx%n"/>
        File>
        
    appenders>
    <loggers>
        
        
        <logger name="common" level="trace" additivity="false">
            <appender-ref ref="Console"/>
         
             <appenderRef ref="Routing"/>
        logger>
        <root level="error">
            <AppenderRef ref="Console"/>
            <appenderRef ref="Routing"/>
        root>
    loggers>
configuration>
package common;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Hello {

    static final Logger logger = LogManager.getLogger(Hello.class.getName());

    public boolean hello() {
        logger.trace("enter");   //trace级别的信息,单独列出来是希望你在某个方法或者程序逻辑开始的时候调用,和logger.trace("entry")基本一个意思
        logger.error("Did it again!");   //error级别的信息,参数就是你输出的信息
        logger.info("我是info信息");    //info级别的信息
        logger.debug("我是debug信息");
        logger.warn("我是warn信息");
        logger.fatal("我是fatal信息");
        logger.log(Level.DEBUG, "我是debug信息");   //这个就是制定Level类型的调用:谁闲着没事调用这个,也不一定哦!
        logger.trace("exit");    //和entry()对应的结束方法,和logger.trace("exit");一个意思
        return false;
    }

    public static void main(String[] args) {
        Hello h = new Hello();
        h.hello();

    }
} 
package common;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class Hello2 {

      static final  Logger logger = LogManager.getLogger(Hello2.class.getName());

        public boolean hello2() {
            logger.trace("enter");   //trace级别的信息,单独列出来是希望你在某个方法或者程序逻辑开始的时候调用,和logger.trace("entry")基本一个意思
            logger.error("Did it again!");   //error级别的信息,参数就是你输出的信息
            logger.info("我是info信息");    //info级别的信息
            logger.debug("我是debug信息");
            logger.warn("我是warn信息");
            logger.fatal("我是fatal信息");
            logger.log(Level.DEBUG, "我是debug信息");   //这个就是制定Level类型的调用:谁闲着没事调用这个,也不一定哦!
            logger.trace("exit");    //和entry()对应的结束方法,和logger.trace("exit");一个意思
            return false;
            }

}
package service;



import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.layout.PatternLayout;


import common.Hello;
import common.Hello2;

public class ThreadLog implements Runnable {



    public void run() {
        ThreadContext.put("ROUTINGKEY", Thread.currentThread().getName());;
        Hello h = new Hello();
        Hello2 h2 = new Hello2();
        h.hello();
        h2.hello2();
        ThreadContext.remove("ROUTINGKEY");

    }

}
package service;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.LoggerContext;

public class TestLoger {


    public static void main(String[] args) {

//       LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
//       File file = new File("log4j2.xml");
//       loggerContext.setConfigLocation(file.toURI());

        for(int i=0;i< 10; i++){

            ThreadLog log = new ThreadLog();
            Thread t = new Thread(log);
            t.start();
        }
    }

}
[Log4j2实现不同线程不同级别日志输出到不同的文件中](http://www.codepub.cn/2016/12/18/Log4j2-to-achieve-different-levels-of-different-threads-log-output-to-a-different-file/)

你可能感兴趣的:(log4j2)