我尝试配置Log4j2几个小时,但无法解决这个问题。
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
...
private static final Logger logger = LogManager.getLogger(ProductAttributesVerifier.class);
private static final Logger validationLoger = LogManager.getLogger("validation");
...
logger.warn("Started validation (in basic logger)");
validationLoger.info("INF Started validation of \"" + product.name + "\"");
validationLoger.warn("WRN Started validation of \"" + product.name + "\"");
validationLoger.error("ERR Started validation of \"" + product.name + "\"");
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="validation" fileName="validation.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m %ex%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="validation" level="warn">
<AppenderRef ref="validation"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
编辑:
来自
的日志:
2019-06-10 14:36:48,507 main DEBUG Apache Log4j Core 2.11.2 initializing configuration XmlConfiguration[location=...................\src\main\resources\log4j2.xml]
2019-06-10 14:36:48,514 main DEBUG Installed 1 script engine
2019-06-10 14:36:48,808 main DEBUG Oracle Nashorn version: 1.8.0_201, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
2019-06-10 14:36:48,809 main DEBUG PluginManager 'Core' found 117 plugins
2019-06-10 14:36:48,809 main DEBUG PluginManager 'Level' found 0 plugins
2019-06-10 14:36:48,813 main DEBUG PluginManager 'Lookup' found 13 plugins
2019-06-10 14:36:48,815 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2019-06-10 14:36:48,825 main TRACE TypeConverterRegistry initializing.
2019-06-10 14:36:48,826 main DEBUG PluginManager 'TypeConverter' found 26 plugins
2019-06-10 14:36:48,840 main DEBUG PatternLayout$Builder(pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(..................\src\main\resources\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2019-06-10 14:36:48,840 main DEBUG PluginManager 'Converter' found 47 plugins
2019-06-10 14:36:48,841 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2019-06-10 14:36:48,850 main DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), name="Console", Configuration(......................\src\main\resources\log4j2.xml), Filter=null, ={})
2019-06-10 14:36:48,852 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2019-06-10 14:36:48,853 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2019-06-10 14:36:48,854 main DEBUG PatternLayout$Builder(pattern="%d %p %c{1.} [%t] %m %ex%n", PatternSelector=null, Configuration(.............................\src\main\resources\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2019-06-10 14:36:48,870 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.FileAppender].
2019-06-10 14:36:48,873 main DEBUG FileAppender$Builder(fileName="validation.log", append="null", locking="null", advertise="null", advertiseUri="null", createOnDemand="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d %p %c{1.} [%t] %m %ex%n), name="validation", Configuration(..................\src\main\resources\log4j2.xml), Filter=null, ={})
2019-06-10 14:36:48,875 main DEBUG Starting FileManager validation.log
2019-06-10 14:36:48,877 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2019-06-10 14:36:48,878 main DEBUG createAppenders(={Console, validation})
2019-06-10 14:36:48,879 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2019-06-10 14:36:48,883 main DEBUG createAppenderRef(ref="validation", level="null", Filter=null)
2019-06-10 14:36:48,883 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2019-06-10 14:36:48,886 main DEBUG createLogger(additivity="true", level="WARN", name="validation", includeLocation="null", ={validation}, ={}, Configuration (..........................\src\main\resources\log4j2.xml), Filter=null)
2019-06-10 14:36:48,888 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2019-06-10 14:36:48,889 main DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2019-06-10 14:36:48,889 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2019-06-10 14:36:48,891 main DEBUG createLogger(additivity="null", level="ERROR", includeLocation="null", ={Console}, ={}, Configuration(.........................\src\main\resources\log4j2.xml), Filter=null)
2019-06-10 14:36:48,891 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2019-06-10 14:36:48,892 main DEBUG createLoggers(={validation, root})
2019-06-10 14:36:48,892 main DEBUG Configuration XmlConfiguration[location=.......................\src\main\resources\log4j2.xml] initialized
2019-06-10 14:36:48,893 main DEBUG Starting configuration XmlConfiguration[location=.......................\src\main\resources\log4j2.xml]
2019-06-10 14:36:48,893 main DEBUG Started configuration XmlConfiguration[location=.......................\src\main\resources\log4j2.xml] OK.
2019-06-10 14:36:48,893 main TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@60215eee...
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration stopping root LoggerConfig.
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration stopping remaining Appenders.
2019-06-10 14:36:48,895 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2019-06-10 14:36:48,895 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2019-06-10 14:36:48,895 main DEBUG Appender DefaultConsole-1 stopped with status true
2019-06-10 14:36:48,895 main TRACE DefaultConfiguration stopped 1 remaining Appenders.
2019-06-10 14:36:48,896 main TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
2019-06-10 14:36:48,896 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@60215eee OK
2019-06-10 14:36:48,942 main TRACE Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@707194ba
2019-06-10 14:36:48,943 main TRACE Reregistering context (1/1): '2a139a55' org.apache.logging.log4j.core.LoggerContext@1190200a
2019-06-10 14:36:48,943 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55'
2019-06-10 14:36:48,944 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=StatusLogger'
2019-06-10 14:36:48,944 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=ContextSelector'
2019-06-10 14:36:48,945 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=*'
2019-06-10 14:36:48,945 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=Appenders,name=*'
2019-06-10 14:36:48,946 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=AsyncAppenders,name=*'
2019-06-10 14:36:48,947 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=AsyncLoggerRingBuffer'
2019-06-10 14:36:48,947 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=*,subtype=RingBuffer'
2019-06-10 14:36:48,948 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55
2019-06-10 14:36:48,950 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=StatusLogger
2019-06-10 14:36:48,952 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=ContextSelector
2019-06-10 14:36:48,953 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=
2019-06-10 14:36:48,954 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=validation
2019-06-10 14:36:48,955 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Appenders,name=Console
2019-06-10 14:36:48,956 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Appenders,name=validation
2019-06-10 14:36:48,958 main TRACE Using default SystemClock for timestamps.
2019-06-10 14:36:48,959 main DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
2019-06-10 14:36:48,959 main TRACE Using DummyNanoClock for nanosecond timestamps.
2019-06-10 14:36:48,959 main DEBUG Reconfiguration complete for context[name=2a139a55] at URI .......................\src\main\resources\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@1190200a) with optional ClassLoader: null
2019-06-10 14:36:48,959 main DEBUG Shutdown hook enabled. Registering a new one.
2019-06-10 14:36:48,960 main DEBUG LoggerContext[name=2a139a55, org.apache.logging.log4j.core.LoggerContext@1190200a] started OK.
2019-06-10 14:36:49,189 main DEBUG Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@14bdbc74
2019-06-10 14:36:49,190 main INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
2019-06-10 14:36:49,190 main DEBUG Closing JarURLInputStream sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@5a7fe64f
我的配置(问题)是正确的。
在重新启动计算机(和Eclipse IDE)之后,它就开始正常工作了。不幸的是,我不知道为什么,但我会把这个答案张贴给未来的读者。
问题内容: 我正在使用log4j2。但是我面临的问题是它记录了所有日志。我想…从特定程序包登录到特定文件,而其他程序包登录到另一个文件。我正在使用log4j2.xml进行配置。请有人帮忙吗? log4j2.xml 问题答案: 刚刚回答了这个问题。 log4j2.xml 从根记录器中删除了。因此,它开始基于包记录日志。
在中有一个,可以将日志写入到。 我需要在中使用相同的功能,但我还没有找到这样做的选项。有人知道如何使用实现同样的效果吗?
假设我们构建了一个JavaSDK,不同的项目可以通过将其添加为类路径中的jar或添加为mavenpom.xml或gradle文件中的依赖项来使用它。当其他项目使用该库时,SDK中的日志在运行时不可见。我尝试使用SL4J,当其他项目使用它时,没有一个日志在运行时可见。我应该用log4j2吗?如果是,我应该在我的SDK中提供log4j配置 /properties文件吗?是否会在运行时从消费者库中提取属
我试图从log4j1更改为log4j2。我所做的: 已删除旧配置文件log4j。xml 创建了新的配置文件log4j2。xml 创建了jboss部署结构。xml 编辑的pom 环境:野蝇10 1 EAR 4 WAR文件中没有使用web.xml。以前的log4j配置日志在EAR中(仅) Log4j2配置文件 Jboss部署结构 新的maven依赖关系 我之前说过我在SLF4j中使用log4j1。这些