Log4J: Log Output of a Specific Class to a Specific Appender

log4j: Log output of a specific class to a specific appender

An example:

log4j.rootLogger=ERROR, logfile

log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.datePattern='-'dd'.log'
log4j.appender.logfile.File=log/radius-prod.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n

log4j.logger.foo.bar.Baz=DEBUG, myappender
log4j.additivity.foo.bar.Baz=false

log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.datePattern='-'dd'.log'
log4j.appender.myappender.File=log/access-ext-dmz-prod.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n

log4j configuration does not work for a specific class

Thanks a lot for the hint. That pointed me to the correct solution. Apparently the authors of that library defined their logger as something like FooLogging. So I changed these two lines:

log4j.logger.io.opentracing.impl.LoggingEventBuilder=INFO, ul 
log4j.additivity.io.opentracing.impl.LoggingEventBuilder=fal‌​se

to

log4j.logger.FooLogging=INFO, ul 
log4j.additivity.FooLogging=false

and that solved the problem.

In java, how to write logs to a specific file appender of log4j?

This might help:-

log4j.appender.successLog=org.apache.log4j.FileAppender
log4j.appender.successLog.File=${dd.log.dir}/success.log

log4j.appender.tempLog=org.apache.log4j.FileAppender
log4j.appender.tempLog.File=${dd.log.dir}/Temp_error.tmp_log

log4j.category.successLogger=INFO, successLog
log4j.additivity.successLogger=false

log4j.category.tempLogger=INFO, tempLog
log4j.additivity.tempLogger=false

Access them like:-

static final Logger successLog = Logger.getLogger("successLogger");
static final Logger tempLog = Logger.getLogger("tempLogger");

How to record log information from a specific class into a specific file?

Here's a very basic example of a log4j.properties file that can get you started:

log4j.logger.com.yourpackage.yourclass=INFO, YourLoggerName

log4j.appender.YourLoggerName=org.apache.log4j.RollingFileAppender
log4j.appender.YourLoggerName.File=/path/to/logfile/forthisappender
log4j.appender.YourLoggerName.MaxFileSize=5M
log4j.appender.YourLoggerName.MaxBackupIndex=10
log4j.appender.YourLoggerName.layout=org.apache.log4j.PatternLayou
log4j.appender.YourLoggerName.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n

There's more complete info here

log4j: output file depending on source calling class

I see three possible ways:

  • if it is enough to log all messages into the same file but prefix them differently depending on context, so that they become easily filterable, you could use a nested diagnostic context, as described in a previous answer of mine,
  • if you absolutely need to have separate log files, you could subclass your class with two different loggers and associated appenders, as Xavier suggested (just his solutions is unfortunately not working as it is),
  • or try this alternative solution.

A working implementation of a subclassing solution would be something like this:

public class SomeCalculationLogic {
protected abstract Log getLog();

public void doCalculation() {
getLog().info("doing calculations...");
...
}
}

public class BackgroundCalculationLogic extends SomeCalculationLogic {
private static Log log = LogFactory.getLog(BackgroundCalculationLogic.class);

protected Log getLog() {
return log;
}
}

public class UserRequestCalculationLogic extends SomeCalculationLogic {
private static Log log = LogFactory.getLog(UserRequestCalculationLogic.class);

protected Log getLog() {
return log;
}
}

How to Overwrite log4j Settings For A Specific Class

Add the following line to your log4j.properties:

log4j.logger.org.apache.hadoop.hbase.tool.Canary=INFO, console

You can change INFO to whatever logging level you need for this class.

If you also want to prevent the class from using other appenders, change the additivity for it by adding the following line:

log4j.additivity.org.apache.hadoop.hbase.tool.Canary = false


Related Topics



Leave a reply



Submit