Log4j multiple pattern layout - log4j

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

Related

log4j.properties output FileName configuration should be : path+DD-MM-AAAA - Filename.log

I'm trying to have a log output file with a path like .log/ DD-MM-AAAA - Filename.log
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/%d{yyyy-MM-dd HH:mm:ss} - logTest.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Actually it doesn't work. If I remove %d{yyyy-MM-dd HH:mm:ss} the output is : logs/logTest.log
But i need to have the date on my output file name.
What I want is :logs/DD-MM-YYYY - logTest.log
Can you please show me how to do that?
Try below snippet, it will add date to generated log file next day.
# configure the root logger
log4j.rootLogger=INFO, DAILY, stdout
#configure the daily rolling file appender
log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.File=/opt/ZudioFeedbackApplication/log/zudio.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.DAILY.DatePattern='.'yyyy-MM-dd
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.conversionPattern=%d{HH:mm} [%p] %c:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
The Generated log file will be like.
zudio.log (you can change name and path), and next day it will rename itself with zudio.log.2019-11-19. For your requirement try it by changing DatePattern

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.

Log4j 1.2 custom loglevel with levelmatchfilter in properties file

I tried to implement a levelmatchfilter for a custom loglevel and configure it within a properties file.
Used the template for the chrunchify loglevel for creating mine:
http://crunchify.com/java-how-to-create-your-own-logging-level-in-log4j-configuring-log4j/
Configured my properties file as follwing:
log4j.rootLogger.level=DEBUG
# LOG4J daily rolling log files configuration
log4j.rootLogger=debug, DebugAppender, InfoAppender, ErrorAppender, ChangeAppender
#DebugAppender (Debug)
log4j.appender.DebugAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DebugAppender.File=./log/debug/ngpmt_debug.log
log4j.appender.DebugAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
log4j.appender.DebugAppender.Threshold=DEBUG
#InfoAppender (Loud)
log4j.appender.InfoAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.InfoAppender.File=./log/loud/ngpmt_info.log
log4j.appender.InfoAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
log4j.appender.InfoAppender.Threshold=INFO
#ErrorAppender (Silent)
log4j.appender.ErrorAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorAppender.File=./log/silent/ngpmt_error.log
log4j.appender.ErrorAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
log4j.appender.ErrorAppender.Threshold=WARN
#ChangeAppender (Changes)
log4j.appender.ChangeAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ChangeAppender.File=./log/silent/ngpmt_changes.log
log4j.appender.ChangeAppender.File.filter.a=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.ChangeAppender.File.filter.a.LevelToMatch=CHANGE#at.xtention.pqm.ngpmt.gui.helpers.Change
log4j.appender.ChangeAppender.File.filter.a.AcceptOnMatch=true
log4j.appender.ChangeAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.ChangeAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ChangeAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
I have 3 log-files with different scopes. Additional to that i want a log-file where only CHANGE-Level messages are logged to.

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.

simple log4j configuration issue

i can't remember how to do this,
i have this log config
# ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=DEBUG, R
log4j.rootLogger=INFO, A
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.MaxFileSize=100mb
log4j.appender.R.MaxBackupIndex=1000
log4j.appender.R.File=${catalina.base}/logs/server.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.logger.org.hibernate.type.BasicTypeRegistry=INFO
log4j.logger.com.opensymphony.xwork2.config.providers.XmlConfigurationProvider=INFO
i want appender A to be info and appender R to be debug. can you remind me how to do that?
log4j.rootLogger=DEBUG, R, A
log4j.appender.A.Threshold=INFO

Resources