How to specify Log4J 2.x config location?

You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) (see source here) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.) Be aware that this class is not part of the public API so your code may break with any minor release. For completeness, you can also specify the location of … Read more

Getting Exception org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext

Remove below jar’s from class path and it should fix the issue – log4j-to-slf4j-2.0.2.jar log4j-to-slf4j-2.0.2-sources.jar log4j-slf4j-impl-2.0.2.jar log4j-slf4j-impl-2.0.2-sources.jar I was able to replicate and fix the issue after downloading apache-log4j-2.0.2 from http://www.apache.org/dyn/closer.cgi/logging/log4j/2.0.2/apache-log4j-2.0.2-bin.zip.

Log4j, configuring a Web App to use a relative path

Tomcat sets a catalina.home system property. You can use this in your log4j properties file. Something like this: log4j.rootCategory=DEBUG,errorfile log4j.appender.errorfile.File=${catalina.home}/logs/LogFilename.log On Debian (including Ubuntu), ${catalina.home} will not work because that points at /usr/share/tomcat6 which has no link to /var/log/tomcat6. Here just use ${catalina.base}. If your using another container, try to find a similar system property, … Read more

Are there technical reasons to prefer using logback instead of log4j?

You should use SLF4J+Logback for logging. It provides neat features like parametrized messages and (in contrast to commons-logging) a Mapped Diagnostic Context (MDC, javadoc, documentation). Using SLF4J makes the logging backend exchangeable in a quite elegant way. Additionally, SLF4J supports bridging of other logging frameworks to the actual SLF4J implementation you’ll be using so logging … Read more

log4j support in Android

Actually using slf4j turned out a remarkably painless process for me, and it seems the common case, at least for libraries that use straightforward log4j features. You don’t really need to swap slf4j in for log4j, only add two slf4j libraries to your project from http://www.slf4j.org/download.html: — the slf4j library for Android (currently slf4j-android-1.6.1-RC1.jar) — … Read more

Production settings file for log4j?

Log4j 1.2 is vulnerable to deadlocks when toString() produces nested logging. See old aged still unresolved issues like Log4J can create deadlock conditions (concurrent package donation) and Deadlock with RollingFileAppender. It also has performance-killing lock synchronization issues under heavy concurrent load. Like Category callAppenders synchronization causes java.lang.Thread.State: BLOCKED and Move org.apache.log4j.Category to reentrant read/write locks. … Read more