I tried to set up LOG4J according documentation (and related SO questions), but it does not create supposed file, but there is such log in WildFly:
No Log4j context configuration provided. This is very unusual
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j2.xml</param-value>
</context-param>
app.war/WEB-INF/classes/log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
<!-- http://logging.apache.org/log4j/2.x/manual/configuration.html -->
<Properties>
<Property name="filename">c:/oauth.log</Property>
</Properties>
<Filter type="ThresholdFilter" level="trace"/>
<Appenders>
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
<File name="MyFile" fileName="c:/oauth2.log" immediateFlush="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="cz.literak.demo" level="debug" additivity="true">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
app.war/WEB-INF/lib
commons-logging-1.1.3.jar
json-smart-1.1.1.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
log4j-jcl-2.0-rc1.jar
Could you tell me what is wrong? I tried to comment out context param in web.xml and rely on autoconfiguration but there is no change.
EDIT
when I added following code
<context-param>
<param-name>log4jContextName</param-name>
<param-value>oauthDemo</param-value>
</context-param>
it failed differently (I do not have time to investigate now)
07:41:29,269 INFO [io.undertow.servlet] (MSC service thread 1-12) Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.
07:41:29,644 INFO [stdout] (MSC service thread 1-12) 2014-02-20 07:41:29,643 ERROR FileManager (c:/oauth2.log) java.io.FileNotFoundException: c:\oauth2.log (PĹ™Ăstup byl odepĹ™en)
07:41:29,646 INFO [stdout] (MSC service thread 1-12) 2014-02-20 07:41:29,645 ERROR Unable to invoke method createAppender in class org.apache.logging.log4j.core.appender.FileAppender for element File 07:41:29,647 INFO [stdout] (MSC service thread 1-12) at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:913)
As a reference this page describes how to configure Log4j2:
https://logging.apache.org/log4j/2.x/manual/webapp.html#ContextParams
In my case I have not configured any context-param in web.xml. The only thing I had to do was to set the display name:
<display-name>My Admin API</display-name>
I am also using log4j2.yaml instead of xml files and the file is not inside the war. Also notice that in this page https://logging.apache.org/log4j/2.x/manual/webapp.html they say there is a problem with versions of Tomcat < 7.0.43. So either you have to use a newer version of do a specific configuration.
Log4J will look for the log4j2.xml config file in the classpath, unless a location is specified.
Have you tried not specifying the location of the log4j2.xml file (that is, remove the context-param stuff from web.xml), and simply relying on putting the config in the classpath? (app.war/WEB-INF/classes/log4j2.xml looks fine to me)
Note that the file must be named log4j2.xml and not log4j.xml.
Related
I am receiving the following logs repeatedly while using Spring AMQP:
01:21:54.323 [SimpleAsyncTaskExecutor-1] DEBUG
o.s.a.r.l.BlockingQueueConsumer - Retrieving delivery for Consumer:
tag=[amq.ctag-XAaHUPISVTmjpL-am3y15g], channel=Cached Rabbit Channel:
AMQChannel(....),
acknowledgeMode=AUTO local queue size=0 01:21:55.325
[SimpleAsyncTaskExecutor-1] DEBUG o.s.a.r.l.BlockingQueueConsumer -
Retrieving delivery for Consumer:
tag=[amq.ctag-XAaHUPISVTmjpL-am3y15g], channel=Cached Rabbit Channel:
AMQChannel(....),
acknowledgeMode=AUTO local queue size=0 01:21:56.327
My log.xml file looks like this
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
....
<logger name="org.springframework.amqp.rabbit">
<level value="off" />
</logger>
<logger name="org.springframework.amqp">
<level value="off" />
</logger>
<!-- Root Logger -->
<root>
<priority value="off" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Upon closer inspection I found that the BlockingQueueConsumer class in org.springframework.amqp.rabbit emits the above logs and the exact line is
if (logger.isDebugEnabled()) {
logger.debug("Retrieving delivery for " + this);
}
My question is how is this possible given the above log4j.xml entries. Am I missing something. I am using the following loggers slf4j-api-1.6.6.jar,slf4j-log4j12-1.6.6.jar,log4j-1.2.15.jar
EDIT
With reference to the comments by zapl I started this project with STS and used a new Spring MVC Project the log files weren't manually configured. Is the project reading the log file from some other xml?
Thanks for pointing that out zapl. I found the issue. In the web.xml file you should mention this
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/resource/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Refer to this answer:
How to configure the log4j output file path in web.xml and log4j.properties?
I have a JAX-RS 2.0 application running on a Tomcat 7 server, and I'm using log4j2 along with SLF4J to record the server logs to a file.
I can't seem to get any logs to show up properly in my log file when running the server in production, although when I run my integration tests, logs are output correctly.
In production, the logs are merely redirected to the console instead.
My log4j2.xml file is located in the WEB-INF/classes folder, and I've included all the necessary dependencies as well.
My configuration file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="file" fileName="log/trace.log" append="true" filePattern="log/trace.%i.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %X %logger{36} - %msg%n"/>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.package" level="TRACE" additivity="false">
<AppenderRef ref="file"/>
</Logger>
<Root level="WARN">
<AppenderRef ref="file"/>
</Root>
</Loggers>
</Configuration>
The web.xml needs no configuration (I'm following the documentation found on the log4j2 website).
EDIT
I've tried setting the Root level to TRACE but everything still gets redirected to console. The file log/trace.log itself is created, it's just never written to. I also tried setting immediateFlush=true but that didn't have any impact either.
I noticed you have status logging (log4j internal logging) set to TRACE. This will dump log4j internal initialization messages to the console. It this what you mean?
Otherwise, the config you provide shows there is no logger that has an appender-ref pointing to the ConsoleAppender.
So, if you are also seeing your application logs being output to the console (in addition to log4j's status log messages), I suspect there is another log4j2.xml (or log4j2-test.xml) config file in the classpath somewhere.
Fortunately log4j's status logs should also show the location of the log4j config file, so you can confirm which config file is actually being loaded.
You can switch off status logging by setting <Configuration status="WARN"> after confirming all works correctly.
I figured it out!
Turns out I was using the gretty plug-in with gradle, which contains it's own logging package (the logback library).
It used it's own internal logback.xml which was redirecting to console. I fixed this by overwriting the internal logback.xml with my own (using logback's configuration) and now everything works as expected!
I'm currently writing a web app using Servlet 3.1. According to the Log4j2 website
Log4j 2 "just works" in Servlet 3.0 and newer web applications.. But whenever I start the server (Tomcat 8.0.5), I get an error Error processing element RandomAccessFileAppender: CLASS_NOT_FOUND. When I lookup the class using eclipses "Open Type", I see that this class exists and is in the package org.apache.logging.log4j.core.appender.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off" packages="org.apache.logging.log4j.core.appender">
<Appenders>
<RandomAccessFileAppender name="DEFLOG" ignoreExceptions="false" fileName="${web:rootDir}/log/log.log">
<PatternLayout pattern="%d %p %C{1.} [%t] %m%n" />
</RandomAccessFileAppender>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="DEFLOG"/>
</Root>
</Loggers>
</Configuration>
This log4j2.xml file is located under src/log4j2.xml, the Log4j2-jars "log4j-api-2.0-rc1.jar" and "log4j-core-2.0-rc1.jar" are located under WEB-INF/lib/.
According to here, Log4j2 should work with Tomcat7.0.47. I'm using TomEE Plus 7.0.47.
I have a webapplication deployed with a log4j2.xml in my web-inf/classes folder. This is the config:
<?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="File" fileName="${sys:catalina.home}/logs/testapp.log">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="org.alex" level="TRACE" additivity="false">
<AppenderRef ref="File"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
I have a logger declared in a class with name org.alex.util.JSON:
private static final Logger LOG = LoggerFactory.getLogger(JSON.class);
I'm using slf4j-api 1.7.5, and have the following libs added to the tomcat lib:
slf4j-api-1.7.5.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
log4j-slf4j-impl-2.0-rc1.jar
If I change the Configuration status to TRACE, I can see my configuration file being picked up and configuration happens as expected. Also I can see the MBeans being added.
However, there's not one logging statement ending up in my logfile. I debugged into the log4j2 Logger, and see that the isEnabled(...) method returns false because the logger (com.alex.util.JSON) has the level "ERROR" set, while the configuration set the package org.alex to TRACE.
Further investigation shows it uses a DefaultConfiguration configured for level=ERROR, and only root is configured. I'm thinking of a classloader issue, but I can't seem to figure out what the cause is and how to solve it.
Does anyone know what I'm doing wrong?
This should work on trunk
Btw saw log4j2 has hacks for tomcat and since tomee wraps classloaders not sure they work as expected...
This is very strange. Please raise a ticket for this in the Log4j2 issue tracker so the Log4j team can take a look.
The problem may go away if you put the jar files you mentioned inside WEB-INF/lib instead of in Tomcat's lib folder.
To be comple log4j2 relies on servletcontainerinitializer which are called after ejb and app scanning so ejbs can be loaded too early. Doing the same with a tomcat context listener would make it working better
I am running an example using log4j 2.0-rc1 and log4j.properties file, but log4j lib always runs it with the default configuration (log level, appender, etc). I also tried changing the name to log4j2.properties and nothing happened.
Log4j 2 doesn't support the Log4j v1 ".properties" format anymore (yet, since v2.4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".properties", it may be confusing).
To specify the location of your configuration file, do you use the system property log4j.configurationFile, the Log4j class ConfigurationFactory, or something else?
Did you read this manual page? It explains that:
Although the Log4j 2 configuration syntax is different than that of Log4j 1.x, most, if not all, of the same functionality is available.
So it seems that a legacy Log4j1.x log4j.propertiesfile is not supported as is, it must be migrated to v2.x format. The migration seems quite easy though, looking at the example in the link I gave above. Here is an extract:
Example of Log4j v1.x config file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<category name="org.apache.log4j.xml">
<priority value="info" />
</category>
<Root>
<priority value ="debug" />
<appender-ref ref="STDOUT" />
</Root>
</log4j:configuration>
Same config file migrated to Log4j v2:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="info"/>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
As of version 2.4, Log4J2 does now, again, support .property files. See here in the documentation for property configuration.
Configuration with Properties
As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1. Like the XML and JSON configurations, properties configurations define the configuration in terms of plugins and attributes to the plugins.
Log4j 2 uses a new configuration file format. You need to use XML (default), JSON (with additional libraries), or even YAML (again, libraries). Check out the documentation.