How to format to a specific pattern with Log4j? - log4j

I want to capture the log4j logging comments in the following way. Is it possible ? If so then how should my log4j.properties look like ??
My Message : 2009-05-22 17:07:28
Thanks
Rink

Use the following conversion pattern: "%m: %d{yyyy-MM-dd HH:mm:ss}%n" without the quotes.
If you wanted to use ConsoleAppender, your log4j.properties file would contain:
log4j.rootLogger=debug, CON
log4j.appender.CON=org.apache.log4j.ConsoleAppender
log4j.appender.CON.layout=org.apache.log4j.PatternLayout
log4j.appender.CON.layout.ConversionPattern=%m: %d{yyyy-MM-dd HH:mm:ss}%n

Add following lines in your log4j.properties file:
log4j.appender.stdout.layout.ConversionPattern=MyMessage : %-5p %d{yyyy-MMM-dd HH:mm:ss,SSS} %m %n
log4j.appender.R.layout.ConversionPattern=MyMessage : %-5p %d{yyyy-MMM-dd HH:mm:ss,SSS} %m %n

Related

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 not writing logs to a file

Log4j is not writing the log files to a file but writing to console. I searched through a lot of resources (i think this question already exists) for it but was unable to find a solution.
Log4j version 1.2.17
Below is my log4j configuration:
log4j.rootLogger=INFO, stdout log4j.rootCategory=INFO, stdout , File
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t
%c{20}:%L - %m%n
log4j.category.org.springframework=ERROR
log4j.category.org.springframework.beans.factory=ERROR
log4j.category.com.mypackage=INFO
log4j.logger.org.springframework=ERROR
log4j.logger.org.hibernate=ERROR
log4j.logger.org.hibernate.persister.entity=ERROR
log4j.appender.File = org.apache.log4j.rolling.RollingFileAppender
log4j.appender.File.File=/tmp/mylog.log
log4j.appender.File.ImmediateFlush=true
log4j.appender.File.threshold=INFO log4j.appender.File.Append=true
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.MaxBackupIndex=20
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE} %5p %t
%c{2}:%L - %m%n log4j.appender.File.rollingPolicy =
org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.File.rollingPolicy.FileNamePattern
=/tmp/mylog%d.log
log4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE} %5p %t
%c{2}:%L - %m%n
I have made sure I have permissions to the directory where i am trying to write to.
Can some one please help me out?
Please make the below mentioned changes to your config file.
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.File=/tmp/mylog.log
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.threshold=INFO
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=20
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.FileNamePattern=/tmp/mylog%d.log
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
Hope this works fine.

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

Log4j multiple pattern layout

I'm pretty new with log4j. I want to display log messages with these infos: log timestamp, priority, fully qualified class name, the message, class name only.
I try to display twice the CATEGORY, once with a ConversionPattern, and once with an other ConversionPattern. Here is the piece of my log4j.properties:
log4j.appender.JDBC2.layoutPartsDelimiter=#-#
log4j.appender.B1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n #-#%-4r [%t] %-5p %c{1} %x %m%n
Sadly it does not work. How can I show full qualified class name and class name only in the same log message? TIA.
Francesco
log4j.appender.B1.layout.ConversionPattern is a key here and you can
associate it with only one value. So, if you write like this:
Log4j.appender.B1.layout.ConversionPattern=%some%value%here
Log4j.appender.B1.layout.ConversionPattern=%another%value%here
The later value would just override the previous one
You can define two different appenders and associate them with the
logger you want(the rootLogger as an example):
log4j.rootLogger=debug,console,console2
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %p %C (%F:%M(%L)) -
%m%n
log4j.appender.console2=org.apache.log4j.ConsoleAppender
log4j.appender.console2.target=System.err
log4j.appender.console2.layout=org.apache.log4j.PatternLayout
log4j.appender.console2.layout.ConversionPattern= %p %c{1}
(%F:%M(%L)) - %m%n
Note: the only difference between the two appenders are the
appenders' name and ConversionPattern property.
a more detailed discussion of log4j can be found Short introduction to log4j
It was my mistake. As you can see in my precedent post, Delimiter and PatternLayout refer to different appenders. I change
log4j.appender.JDBC2.layoutPartsDelimiter=#-#
log4j.appender.B1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n #-#%-4r [%t] %-5p %c{1} %x %m%n
into this:
log4j.appender.B1.layoutPartsDelimiter=#-#
log4j.appender.B1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n #-#%-4r [%t] %-5p %c{1} %x %m%n
and everything works fine, when I use #LAYOUT:1# I got the first ConversionPattern (left of delimiter #-#). When I use #LAYOUT:2# I got the second pattern. Thank you.
Francesco

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