Nixing screen output with log4j - 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.

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

Related

Write Spark logs to STDOUT instead of STDERR

I have a service that reads the logs from STDOUT for further analysis. It seems like there has been struggle with writing spark logs to STDOUT, by default, log4j sends any kind of log to STDERR.
Is there a way to change this behavior?
What changes need to be made specifically to move logs from STDERR to STDOUT?
Here's what my log4j file looks like:
log4j.rootLogger=INFO, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=stderr
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=....
log4j.appender.FILE.MaxFileSize=5248997
log4j.appender.FILE.MaxBackupIndex=10
log4j.logger.org.spark_project.jetty=WARN
log4j.logger.org.spark_project.jetty.util.component.AbstractLifeCycle=ERROR
log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO
log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO
log4j.logger.org.apache.parquet=ERROR
log4j.logger.parquet=ERROR
When you do a spark submit add 2>&1 at the the end. This means combine stderr(2) and stdout(1) into the stdout stream.
To do it through log4j.properties file, try adding the below properties.
# Log everything INFO and above to stdout
log4j.rootLogger=INFO,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d] %-5p %.26c [%X{testName}] [%X{akkaSource}] - %m%n

Need LOG4J to drop .log from our file name when appending .date extension

Hello we have applications that write to a file called xfiles.log. Log4j does a daily rename to xfiles.log.date, for example xfiles.log.2009-04-12. I would like to remove the '.log' and just have a date extension. For example the new file would be xfiles.2009-04-12. Below is our current log4j setup. Thanks
log4j.rootCategory=INFO, DAILY
log4j.logger.org.springframework=WARN
log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.file=${LOGDIR}/xfiles.log
log4j.appender.DAILY.datePattern='.'yyyy-MM-dd
log4j.appender.DAILY.append=true
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c %m %n

log4j - why am I getting this ludicrous amount of DEBUG even if there's no log4j.properties?

I used to have the following file on /src but it didn't seem to be working. So I took it out (made sure to backup it first) and I am still getting a lot of debug info.
The file is:
# Log levels
# TRACE < DEBUG < INFO < WARN < ERROR < FATAL
log4j.rootLogger=ERROR
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.rollingFile.File=/home/gtl/workspace/hrm_agent/log/log.out
log4j.appender.rollingFile.MaxFileSize=2MB
# Number of backup files
log4j.appender.R.MaxBackupIndex=10
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%
Am I doing something wrong?

Using logAnalyzer with log4j syslog appender

I am trying to send syslog messages from my web application to RSYSLOG and then view them in logAnalyzer.
I manage to see the logs but the following fields are not shown in the main table: Facility, Sevirty, ProcessID. They do appear in the messages, but their columns are empty.
In what format should I send them message so that the logAnalyser will be able to parse it correctly?
I am looking for the right log4j.appender.SYSLOG_LOCAL1.layout.conversionPattern string to place in my log4j.xml file.
Here is my log4j.properties file.
I am using LOCAL1 as the SYSLOG facility:
log4j.rootLogger=DEBUG, R, stdout ,SYSLOG_LOCAL1
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${CATALINA_HOME}logs/mylweb.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=100
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.category.org.springframework=INFO
log4j.category.org.directwebremoting=INFO
log4j.category.org.apache.http=INFO
log4j.appender.SYSLOG_LOCAL1=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG_LOCAL1.threshold=DEBUG
log4j.appender.SYSLOG_LOCAL1.syslogHost=localhost
log4j.appender.SYSLOG_LOCAL1.facility=Local1
log4j.appender.SYSLOG_LOCAL1.facilityPrinting=true
log4j.appender.SYSLOG_LOCAL1.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG_LOCAL1.layout.conversionPattern=%d %p [%c] - %m%n

create log4j depending on level using .properties file

I have to create new file for each level like:
fatel.log for only FATAL
error.log for only ERROR
warn.log for only WARN etc.
how can i achieve this thing in changing this properties file.
Here is my log4j.properties file:
FILE
# level : OFF,FATAL,ERROR,WARN,INFO,DEBUG,ALL
log4j.rootLogger=DEBUG,CONSOLE,A1
log4j.addivity.org.apache=true
# for console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[INFO] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# A1
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=C:/hibernateSqlite.log
log4j.appender.A1.Encoding=UTF-8
log4j.appender.A1.Threshold=DEBUG
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L : %m%n
Though I've never done it, I guess it will be something like:
log4j.appender.AppDebug.filter.DebugFilter=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.AppDebug.filter.DebugFilter.acceptOnMatch=true
log4j.appender.AppDebug.filter.DebugFilter.levelToMatch=DEBUG
The type of the AppDebug appender could be DailyRollingFileAppender, as in your case, with the remaining configuration options along.

Resources