代码:
package com.zero.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log1 { private static Logger logger = LogManager.getLogger(Log1.class); public Log1() { logger.debug("Log1,我默认记录在log1.log"); logger.info("Log1,我默认记录在log1.log"); logger.warn("Log1,我默认记录在log1.log"); logger.error("Log1,我默认记录在log1.log"); } }
package com.zero.log4j2.log2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log2 { private static Logger logger = LogManager.getLogger(); public Log2() { logger.debug("Log2, 我默认记录到log2.log"); logger.info("Log2, 我默认记录到log2.log"); logger.warn("Log2, 我默认记录到log2.log"); logger.error("Log2, 我默认记录到log2.log"); } }
package com.zero.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log3 { private static Logger logger = LogManager.getLogger(); public Log3() { logger.debug("Log3, 我默认记录到log1.log"); logger.info("Log3, 我默认记录到log1.log"); logger.warn("Log3, 我默认记录到log1.log"); logger.error("Log3, 我默认记录到log1.log"); Log4j2Util.log("Log3," + " 我要记录到log3.log", "zero"); } }
package com.zero.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4j2Util { private static Logger logger = LogManager.getLogger("zero"); public static void log(String logStr, String logType) { Logger myLogger = LogManager.getLogger(logType); myLogger.info(logStr); myLogger.warn(logStr); logger.info("Log4j2Util, 我想记录在log3.log"); } }
package com.zero.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.zero.log4j2.log2.Log2; public class Test { static Logger logger = LogManager.getLogger(); public static void main(String[] args) { logger.info("log {}", 123456789); new Log1(); new Log3(); new Log2(); } }log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- No need to set system property "Log4jContextSelector" to any value when using <asyncLogger> or <asyncRoot>. --> <Configuration status="WARN"> <Properties> <Property name="filedir">logs</Property> </Properties> <Appenders> <!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. --> <RollingRandomAccessFile name="log1" fileName="${filedir}/log1.log" append="true" immediateFlush="false" filePattern="logs/app-%d{MM-dd-yyyy}.log"> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%c{36}] %msg%xEx%n </Pattern> </PatternLayout> <bufferSize>262144</bufferSize><!--默认:256 * 1024 bytes --> <Policies> <!-- 基于时间的触发策略。该策略主要是完成周期性的log文件封存工作。 --> <!-- interval,integer型,指定两次封存动作之间的时间间隔。 --> <!-- ====单位:以日志的命名精度来确定单位,比如yyyy-MM-dd-HH 单位为小时,yyyy-MM-dd-HH-mm 单位为分钟。 --> <!-- modulate,boolean型,说明是否对封存时间进行调制。 --> <!-- ====若modulate=true,则封存时间将以0点为边界进行偏移计算。 --> <!-- ====比 如,modulate=true,interval=4hours,那么假设上次封存日志的时间为01:00,则下次封存日志的时间为04:00,之后的封存时间依次为08:00,12:00。。。 --> <TimeBasedTriggeringPolicy interval="24" modulate="true" /> </Policies> </RollingRandomAccessFile> <RollingRandomAccessFile name="log2" fileName="logs/log2.log" append="true" immediateFlush="false" filePattern="logs/log2-%d{MM-dd-yyyy}.log"> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%c{36}] %msg%xEx%n </Pattern> </PatternLayout> <bufferSize>262144</bufferSize><!--默认:256 * 1024 bytes --> <Policies> <TimeBasedTriggeringPolicy interval="24" modulate="true" /> </Policies> </RollingRandomAccessFile> <RollingRandomAccessFile name="log3" fileName="${filedir}/log3.log" append="true" immediateFlush="false" filePattern="logs/log3-%d{MM-dd-yyyy}.log"> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%c] %msg%xEx%n </Pattern> </PatternLayout> <bufferSize>262144</bufferSize><!--默认:256 * 1024 bytes --> <Policies> <TimeBasedTriggeringPolicy interval="24" modulate="true" /> </Policies> </RollingRandomAccessFile> </Appenders> <Loggers> <AsyncRoot level="debug"> <AppenderRef ref="log1" /> </AsyncRoot> <AsyncLogger name="com.zero.log4j2.log2" level="trace" includeLocation="false" additivity="false"> <AppenderRef ref="log2" /> </AsyncLogger> <AsyncLogger name="zero" level="debug" includeLocation="false" additivity="false"> <AppenderRef ref="log3" /> </AsyncLogger> </Loggers> </Configuration>