我通过使用-DLog4jContextSelector = org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -DAsyncLogger.RingBufferSize = 524288尝试了异步记录器 系统属性和异步记录器正常运行,但是当我删除系统属性并使用xml配置时,似乎记录器未配置为异步记录器,而是普通记录器。 在使用xml配置Async记录器时需要帮助,以便可以同步配置某些记录器,而另一些可以配置为Async。
<Configuration status="debug">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT" >
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
<RollingRandomAccessFile name="Rolling-Random-Access-File-Appender"
fileName="TestLogExample.log" filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz"
immediateFlush="false" append="false">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1MB"/>
</Policies>
<DefaultRolloverStrategy max="1"/>
</RollingRandomAccessFile >
</Appenders>
<Loggers>
<AsyncLogger name="asyncLogger" level="debug">
<AppenderRef ref="Rolling-Random-Access-File-Appender"/>
</AsyncLogger>
<AsyncRoot level="debug">
<AppenderRef ref="Console-Appender"/>
</AsyncRoot>
</Loggers>
</Configuration>
我的Java主程序
package com.program.async.log4j;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class AsyncLoggingExample{
private static Logger logger = LogManager.getLogger();
public void doLogging() {
logger= LogManager.getRootLogger();
logger.info("Started logger for first message-- ");
logger.info(Thread.currentThread().getName());
System.out.println("Started logger for first message-- ");
for (int i=0; i<400000;i++){
logger.info("Test first Message -- "+i);
}
logger.info("Started logger for second message-- ");
System.out.println("Started logger for second message-- ");
System.out.println("work completed");
}
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("Program started...."+sdf.format(date));
AsyncLoggingExample AsyncLoggingExample = new AsyncLoggingExample();
AsyncLoggingExample.doLogging();
System.out.println("Program ended... "+logger.getName());
System.out.println(logger.getName());
System.out.println("Program complete...."+sdf.format(new Date()));
}
}
日志
2018-08-01 15:14:56,212 main DEBUG Initializing configuration XmlConfiguration[location=C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml]
2018-08-01 15:14:56,212 main DEBUG Installed 1 script engine
2018-08-01 15:14:56,243 main DEBUG Mozilla Rhino version: 1.7 release 3 PRERELEASE, language: ECMAScript, threading: MULTITHREADED, compile: true, names: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript], factory class: com.sun.script.javascript.RhinoScriptEngineFactory
2018-08-01 15:14:56,243 main DEBUG PluginManager 'Core' found 116 plugins
2018-08-01 15:14:56,243 main DEBUG PluginManager 'Level' found 0 plugins
2018-08-01 15:14:56,243 main DEBUG PluginManager 'Lookup' found 13 plugins
2018-08-01 15:14:56,243 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2018-08-01 15:14:56,259 main DEBUG PluginManager 'TypeConverter' found 26 plugins
2018-08-01 15:14:56,274 main DEBUG PatternLayout$Builder(pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n", PatternSelector=null, Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2018-08-01 15:14:56,274 main DEBUG PluginManager 'Converter' found 44 plugins
2018-08-01 15:14:56,290 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2018-08-01 15:14:56,306 main DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout([%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n), name="Console-Appender", Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml), Filter=null)
2018-08-01 15:14:56,306 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2018-08-01 15:14:56,306 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2018-08-01 15:14:56,306 main DEBUG PatternLayout$Builder(pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n", PatternSelector=null, Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2018-08-01 15:14:56,306 main DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2018-08-01 15:14:56,306 main DEBUG createPolicy(size="1MB")
2018-08-01 15:14:56,321 main DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2018-08-01 15:14:56,321 main DEBUG createPolicy(={SizeBasedTriggeringPolicy(size=1048576)})
2018-08-01 15:14:56,321 main DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
2018-08-01 15:14:56,321 main DEBUG DefaultRolloverStrategy$Builder(max="1", min="null", fileIndex="null", compressionLevel="null", ={}, stopCustomActionsOnError="null", tempCompressedFilePattern="null", Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml))
2018-08-01 15:14:56,321 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender].
2018-08-01 15:14:56,321 main DEBUG RollingRandomAccessFileAppender$Builder(fileName="TestLogExample.log", filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz", append="false", Policies(CompositeTriggeringPolicy(policies=[SizeBasedTriggeringPolicy(size=1048576)])), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=1, useMax=true)), advertise="null", advertiseURI="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="false", ignoreExceptions="null", PatternLayout([%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n), name="Rolling-Random-Access-File-Appender", Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml), Filter=null)
2018-08-01 15:14:56,337 main DEBUG Starting RollingRandomAccessFileManager TestLogExample.log
2018-08-01 15:14:56,337 main DEBUG PluginManager 'FileConverter' found 2 plugins
2018-08-01 15:14:56,337 main DEBUG Setting prev file time to 2018-08-01T15:14:56.321-0400
2018-08-01 15:14:56,337 main DEBUG Initializing triggering policy CompositeTriggeringPolicy(policies=[SizeBasedTriggeringPolicy(size=1048576)])
2018-08-01 15:14:56,337 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2018-08-01 15:14:56,337 main DEBUG createAppenders(={Console-Appender, Rolling-Random-Access-File-Appender})
2018-08-01 15:14:56,337 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2018-08-01 15:14:56,337 main DEBUG createAppenderRef(ref="Rolling-Random-Access-File-Appender", level="null", Filter=null)
2018-08-01 15:14:56,337 main DEBUG Building Plugin[name=asyncLogger, class=org.apache.logging.log4j.core.async.AsyncLoggerConfig].
2018-08-01 15:14:56,353 main ERROR AsyncLogger contains an invalid element or attribute "param"
2018-08-01 15:14:56,353 main DEBUG createLogger(additivity="null", level="debug", name="asyncLogger", includeLocation="null", ={Rolling-Random-Access-File-Appender}, ={}, Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml), Filter=null)
2018-08-01 15:14:56,353 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2018-08-01 15:14:56,353 main DEBUG createAppenderRef(ref="Console-Appender", level="null", Filter=null)
2018-08-01 15:14:56,353 main DEBUG Building Plugin[name=asyncRoot, class=org.apache.logging.log4j.core.async.AsyncLoggerConfig$RootLogger].
2018-08-01 15:14:56,353 main DEBUG createLogger(additivity="null", level="debug", includeLocation="null", ={Console-Appender}, ={}, Configuration(C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml), Filter=null)
2018-08-01 15:14:56,353 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2018-08-01 15:14:56,353 main DEBUG createLoggers(={asyncLogger, root})
2018-08-01 15:14:56,353 main DEBUG Configuration XmlConfiguration[location=C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml] initialized
2018-08-01 15:14:56,353 main DEBUG Starting configuration XmlConfiguration[location=C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml]
2018-08-01 15:14:56,384 main DEBUG Starting AsyncLoggerConfig disruptor for this configuration with ringbufferSize=4096, waitStrategy=TimeoutBlockingWaitStrategy, exceptionHandler=org.apache.logging.log4j.core.async.AsyncLoggerConfigDefaultExceptionHandler@7ee4e2f4...
2018-08-01 15:14:56,384 main DEBUG Started configuration XmlConfiguration[location=C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml] OK.
2018-08-01 15:14:56,399 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2018-08-01 15:14:56,399 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2018-08-01 15:14:56,399 main DEBUG Appender DefaultConsole-1 stopped with status true
2018-08-01 15:14:56,399 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@2248ffff OK
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=StatusLogger
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=ContextSelector
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=Loggers,name=
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=Loggers,name=,subtype=RingBuffer
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=Loggers,name=asyncLogger
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=Loggers,name=asyncLogger,subtype=RingBuffer
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=Appenders,name=Console-Appender
2018-08-01 15:14:56,431 main DEBUG Registering MBean org.apache.logging.log4j2:type=80eb7c5a,component=Appenders,name=Rolling-Random-Access-File-Appender
2018-08-01 15:14:56,446 main DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
2018-08-01 15:14:56,446 main DEBUG Reconfiguration complete for context[name=80eb7c5a] at URI C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@11469e98) with optional ClassLoader: null
2018-08-01 15:14:56,446 main DEBUG Shutdown hook enabled. Registering a new one.
2018-08-01 15:14:56,446 main DEBUG LoggerContext[name=80eb7c5a, org.apache.logging.log4j.core.LoggerContext@11469e98] started OK.
Program started....2018/08/01 15:14:56
Started logger for first message--
Started logger for second message--
work completed
Program ended...
Program complete....2018/08/01 15:14:56
2018-08-01 15:14:56,462 pool-1-thread-1 DEBUG Stopping LoggerContext[name=80eb7c5a, org.apache.logging.log4j.core.LoggerContext@11469e98]
2018-08-01 15:14:56,462 pool-1-thread-1 DEBUG Stopping LoggerContext[name=80eb7c5a, org.apache.logging.log4j.core.LoggerContext@11469e98]...
[INFO ] 2018-08-01 15:14:56.446 [main] - Started logger for first message--
[INFO ] 2018-08-01 15:14:56.446 [main] - main
[INFO ] 2018-08-01 15:14:56.446 [main] - Started logger for second message--
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Shutting down RollingRandomAccessFileManager TestLogExample.log
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Shutting down RollingFileManager {}TestLogExample.log
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG All asynchronous threads have terminated
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG RollingFileManager shutdown completed with status true
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Shut down RollingRandomAccessFileManager TestLogExample.log, all resources released: true
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Appender Rolling-Random-Access-File-Appender stopped with status true
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false, all resources released: true
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Appender Console-Appender stopped with status true
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Stopped XmlConfiguration[location=C:\Users\najoshi\Documents\WPWorkspaceDev4\TestLoggerProject\bin\log4j2.xml] OK
2018-08-01 15:14:56,478 pool-1-thread-1 DEBUG Stopped LoggerContext[name=80eb7c5a, **org.apache.logging.log4j.core.LoggerContext@11469e98] with status true**