How to enable logging of HTTP headers via log4j - log4j

I am trying to enable logging of incoming HTTP headers on my server which uses Axis2, is there a way to do it via log4j?

Axis2 uses Apache Commons Logging, a pluggable logging API. Now the question is: which logging library does your server use? If it's log4j, then you probably can make it log the Axis2 traffic — try setting
log4j.logger.org.apache.axis2.transport.http.server.wire=DEBUG
in the log4j.properties file of the application server.
If your server uses other lib, however, redirecting axis output to log4j may involve playing with classloaders as described in Commons Logging FAQ — so that Commons Logging and Log4j are loaded by the same classloader, you'll need to deploy both libraries with your EAR and reverse classloading policy to "parent last". Chances are other libraries from your application won't run with this setting.

I know it is old post, but I would like to share my solution to help other as I just faced the same case recently. I have configured the following in log4j.xml
<appender name="fileout" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/soapLog/axis2.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %m%n" />
</layout>
</appender>
<logger name="org.apache.axis2.enterprise">
<level value="debug" />
<appender-ref ref="fileout" />
</logger>
<logger name="de.hunsicker.jalopy.io">
<level value="debug" />
<appender-ref ref="fileout" />
</logger>
<logger name="httpclient.wire">
<level value="debug" />
<appender-ref ref="fileout" />
</logger>
<logger name="org.apache.commons.httpclient">
<level value="debug" />
<appender-ref ref="fileout" />
</logger>
<logger name="org.apache.axis2.transport.http.server.wire">
<level value="debug" />
<appender-ref ref="fileout" />
</logger>
Hope this may help.

Related

Invoke log4net appender only for Warn and above

Quiet new to log4net appender.
I have created a new log4net appender which I would like to get called only if the level is Warn or Fatal or Error.
I tried below but doesn't seem to be working i.e. it gets called for Info as well.
<root>
<priority value="ERROR" />
<appender-ref ref="MyBrandNewAppender" />
</root>
Also what's the difference between above and below:
<logger name="bla" additivity="false">
<level value="ERROR" />
<appender-ref ref="MyBrandNewAppender" />
</logger>
Thanks
In the root tag you are using priority.
<priority value="ERROR" />
I am using level which works for me.
<level value="ERROR" />

I don't understand why this log4j.xml is wrong

I want to log into Cassandra db with log4j.
So I write this log4j.xml configuration:
<appender name="DBlog" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="URL" value="jdbc:cassandra://localhost:9160/MyKeyspace" />
<param name="Driver" value="org.apache.cassandra.cql.jdbc.CassandraDriver" />
<param name="User" value="" />
<param name="Password" value="" />
<layout class="..."/>
</appender>
<logger name="logger">
<level value="info"/>
<appender-ref ref="DBlog" />
</logger>
Logging is doing well. But, log4j generates this message:
log4j:WARN No appenders could be found for logger (org.apache.cassandra.cql.jdbc.CassandraDriver).
log4j:WARN Please initialize the log4j system properly.
I don't understand what is wrong.
If anyone knows the answer, please help.
You need to define one more logger:
<logger name="org.apache.cassandra.cql.jdbc">
<level value="info"/>
<appender-ref="DBlog" />
</logger>
And I think it's a good practice to define root logger in any cases.
<root>
<level value="info"/>
<appender-ref ref="DBlog"/>
</root>

Log4j configuration

I have two appenders, file and console, in my project. I would like to configure my application to perform as such:
all loggers with name "my.app.*":
1. log events DEBUG and higher to fileA
2. log events DEBUG and higher to fileB
all other loggers:
1. log events WARN and higher to fileA
2. log events DEBUG and higher to fileB
Ideally, the configuration would look something like this:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="fileA" class="org.apache.log4j.FileAppender">
<!-- configuration -->
</appender>
<appender name="fileB" class="org.apache.log4j.FileAppender">
<!-- configuration -->
</appender>
<logger name="my.app" additivity="false">
<level="DEBUG"/>
<appender-ref ref="fileA"/>
</logger>
<logger name="" additivity="true">
<level="DEBUG"/>
<appender-ref ref="fileB"/>
</logger>
<root>
<level="WARN"/>
<appender-ref ref="fileA"/>
</root>
</log4j>
However, this setup causes loggers named "my.app" to only log to console, and all other loggers to log to console on WARN and above. Essentially, <logger name=""> is being ignored. Is there another way to emulate this behavior with log4j?
PS. I apologize for the poor formatting, really struggling to get this to work tonight :/
You need:
<logger name="my.app" additivity="false">
<level="DEBUG"/>
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</logger>
<root>
<appender-ref ref="console-warn"/>
<appender-ref ref="file-debug"/>
</root>
For the root-appender you need two new console/file appenders, that have the desired level restrictions.
<appender name="file-debug" class="org.apache.log4j.FileAppender">
<param name="Threshold" value="DEBUG"/>
</appender>
<appender name="console-warn" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="WARN"/>
</appender>

log4j:ERROR Attempted to append to closed appender named [..]

I am getting following errors on my console repeatedly
log4j:ERROR Attempted to append to closed appender named [ConsoleAppender].
log4j:ERROR Attempted to append to closed appender named [FixedWindowRollingFile].
used log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender class="org.apache.log4j.rolling.RollingFileAppender" name="FixedWindowRollingFile">
<param name="Append" value="true"/>
<param name="ImmediateFlush" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="fileNamePattern" value="logs/StandardizeAccountService.%i.log"/>
<param name="minIndex" value="1"/>
<param name="maxIndex" value="10"/>
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="1002400"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout"/>
</appender>
<logger name="com.arosys" additivity="false" >
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FixedWindowRollingFile"/>
</logger>
<root>
<priority value="INFO"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="FixedWindowRollingFile"/>
</root>
</log4j:configuration>
please help me where the problem.
I got the same error:
log4j:ERROR Attempted to append to closed appender named [rollingFileAppender].
In my log4j.xml
I have two loggers with the same name like below
<logger name="java.sql.PreparedStatement" additivity="false">
<level value="INFO"/>
<appender-ref ref="rollingFileAppender"/>
</logger>
<logger name="java.sql.PreparedStatement">
<level value="INFO"/>
<appender-ref ref="rollingFileAppender"/>
</logger>
I removed the duplicate, it worked.
I've answered similar question here: https://stackoverflow.com/a/9973283/340290
In my case, I've two log4j.properties available to the Log4J: one via placing it in classpath and other being loaded programmatically (using PropertyConfigurator.configure(..)).
And in the two files, I've ConsoleAppender registered with same name stdout and used for same category twice (one per each properties file). Removing config or the properties file solved my issue.
One could overwrite the configuration using:
BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(props);
Just to clarify because I was mislead by MaDa answer, additivity=false redirects the output to another place than the default (root logger) and NOT to the default.
See http://logging.apache.org/log4j/1.2/manual.html chapter "Appenders and Layouts"
This may also mean that you already have your server running, and you're trying to run it again. The second instance can't write to the log file because it's already open in your server.
solution: check to see if your server is already running, and restart it if neccessary.
In my case, I added by mistake 2 "root" elements.
I'm not saying this is the cause of the behaviour you describe (but it might), but this part:
<logger name="com.arosys" additivity="false" >
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FixedWindowRollingFile"/>
</logger>
is pointless. Normally, you'd set a non-additive logger to redirect it somewhere else than the default place (your root logger), but you still send the output to the default place. You might as well delete this fragment.

Apache commons HTTPClient and log4j.xml

I'm using Apache commons HTTPClient with Apache Axis 1.5 and I'm trying to log the messages exchanged when making Web Service calls by enabling org.apache.commons.httpclient to DEBUG and httpclient.wire to DEBUG. However, this doesn't work. Mentioned below is my log4j.xml - can someone help me?
Thanks
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="test.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c:%L - %m%n"/>
</layout>
</appender>
<logger name="org.apache.commons.httpclient">
<level value="DEBUG"/>
</logger>
<logger name="httpclient.wire">
<level value="DEBUG"/>
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="rolling"/>
</root>
</log4j:configuration>
You need to have the log level set at ALL, not DEBUG
<logger name="httpclient.wire">
<level value="ALL"/>
</logger>
Try putting a priority value set to DEBUG to your <root> node in log4j.xml
<root>
<level value="DEBUG" />
<priority value="DEBUG" />
<appender-ref ref="rolling"/>
</root>

Resources