I'd like to have 2 different log4j ConsoleAppenders defined with different layouts. I tried the following:
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{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.stdoutMDC=org.apache.log4j.ConsoleAppender
log4j.appender.stdoutMDC.Target=System.out
log4j.appender.stdoutMDC.layout=org.apache.log4j.PatternLayout
log4j.appender.stdoutMDC.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L (hibernateLoadPlanWalkPath->%X{hibernateLoadPlanWalkPath}) - %m%n
However, when I attempt to use these appenders I am running into problems. I have the first appender attached to root and then attempt to attach the second to certain ancestor loggers:
log4j.rootLogger=info, stdout
log4g.logger.org.hibernate.loader.plan=trace, stdoutMDC
log4g.additivity.org.hibernate.loader.plan=false
log4g.logger.org.hibernate.persister.walking=trace, stdoutMDC
log4g.additivity.org.hibernate.persister.walking=false
The trouble I am having is that the messages from both of those ancestor loggers end up going to the stdout appender and not the stdoutMDC appender. I tried both with and without disabling additivity, but no difference.
Any ideas?
please try this.
It may help out you
Note : %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
log4j.appender.consoleAppender.layout.ConversionPattern = %-4r [%t] %5p %c %x - %m - %X{userName}%n
log4j.rootLogger = DEBUG, consoleAppender
Related
I am using log4j (1.2.15) for logging in my application, where I have two file appenders for two different packages (darius.log4j.sql and darius.log4j.net) and a console appender. Its configuration is this:
# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FILE A
log4j.appender.A=org.apache.log4j.FileAppender
log4j.appender.A.File=A.log
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FILE B
log4j.appender.B=org.apache.log4j.FileAppender
log4j.appender.B.File=B.log
log4j.appender.B.layout=org.apache.log4j.PatternLayout
log4j.appender.B.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#
log4j.rootLogger=ERROR, CONSOLE
#
log4j.logger.darius.sql=DEBUG, A
log4j.logger.darius.net=INFO, B
What I need now is to add a new file appender programmatically if a specific data combination appears. I konw how to define this new file appender (ex C) but I don't know how to set the level and the new appender to a third package:
log4j.logger.darius.ws=INFO, C
where the C is the third file appender.
Any hit?
Simple! After you create your third appender (C) get a logger for your namespace (darius.ws), set the INFO level for this logger. At the end just append this C appender to the logger.
FileAppender fileAppender = new FileAppender();
fileAppender.setName("C");
fileAppender.setFile("C.log");
fileAppender.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fileAppender.setThreshold(Level.INFO);
fileAppender.setAppend(true);
...
Logger someLogger = Logger.getLogger("darius.ws");
someLogger.setLevel(Level.INFO);
someLogger.addAppender(fileAppender);
Best regards
log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.FileAppender.
log4j:WARN No such property [maxFileSize] in org.apache.log4j.FileAppender.
What does this mean, what causes it, and how should you fix it?
# initialize root logger with level ERROR for stdout and file
log4j.rootLogger=DEBUG,stdout,file
# set the log level for these components
log4j.logger.com.endeca=DEBUG
log4j.logger.com.endeca.itl.web.metrics=INFO
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# use a simple message format
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# add a FileAppender to the logger file
log4j.appender.file=org.apache.log4j.FileAppender
# create a log file
log4j.appender.file.File=E:\\temp\\GAPN.log
# take backup periodically
log4j.appender.file.MaxFileSize=2KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
# use a more detailed message pattern
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
The appender 'file' is of type FileAppender. FileAppender does not have a field MaxFileSize. This is a property of RollingFileAppender class, which is a subclass of FileAppender and allows for log rotation based on size.
I am trying to configure my Jboss logs in the following format in Jboss 6.4
<DATE> <TIME> <LEVEL> [<CLASS>] (<THREAD>) MESSAGE
*Example*
2017-03-09 05:09:05,961 INFO [com.app.checker.CallInterceptor] (ajp-/0.0.0.0:22329-5 api~55C41D7CDD44XXXX646.api GET /call/get) processing the request
My current logger properties file have the following entry
formatter.PATTERN.pattern=%d{HH\:mm\:ss,SSS} %-5p [%c] (%t) %s%E%n
Please suggest following edits in above entry to match the required log format above.
This is very similar to log4j synthax.
%-5p for level
%t for thread
%c for class
%M is for function where issue originates
%L is the line
This pattern %d{HH:mm:ss,SSS} %-5p [%c.%M:%L] (%t) %s%E%n works for me:
12:19:06,131 DEBUG [test.testConfig:731] (main) Test message to show up on the console.
Then to add the date with time zone if you want:
%d{yyyy-dd-MM HH:mm:ss,SSS Z} %-5p [%c.%M:%L] (%t) %s%E%n
I have a problem with logging in tomcat.
I use a DailyRollingFileAppender, but the problem is: the file with date log.log.2015-05-25 have data from 2015-05-26... it happens with all files.
This is my log4j.properties:
log4j.appender.GDebug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.GDebug.File=/opt/tomcat/logs/log.log
log4j.appender.GDebug.DatePattern='.'yyyy-MM-dd
log4j.appender.GDebug.layout=org.apache.log4j.PatternLayout
log4j.appender.GDebug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %X{username} %c{1} %m%n
log4j.appender.GDebug.Threshold=DEBUG
Thanks in advance!
I am trying to get my log4j property working, I messed it up I think.
log4j.rootLogger=ALL
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{ddMMMyy HH:mm:ss,SSS} %-4r [%t] %-5p %c{1} %x - %m%n
log4j.logger.org.hibernate=ERROR
log4j.debug=TRUE
log4j.logger.net.spy.memcached=ERROR
log4j.logger.com.mcruiseon.server=ALL
My package is com.mcruiseon.server
You might try something like this:
log4j.rootLogger=ERROR, CA
log4j.logger.com.mcruiseon.server=INFO, CA
log4j.additivity.com.mcruiseon.server=false
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{ddMMMyy HH:mm:ss,SSS} %-4r [%t] %-5p %c{1} %x - %m%n
This does: define a CA console logger, tell rootLogger to log ERROR and above to CA, define a logger for your package that logs INFO and above to CA, and sets its additivity to false to prevent messages from appearing in more than one appender (which is no issue here since you only have CA, but will become one once you route your logger to its own file appender).