Configuring log4j2 and log4j using a single log4j2 xml file

I would recommend using the log4j-1.2 adapter that is included in the log4j2 distribution. That way, any libraries coded to the log4j-1.2 API will work with log4j2 without any code changes. Your classpath should include: log4j-api-2.6.1.jar log4j-core-2.6.1.jar log4j-1.2-api-2.6.1.jar log4j2.xml Your classpath should not include: log4j-1.2.x.jar log4j.properties or log4j.xml (these will be ignored by log4j2 anyway) … Read more

How to log using log4j to local file system inside a Spark application that runs on YARN?

It looks like you’ll need to append to the JVM arguments used when launching your tasks/jobs. Try editing conf/spark-defaults.conf as described here spark.executor.extraJavaOptions=-Dlog4j.configuration=file:/apps/spark-1.2.0/conf/log4j.properties spark.driver.extraJavaOptions=-Dlog4j.configuration=file:/apps/spark-1.2.0/conf/log4j.properties Alternatively try editing conf/spark-env.sh as described here to add the same JVM argument, although the entries in conf/spark-defaults.conf should work. If you are still not getting any joy, you can explicitly … Read more

log4j: package-specific logging

You have to create two new appenders and set additivity accordingly. log4j.appender.FRED=org.apache.log4j.RollingFileAppender log4j.appender.FRED.File=/path/to/fred.log log4j.appender.FRED.layout=org.apache.log4j.PatternLayout log4j.appender.DEREK=org.apache.log4j.RollingFileAppender log4j.appender.DEREK.File=/path/to/derek.log log4j.appender.DEREK.layout=org.apache.log4j.PatternLayout log4j.additivity.com.myname.fred=false log4j.additivity.com.myname.derek=false log4j.logger.com.myname.fred=DEBUG, FRED log4j.logger.com.myname.derek=DEBUG, DEREK Update: Just check if you need to add the below line. log4j.rootLogger=DEBUG, R, FRED, DEREK Where R is your regular log file that logs everything except FRED and DEREK.

Log4j formatting: Is it possible to truncate stacktraces?

You can use a EnhancedPatternLayout in log4j to format your stacktraces. See http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/EnhancedPatternLayout.html, specifically the section about the “throwable” pattern in the pattern table. Note that support for the %throwable{n} support is rather new and requires at least log4j 1.2.16 (which is the latest at time of writing) For tracking purposes, this is the ticket … Read more

Configuring RollingFileAppender in log4j

I had a similar problem and just found a way to solve it (by single-stepping through log4j-extras source, no less…) The good news is that, unlike what’s written everywhere, it turns out that you actually CAN configure TimeBasedRollingPolicy using log4j.properties (XML config not needed! At least in versions of log4j >1.2.16 see this bug report) … Read more

Eclipse: Referencing log4j.dtd in log4j.xml

I know this question has been answered, but I’d like to provide my slightly different alternative: <!DOCTYPE log4j:configuration PUBLIC “-//APACHE//DTD LOG4J 1.2//EN” “http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd”> It is similar to @FrVaBe’s response, but on the plus side, does not require any further Eclipse configuration (i.e., if you’re sharing your project with others, or have a large team, it’s … Read more

LOG4J: Modify logged message using custom appender

I’m not entirely sure why creating a new LoggingEvent is so onerous. This seems to work for me: package test.logging; import org.apache.log4j.DailyRollingFileAppender; import org.apache.log4j.spi.LoggingEvent; public class MyDailyRollingFileAppender extends DailyRollingFileAppender { @Override protected void subAppend(LoggingEvent event) { String modifiedMessage = String.format(“**** Message modified by MyDailyRollingFileAppender ****\n\n%s\n\n**** Finished modified message ****”, event.getMessage()); LoggingEvent modifiedEvent = new LoggingEvent(event.getFQNOfLoggerClass(), … Read more

Override logging in WildFly

You can use logback to configure logging in your application. You can’t use logback to configure logging for the server. To use logback in your configuration you’ll need to change the add-logging-api-dependencies to false or create a jboss-deployment-structure.xml that excludes the subsystem. You’ll also need to include logback and slf4j in your deployment. The first … Read more