How do I control logging in 3rd party libraries - java

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 .

Related

Force a specific logger in Spring Boot

We wanted to switch from standard logback to log4j in Spring Boot 2.4.x.
For most modules, it is a no-brainer by simply removing the dependency of logback, but there are some modules, which are using pact-jvm as a shadow-jar dependency to be able to create the pact files from unit tests.
Now the odyssey begins, because pact needs logback and with the pact-jar on the classpath Spring recognizes the logback class it is looking for and decides to use logback instead of log4j as the logger in the tests.
Is there a possibility to create something like a log bean or a hidden configuration, which allows forcing Spring to use log4j instead of logback, also if logback is on the classpath?
Pact should not be an issue at this point, because the server is started as a standalone server from gradle. We only require the dependency to be able to start everything and to have the classes available.
Thanks!
You may follow these steps to configure log4j with spring boot
Add the log4j dependency to your module
Create logger object in your respective class and use the logger object to log the messages.
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(<YourClassName>.class);
In your spring boot module/project add log4j.properties file in resources so that this will be available on class path at runtime. Following is sample content for the log4j.properties
log4j.rootLogger=INFO, stdout
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
Make sure that the spring boot module bundles the log4j jar when you build.
Once spring boot finds the log4j jar and the log4j.properties on the class path it will be able to initialize the log4j logger for you.
Yes, that's clear. The issue is, that Spring Boots Autoconfiguration is loading the logger for slf4j (sorry forgot that). So here it iterates over the supported candidates in the order:
logback
log4j
apache-log-commons
And it seems that it does not look for beans, it simply looks for the classes by reflection.
If we now start the app for testing together with pact server, Spring Boot resolves an Append* class and thinks, that this has to be the logger of our choice.
It creates the logback instance, which is searching for its config, but then it fails, because the config is for log4j.

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

unable to see spring logging with log4j

I have log4j configured in my j2ee application,
I am able to see log messages from my classes fine - I am unable to see messages from the spring framework (to debug #RequestMapping issues)
I am using latest spring (4.0) and log4j (1.2.17)
This is my log4j.properties:
log4j.appender.H=org.apache.log4j.RollingFileAppender
log4j.appender.H.File=c:/tmp/AppLog.html
log4j.appender.H.MaxFileSize=4000KB
log4j.appender.H.Append=false
log4j.appender.H.encoding=UTF-8
log4j.appender.H.layout=org.apache.log4j.HTMLLayout
log4j.rootCategory=DEBUG, H
I added this to allow Spring to log,
but no output from spring.....
##Spring Framework
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.web=DEBUG
In your log4j.properties file make the following changes:
From:
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.web=DEBUG
To:
log4j.category.org.springframework=DEBUG
log4j.category.org.springframework.web=DEBUG

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.

How to use simpledb appender using Log4j

How to log to amazon simple db using log4j properties
http://code.google.com/p/simpledb-appender/
This site demos how to configure simpledb using slf4j using xml file, but i require the same with log4j properties, is it possible??
Yes, See below
# Root logger option
log4j.rootLogger=INFO, simpledb
# Direct log messages to simpledb
log4j.appender.simpledb=com.kikini.logging.simpledb.SimpleDBAppender
log4j.appender.simpledb.DomainName=your_simpledb_domain
log4j.appender.simpledb.AccessId=your_aws_access_id
log4j.appender.simpledb.SecretKey=your_aws_secret_key

Categories