Logging using ning's asyncronous http client library - java

How do we setup logging using Ning's Asynchronous HTTP client ?
There are no pointers to it in the documentation except the fact that it is dependent on Log4J.
My project uses log4j and below shows the content of our log4j.properties file.
log4j.rootCategory=DEBUG, STDOUT
log4j.logger.com.ning.http.client=DEBUG
# Configure the Standard Out Appender
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.target=System.out
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p (%F:%L) %m%n
I am aware that ning apache HTTP client uses slf4j for logging and hence we have log4j-over-slf4j.jar on the project classpath instead of log4j.jar.
Please help.
Thanks in advance.

Related

Disabling Log4J logger on ALL external libraries

I am developing a project with lots of external libraries that most are using Log4J. Currently, we are identifying them and turning their log level to OFF one by one in log4j.properties. I was wondering if there is a more efficient solution that one can turn off logger on all packages except one?
I researched a lot on StackOverflow, but almost all answers are revolving around explicitly turning off the loggers for external libraries individually, which is not an efficient solution in our case.
For reference, our log4j.properties looks like this:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout
# here we exclude loggers for external packages one by one
log4j.logger.com.github.external1 = OFF
log4j.logger.com.github.external2 = OFF
log4j.logger.org.external3 = OFF
....
log4j.logger.com.example.external100 = OFF
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=./log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
Log4j organizes it's loggers in a hierarchy. If you turn off the root logging you deactivate all but can still turn on your well known packages, which are a lot less than all the others. Here is an example based on yours:
# Define the root logger with appender file and turn all the categories off
log4j.rootLogger = OFF, stdout
# Now turn on only my packages
log4j.logger.com.mycompany = DEBUG
# here we exclude loggers for external packages one by one
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=./log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
However be aware this configuration file works on Log4j 1. You want to migrate to Log4j 2.17.x to mitigate that famous vulnerability.

How do I control logging in 3rd party libraries

I have a Tomcat server running a Spring-based servlet.
I've set up [project root]/src/log4j.properties file as below:
# Root logger option
log4j.rootLogger=WARN, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss} %m [%c{3}:%L]%n
log4j.logger.com.martincarney.bugTracker=DEBUG
log4j.logger.com.martincarney.bugTracker.controller=ERROR
This correctly logs my own code just fine, but doesn't seem to have any effect on logging from within the various libraries I'm using. For example, I still get INFO logs from org.apache.* to the Eclipse console error stream during Tomcat startup, even if I add log4j.logger.org.apache=WARN to my log4j.properties.
I'm using slf4j-api and slf4j-log4j jars, obtained through Maven.
How can I take control of logging levels and targets outside my own code?
Some libraries use other logging frameworks like java.util.logging.
You could redirect logging with SLF4J, see SLF4J - Bridging legacy APIs:
Redirection for Jakarta Commons Logging:
To ease migration to SLF4J from JCL, SLF4J distributions include the jar file jcl-over-slf4j.jar. This jar file is intended as a drop-in replacement for JCL version 1.1.1. It implements the public API of JCL but using SLF4J underneath, hence the name "JCL over SLF4J."
Redirection for java.util.Logging (SLF4J API):
Installation via logging.properties configuration file:
// register SLF4JBridgeHandler as handler for the j.u.l. root logger
handlers = org.slf4j.bridge.SLF4JBridgeHandler
For configuration of java.util.Logging see JUL API.
Some libraries like Apache CXF supports more than one logging framework, see Apache CXF - Debugging and Logging .

Set JCS log level to ERROR - log4j

I have implemented JCS in my J2ee application which uses log4j for logging.
My Requirement
Set the application rootLogger in DEBUG level and jcs logs in ERROR mode.
What is tried
Tried the following in log4j properties
log4j.category.org.apache.common.jcs=ERROR
log4j.logger.org.apache.common.jcs=ERROR
But nothing is affecting the logging.
Whenever the cache access happens, it logs a bunch of returning first node messages.
Note : I am using the latest JCS commons-jcs-core-2.0-beta-1.jar
This is my complete log4j.properties
log4j.rootLogger=DEBUG, A1
# Use Console Appender for development
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern= %d [%t] %-5p - %m%n
log4j.logger.net.sf.jasperreports=ERROR
log4j.category.org.apache.common.jcs=ERROR
I missed an "s" in commons. Correcting it solved my problem.
Solution
Both the below statements can be used to configure JCS log level in log4j.
log4j.category.org.apache.commons.jcs=ERROR
log4j.logger.org.apache.commons.jcs=ERROR
JCS has a dependency on commons-logging for logging, and not log4j. Hence, log4j configurations won't matter unless you redirect commons-logging to log4j. To do that, create a file named commons-logging.properties and add the following in that.
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4jLogger

Config SLF4J in jar file

I have my own jar file which is used as a library for my main application. I need to set SLF4J as my logger and configure it in my jar.
Write logs into a file
Set other configurations in a property file
When it is googled, there are samples for web apps. Any one let me know how to do above functionalities or mention any useful resource.
For a library, you should only include slf4j-api.jar. This means that in your code, you should only use classes within SLF4J's API, i.e. LoggerFactory and Logger.
Your library should not define anything else regarding logging. It's the responsibility of the application that uses your library to define the underlying logging implementation (logback, log4j, jcl, etc) and include the necessary bindings, as well as the underlying logging platform's configuration, such as a logback.xml file.
Please refer to the SLF4J manual for further reference.
To log something using slf4j, you must include the slf4j-api jar in your application. This is the bare minimum that is nessesary for slf4j logging. If using maven, it can be included in the pom as shown below
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
However the default logging behavior in slf4j is very simple. In nearly all circumstances you will want to send slf4j logs to a logging framework, which can perform more complex logging. There are several jars that can be included to accomplish this. I use log4j, so the examples below are for that framework. In order to log over log4j, slf4j-log4j12 jar is needed.
Pom dependancy
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
From there all slf4j logs will be redirected to log4j, which will work normally. Configure it through a log4j.properties file. The below example log4j.properties will log things both to the console, and to the /tmp/logfile.log file. remove the console appender to only log to the file.
# Root logger option
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/logfile.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
If you want to log with a different framework, include the appropriate jar, and configure the underlying framework as you otherwise would.
SLF4J is not a logger, therefore you cannot configure anything. Use Logback.

Can't configure Jetty and log4j

Recently I inherited a project and having some hard time configuring the logging in the sense I can't seem to configure it at all!
So let me first give you the context:
It runs under Jetty 9.2
It uses the Apache Commons logging library
It has a Maven dependency on log4j which I assume the apache commons will pick that up and use it.
There are no existing commons-logging.properties or log4j.properties so I assume it falls back to defaults.
There is no jetty-logging.properties either.
So here is my problem, it currently does not log the timestamp, the output is like this:
5027 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/css/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
and:
912562 [qtp760972690-15] DEBUG com.xxx.xx.Class - Some Action Log Msg
No idea what those fields before the DEBUG are. I had previously worked with Tomcat, sl4j and pretty sure Apache Commons logging too.
So I thought I would config files both 'commons-logging.properties' and 'log4j.properties' in the WAR file WEB-INF/classes.
Now it recognises and picks up the 'commons-logging.properties' but the 'log4j.properities' seems to have no effect at all.
So am I missing a step here, have I forgotten something? Is this a Jetty issue because it uses sl4j and I'm not?
Here is is m commons-logging.properties:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.configuration=log4j.properties
And here is my log4j.properties:
# Set root logger level to DEBUG.
log4j.rootLogger=DEBUG,stdout
# stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Another note is that log lines from Jetty itself do have the nice date-stamp I want:
2014-09-06 19:36:19.356:INFO:/:main: Initializing Spring FrameworkServlet 'appServlet'
But not my main application logging.

Categories