Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nixing screen output with log4j

Tags:

log4j

One of the irritating things with log4j is that it always wants to dump stuff to the screen. I don't need that if I'm logging to a file. I'm sure it's in how I set up the log4j.properties file. Getting all of this configuration stuff ironed out is frustrating! :-)

For a program I'm currently calling Balancer, this is how I'm doing my logger initialization. Perhaps it is wrong or something.

static Logger log = Logger.getLogger(Balancer.class);

A partial dump of my log4j.properties:

log4j.rootLogger=fatal, stdout
log4j.logger.Balancer=fatal, rollingLog

# I still don't understand how category stuff works yet
log4j.category.Balancer=info, BalancerLog

#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

#### Second appender writes to a file
# Control the maximum log file size
# Archive log files (ten backups here)
log4j.appender.rollingLog=org.apache.log4j.RollingFileAppender
log4j.appender.rollingLog.File=default.log
log4j.appender.rollingLog.MaxFileSize=10000KB
log4j.appender.rollingLog.MaxBackupIndex=10
log4j.appender.rollingLog.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingLog.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

log4j.appender.BalancerLog=org.apache.log4j.RollingFileAppender
log4j.appender.BalancerLog.File=Balancer.log
log4j.appender.BalancerLog.MaxFileSize=100000KB
log4j.appender.BalancerLog.MaxBackupIndex=10
log4j.appender.BalancerLog.layout=org.apache.log4j.PatternLayout
log4j.appender.BalancerLog.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

I get how the rootLogger sends stuff to the stdout appender. Is there a /dev/null appender? You have to have at least one appender.

Anyway, if nothing else, my basic work-around now is to send screen output to /dev/null. BTW, my Java programs run in a scheduled batch environment (no GUI). Having to clean up spooled files (yes this is on an AS/400) is a bit of a pain, although that can be automated as well.

like image 478
Kelly Beard Avatar asked Aug 06 '12 16:08

Kelly Beard


1 Answers

Is there a /dev/null appender?

Yes.

log4j.appender.devnull=org.apache.log4j.varia.NullAppender
log4j.rootLogger=fatal, devnull

I'm not sure where you got log4j.category.* from but it's not something I've seen before, I would stick to just using appender and logger.

log4j.logger.Balancer=fatal, rollingLog, BalancerLog

would send fatal level messages for the logger named Balancer (with no package prefix) to both the rollingLog and BalancerLog appenders. If you change the logger level to info

log4j.logger.Balancer=info, rollingLog, BalancerLog

then it would send messages of level info and above to both appenders. You can't restrict it so that BalancerLog gets info and above but rollingLog gets only fatal messages on a per-logger basis, but you can set a threshold on the rollingLog appender so that it only records fatal messages (regardless of the logger they came from)

log4j.appender.rollingLog=org.apache.log4j.RollingFileAppender
log4j.appender.rollingLog.Threshold=fatal
log4j.appender.rollingLog.File=default.log
# other parameters as before
like image 62
Ian Roberts Avatar answered Oct 25 '22 14:10

Ian Roberts