2017-01-17 229 views
1

我正在嘗試使用Jersey爲Java Web應用程序項目設置Log4j2。這是我log4j2.xml文件保存在WEB-INF/classes來自Log4j2的日誌文件的位置

<?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="MyFile" fileName="/home/hans/Dev/Software/Tomcat/logs/all.log"> 
      <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> 
     </File> 
    </Appenders> 
    <Loggers> 
     <Root level="debug"> 
      <AppenderRef ref="Console" /> 
      <AppenderRef ref="MyFile"/> 
     </Root> 
    </Loggers> 
</Configuration> 

當我運行的IntelliJ(IJ)在Tomcat項目它打印出精美的控制檯按照Log4j2.xml文件。當我將相同的WAR文件直接部署到本地Tomcat服務器時,我無法獲得控制檯輸出,因此我指定了要寫入/home/hans/Dev/Software/Tomcat/logs/all.log的日誌文件。我已經在日誌文件夾上設置了權限,以便任何人都可以寫入,但日誌文件不會寫入該文件夾。

在IJ服務器啓動時,我有以下消息:

ERROR StatusLogger File not found in file system or classpath: /WEB-INF/classes/log4j2.xml 
ERROR StatusLogger File not found in file system or classpath: /WEB-INF/classes/log4j2.xml 
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 

我試過兩個不同的設置。兩者都給出相同的錯誤。這是德兩種設置和結果:

<context-param> 
    <param-name>log4jConfiguration</param-name> 
    <param-value>/WEB-INF/classes/log4j2.xml</param-value> 
</context-param> 

enter image description here

<context-param> 
    <param-name>log4jConfiguration</param-name> 
    <param-value>log4j2.xml</param-value> 
</context-param> 

隨着log4j2.xml/WEB-INF/classes enter image description here

隨着log4j2.xml/WEB-INF enter image description here

出從Dorg.apache.logging.log4j.simplelog.StatusLogger.level = TRACE放置。我只能看到它正在查找配置文件,但我無法看到它正在查找的位置。

DEBUG StatusLogger Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment. 
DEBUG StatusLogger Loaded Provider Provider[priority=10, className=org.apache.logging.log4j.core.impl.Log4jContextFactory, url=jar:file:/home/mikael/Dev/Java/Java%20Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/lib/log4j-core-2.7.jar!/META-INF/log4j-provider.properties, classLoader=ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
] 
DEBUG StatusLogger Using ShutdownCallbackRegistry class org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry 
DEBUG StatusLogger Using the servlet context name "CurrencyRegistry". 
DEBUG StatusLogger getConfigURI found [log4j2.xml] in servletContext at [log4j2.xml] 
DEBUG StatusLogger Took 0.087752 seconds to load 192 plugins from ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 

DEBUG StatusLogger PluginManager 'Converter' found 41 plugins 
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-1 
DEBUG StatusLogger PluginManager 'ConfigurationFactory' found 4 plugins 
DEBUG StatusLogger Missing dependencies for Yaml support 
DEBUG StatusLogger Missing dependencies for Json support 
DEBUG StatusLogger Using configurationFactory or[email protected]7dea68f1 
TRACE StatusLogger Trying to find [log4j2.xml] using context class loader ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
. 
TRACE StatusLogger Trying to find [log4j2.xml] using ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
class loader. 
TRACE StatusLogger Trying to find [log4j2.xml] using ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
class loader. 
TRACE StatusLogger Trying to find [log4j2.xml] using ClassLoader.getSystemResource(). 
ERROR StatusLogger File not found in file system or classpath: log4j2.xml 
TRACE StatusLogger Trying to find [log4j2.xml] using context class loader ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
. 
TRACE StatusLogger Trying to find [log4j2.xml] using ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
class loader. 
TRACE StatusLogger Trying to find [log4j2.xml] using ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
class loader. 
TRACE StatusLogger Trying to find [log4j2.xml] using ClassLoader.getSystemResource(). 
ERROR StatusLogger File not found in file system or classpath: log4j2.xml 
TRACE StatusLogger Trying to find [log4j2-testCurrencyRegistry.properties] using context class loader ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
. 

<---MORE OF THESE TESTS TO FIND THE CONFIG FILE NONE OF THEM FINDS THE FILE---> 

TRACE StatusLogger Trying to find [log4j2.xml] using ClassLoader.getSystemResource(). 
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 

log4j2.xml文件移動到文件夾src現在得到由log4j2後撈起。我仍然收到一些警告。

log4j:WARN No appenders could be found for logger (org.jose4j.jwa.AlgorithmFactoryFactory). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

我使用的是使用SLF4J的日誌Bitbuckets JWT庫,它需要橋接使用

<dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-slf4j-impl</artifactId> 
     <version>2.0.2</version> 
    </dependency> 

從Dorg.apache.logging.log4j移動文件輸出後log4j2。 simplelog.StatusLogger.level = TRACE是根據以下。現在看起來它能夠在/WEB-INF/classes中找到該文件,即使該文件在src之下。我不知道爲什麼。

DEBUG StatusLogger Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment. 
DEBUG StatusLogger Loaded Provider Provider[priority=10, className=org.apache.logging.log4j.core.impl.Log4jContextFactory, url=jar:file:/home/mikael/Dev/Java/Java%20Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/lib/log4j-core-2.7.jar!/META-INF/log4j-provider.properties, classLoader=ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
] 
DEBUG StatusLogger Using ShutdownCallbackRegistry class org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry 
DEBUG StatusLogger Using the servlet context name "CurrencyRegistry". 
DEBUG StatusLogger getConfigURI found [log4j2.xml] in servletContext at [log4j2.xml] 
DEBUG StatusLogger Took 0.097889 seconds to load 192 plugins from ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 

DEBUG StatusLogger PluginManager 'Converter' found 41 plugins 
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-1 
DEBUG StatusLogger PluginManager 'ConfigurationFactory' found 4 plugins 
DEBUG StatusLogger Missing dependencies for Yaml support 
DEBUG StatusLogger Missing dependencies for Json support 
DEBUG StatusLogger Using configurationFactory or[email protected]2f824a06 
TRACE StatusLogger Trying to find [log4j2.xml] using context class loader ParallelWebappClassLoader 
    context: ROOT 
    delegate: false 
----------> Parent Classloader: 
[email protected] 
. 
2017-01-18 11:01:06,589 RMI TCP Connection(2)-127.0.0.1 DEBUG Starting LoggerContext[name=CurrencyRegistry] from configuration at log4j2.xml 
2017-01-18 11:01:06,590 RMI TCP Connection(2)-127.0.0.1 DEBUG Starting LoggerContext[name=CurrencyRegistry, [email protected]] with configuration XmlConfiguration[location=/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml]... 
2017-01-18 11:01:06,596 RMI TCP Connection(2)-127.0.0.1 DEBUG Shutdown hook enabled. Registering a new one. 
2017-01-18 11:01:06,598 RMI TCP Connection(2)-127.0.0.1 DEBUG Initializing configuration XmlConfiguration[location=/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml] 
2017-01-18 11:01:06,610 RMI TCP Connection(2)-127.0.0.1 DEBUG Installed script engines 
2017-01-18 11:01:07,234 RMI TCP Connection(2)-127.0.0.1 DEBUG Oracle Nashorn Version: 1.8.0_111, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript} 
2017-01-18 11:01:07,235 RMI TCP Connection(2)-127.0.0.1 DEBUG PluginManager 'Core' found 108 plugins 
2017-01-18 11:01:07,235 RMI TCP Connection(2)-127.0.0.1 DEBUG PluginManager 'Level' found 0 plugins 
2017-01-18 11:01:07,241 RMI TCP Connection(2)-127.0.0.1 DEBUG Log4j2 ConfigurationScheduler: No scheduled items 
2017-01-18 11:01:07,242 RMI TCP Connection(2)-127.0.0.1 DEBUG PluginManager 'Lookup' found 14 plugins 
2017-01-18 11:01:07,245 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. 
2017-01-18 11:01:07,258 RMI TCP Connection(2)-127.0.0.1 TRACE TypeConverterRegistry initializing. 
2017-01-18 11:01:07,259 RMI TCP Connection(2)-127.0.0.1 DEBUG PluginManager 'TypeConverter' found 23 plugins 
2017-01-18 11:01:07,281 RMI TCP Connection(2)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null") 
2017-01-18 11:01:07,282 RMI TCP Connection(2)-127.0.0.1 DEBUG PluginManager 'Converter' found 41 plugins 
2017-01-18 11:01:07,283 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender]. 
2017-01-18 11:01:07,296 RMI TCP Connection(2)-127.0.0.1 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", Filter=null) 
2017-01-18 11:01:07,297 RMI TCP Connection(2)-127.0.0.1 DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false 
2017-01-18 11:01:07,297 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. 
2017-01-18 11:01:07,299 RMI TCP Connection(2)-127.0.0.1 DEBUG PatternLayout$Builder(pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null") 
2017-01-18 11:01:07,318 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.FileAppender]. 
2017-01-18 11:01:07,322 RMI TCP Connection(2)-127.0.0.1 DEBUG FileAppender$Builder(fileName="/home/mikael/Dev/Software/Tomcat/logs/all.log", append="null", locking="null", advertise="null", advertiseUri="null", createOnDemand="null", Configuration(/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml), bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), name="MyFile", Filter=null) 
2017-01-18 11:01:07,326 RMI TCP Connection(2)-127.0.0.1 DEBUG Starting FileManager /home/mikael/Dev/Software/Tomcat/logs/all.log 
2017-01-18 11:01:07,326 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. 
2017-01-18 11:01:07,327 RMI TCP Connection(2)-127.0.0.1 DEBUG createAppenders(={Console, MyFile}) 
2017-01-18 11:01:07,328 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. 
2017-01-18 11:01:07,333 RMI TCP Connection(2)-127.0.0.1 DEBUG createAppenderRef(ref="Console", level="null", Filter=null) 
2017-01-18 11:01:07,334 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. 
2017-01-18 11:01:07,334 RMI TCP Connection(2)-127.0.0.1 DEBUG createAppenderRef(ref="MyFile", level="null", Filter=null) 
2017-01-18 11:01:07,335 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. 
2017-01-18 11:01:07,337 RMI TCP Connection(2)-127.0.0.1 DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={Console, MyFile}, ={}, Configuration(/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml), Filter=null) 
2017-01-18 11:01:07,342 RMI TCP Connection(2)-127.0.0.1 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. 
2017-01-18 11:01:07,343 RMI TCP Connection(2)-127.0.0.1 DEBUG createLoggers(={root}) 
2017-01-18 11:01:07,345 RMI TCP Connection(2)-127.0.0.1 DEBUG Configuration XmlConfiguration[location=/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml] initialized 
2017-01-18 11:01:07,345 RMI TCP Connection(2)-127.0.0.1 DEBUG Starting configuration XmlConfiguration[location=/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml] 
2017-01-18 11:01:07,346 RMI TCP Connection(2)-127.0.0.1 DEBUG Started configuration XmlConfiguration[location=/home/mikael/Dev/Java/Java Programs/CurrencyRegistry/target/CurrencyRegistry/WEB-INF/classes/log4j2.xml] OK. 
2017-01-18 11:01:07,346 RMI TCP Connection(2)-127.0.0.1 TRACE Stopping [email protected]52... 
2017-01-18 11:01:07,346 RMI TCP Connection(2)-127.0.0.1 TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped. 
2017-01-18 11:01:07,347 RMI TCP Connection(2)-127.0.0.1 TRACE DefaultConfiguration stopping root LoggerConfig. 
2017-01-18 11:01:07,347 RMI TCP Connection(2)-127.0.0.1 TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped. 
2017-01-18 11:01:07,348 RMI TCP Connection(2)-127.0.0.1 TRACE DefaultConfiguration stopping remaining Appenders. 
2017-01-18 11:01:07,348 RMI TCP Connection(2)-127.0.0.1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1 
2017-01-18 11:01:07,348 RMI TCP Connection(2)-127.0.0.1 DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true 
2017-01-18 11:01:07,349 RMI TCP Connection(2)-127.0.0.1 TRACE DefaultConfiguration stopped 1 remaining Appenders. 
2017-01-18 11:01:07,349 RMI TCP Connection(2)-127.0.0.1 TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs. 
2017-01-18 11:01:07,349 RMI TCP Connection(2)-127.0.0.1 DEBUG Stopped [email protected]52 OK 
2017-01-18 11:01:07,352 RMI TCP Connection(2)-127.0.0.1 TRACE Reregistering MBeans after reconfigure. Selector=or[email protected]3b475abf 
2017-01-18 11:01:07,352 RMI TCP Connection(2)-127.0.0.1 TRACE Reregistering context (1/1): 'CurrencyRegistry' [email protected] 

我不知道爲什麼它不工作的時候,配置文件是在WEB-INF文件夾中。也許是我的文件夾結構中的某些內容與log4j2如何查找配置文件無關。我也不明白爲什麼當我將它移動到src時找到/WEB-INF/classes下的配置文件。

+0

嘗試設置'<配置狀態=「跟蹤」>'在配置以查看是否Log4j2初始化期間發生的任何錯誤。 –

+0

跟蹤給了一些更多的輸出到控制檯。這個問題似乎是log4j2無法找到log4j2.xml文件。我不明白爲什麼找不到它。我在web.xml文件中添加了一些關於我的文件夾結構和配置的更多信息。 – g3blv

+0

已經嘗試了很多,它很難遵循因素的組合。這似乎是一個Maven化項目,所需要的唯一的事情應該是把文件'的src/main/resources',這將引發戰爭的Maven插件來包裝這裏面'WEB-INF/classes'。不需要進一步的配置,log4j按照約定會在類路徑的根目錄(包括WEB-INF/classes)中查找配置文件。 – Gimby

回答

0

我相信指定的路徑是相對於Web應用程序的類路徑的根,所以這樣的:

<context-param> 
    <param-name>log4jConfiguration</param-name> 
    <param-value>/WEB-INF/classes/log4j2.xml</param-value> 
</context-param> 

如果WEB-INF/classes/WEB-INF/classes/log4j2.xml存在,纔會被發現。由於您的示例log4j2.xml文件在classpath的根目錄,下面的配置應該是足夠了:

<context-param> 
    <param-name>log4jConfiguration</param-name> 
    <param-value>log4j2.xml</param-value> 
</context-param> 

您可以通過設置系統屬性-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACEdebug配置。


log4j:WARN消息不被Log4j2生成。只有在類路徑中存在舊的Log4j 1.2.x jar文件時纔會看到這些消息。


IntelliJ將0​​中的所有資源複製到類路徑(WEB-INF/classes)中。在構建期間,它可能首先清理目標目錄,以便刪除您手動在其中複製的任何文件。所以,不要手動將文件放入classes,那麼您只是在與IDE戰鬥。標準設置是遵循Maven的目錄佈局,這是把配置文件中resources目錄。這個目錄需要被添加到類路徑中。請參閱IntelliJ幫助以將文件夾指定爲資源文件夾。

+0

我給了一個與在'/ WEB-INF'文件夾中的'log4j2.xml'文件和'/ WEB-INF/classes'文件夾都試一下。但它仍然無法找到它。我應該把'log4j2.xml'文件移到別的地方嗎? – g3blv

+0

更新了我的答案。 –

+0

我猜將在'src'文件夾'log4j2.xml'是IJ的解決方案。 'log4j:WARN'是由錯誤的橋接依賴引起的。我正在使用'slf4j-log4j12'版本1.7.22。更改爲'log4j-slf4j-impl'版本2.0.2解決了這個問題。 – g3blv