I am new to this forum. At first glance this community looks awesome.
I have an easy question. I am logging with log4j and am new to tomcat.
I have found the file where the log rules are set, filename: server.log4j-console.properties
I use for it to not log all digester in BATCH.debug.log
Here is the code in server.log4j-console.properties:
# eposerver logger configuration
log4j.rootLogger=DEBUG, debug-file, console
log4j.logger.systrace=DEBUG, systrace-file
log4j.logger.apptrace=DEBUG, apptrace-file
log4j.logger.ntlogger=INFO, nt-events
log4j.logger.httpclient.wire=INFO
log4j.logger.org.apache.commons.httpclient=INFO
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}<%M:%L> - %m%n
log4j.appender.debug-file=org.apache.log4j.RollingFileAppender
log4j.appender.debug-file.layout=org.apache.log4j.PatternLayout
log4j.appender.debug-file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}<%M:%L> - %m%n
log4j.appender.debug-file.File=${EPO_JAVA_DIR}/apps/var/logs/server/debug.log
log4j.appender.debug-file.MaxFileSize=2MB
log4j.appender.debug-file.MaxBackupIndex=500
log4j.appender.systrace-file=org.apache.log4j.RollingFileAppender
log4j.appender.systrace-file.layout=org.apache.log4j.PatternLayout
log4j.appender.systrace-file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}<%M:%L> - %m%n
log4j.appender.systrace-file.File=${EPO_JAVA_DIR}/apps/var/logs/server/systrace.log
log4j.appender.systrace-file.MaxFileSize=1MB
log4j.appender.systrace-file.MaxBackupIndex=100
log4j.appender.apptrace-file=org.apache.log4j.RollingFileAppender
log4j.appender.apptrace-file.layout=org.apache.log4j.PatternLayout
log4j.appender.apptrace-file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}<%M:%L> - %m%n
log4j.appender.apptrace-file.File=${EPO_JAVA_DIR}/apps/var/logs/server/apptrace.log
log4j.appender.apptrace-file.MaxFileSize=1MB
log4j.appender.apptrace-file.MaxBackupIndex=100
log4j.appender.nt-events=org.apache.log4j.nt.NTEventLogAppender
log4j.appender.nt-events.layout=org.apache.log4j.PatternLayout
log4j.appender.nt-events.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}<%M:%L> - %m%n0
log4j.appender.nt-events.source=eposerver
As you can tell I am new to tomcat, I am assuming this can be done in a couple of lines.
Try adding:
log4j.logger.org.apache.commons.digester=INFO
Related
I need to create a new log file once a day, or if log file size become 250 MB. So how can i do this through log4j.properties file? Dividing by day is working, but if size become more than 250MB nothing happens.
My current property file is:
# Root logger option
log4j.rootLogger=INFO,console, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.encoding=UTF-8
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
log4j.appender.rollingFile.MaxFileSize=10MB
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
# configuration to print on console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
As per the following log4j link https://logging.apache.org/log4php/docs/appenders/rolling-file.html, you have to add the following lines.
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
Note:
File Appender should be RollingFileAppender
Triggering Policy should be SizeBasedTriggeringPolicy
EDIT:
# Direct log messages to a log filelog4j.appender.file.encoding=UTF-8
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.File=${LOG_DIR}/application.log
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.MaxFileSize=250MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.append=true
You can refer complete example below.
https://howtodoinjava.com/log4j/log4j-rolling-file-appender/
I have many applications (war, ejb) in same glassfish server, the applications have different log4j configuration
app1.war -> log4j.properties
log4j.rootLogger=info, infcore
log4j.appender.infcore=org.apache.log4j.RollingFileAppender
log4j.appender.infcore.File=/var/log/app1info.log
log4j.appender.infcore.MaxFileSize=20000KB
log4j.appender.infcore.MaxBackupIndex=20
log4j.appender.infcore.layout=org.apache.log4j.PatternLayout
log4j.appender.infcore.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n
app2.jar -> log4j.properties
log4j.rootLogger=info, infoejb
log4j.appender.infoejb=org.apache.log4j.RollingFileAppender
log4j.appender.infoejb.File=/var/log/ejbinfo.log
log4j.appender.infoejb.MaxFileSize=20000KB
log4j.appender.infoejb.MaxBackupIndex=20
log4j.appender.infoejb.layout=org.apache.log4j.PatternLayout
log4j.appender.infoejb.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n
app3.war -> log4j.properties
log4j.rootLogger=info, infoapp2
log4j.appender.infoapp2=org.apache.log4j.RollingFileAppender
log4j.appender.infoapp2.File=/var/log/infoapp2.log
log4j.appender.infoapp2.MaxFileSize=20000KB
log4j.appender.infoapp2.MaxBackupIndex=20
log4j.appender.infoapp2.layout=org.apache.log4j.PatternLayout
log4j.appender.infoapp2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n
I call logger:
protected static final Logger LOG = Logger.getLogger(JAXSecurutyValidation.class);
The problem is that the information is recorded in only one file the ejb, the others files are not created.
How can I fix it?
SOLVED
I find the solution, is very simple update to glasfish 5 and all works fine, thanks.
Maybe this can help you:
String log4jfile = "log4 file path";
PropertyConfigurator.configure(log4jfile);
You can call each log4j file for each application.
I am new to log4j and worked to setup on eclipse and its running. I understood the chain of priority in the levels and this is my properties file config:
log4j.rootLogger=ALL, file, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
My question is
Is it possible to print only INFO and ERROR type of messages in log files with properties file configuration
Finally I got the solution for my requirement. Here I used concept of filters i.e LevelRangeFilter. And for every logging level we defined appender.
log4j.rootLogger=DEBUG, file, console, file1
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file.filter.a.LevelMin=INFO
log4j.appender.file.filter.a.LevelMax=INFO
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=logs/justfortesting.log
log4j.appender.file1.Append=true
log4j.appender.file1.ImmediateFlush=true
log4j.appender.file1.Threshold=DEBUG
log4j.appender.file1.filter.g=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file1.filter.g.LevelMin=ERROR
log4j.appender.file1.filter.g.LevelMax=ERROR
log4j.appender.file1.MaxFileSize=1KB
log4j.appender.file1.MaxBackupIndex=2
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.logger.com.log4j=DEBUG, file, console, file1
log4j.additivity.com.log4j=false
If any one suggest to reduce the above properties configuration for same requirement,, please comment.
Try
log4j.appender.file.Threshold=INFO
Also see related SO question and documentation
I want to redirect all messages with error level to file and messages with info level to console.
Here is my log4j.properties:
log4j.rootLogger=INFO, stdout
log4j.logger.java.lang.Exception=ALL, java.lang.Exception
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
log4j.appender.java.lang.Exception=org.apache.log4j.RollingFileAppender
log4j.appender.java.lang.Exception.File=\\clientexceptionlog.txt
log4j.appender.java.lang.Exception.MaxFileSize=2048KB
log4j.appender.java.lang.Exception.layout=org.apache.log4j.PatternLayout
log4j.appender.java.lang.Exception.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
But seems like it doesn't work as I expect: clientexceptionlog.txt is always empty.
What I'm doing wrong? Thanks!
You may work with the Treshold option. Doing it that way, you can set the rootLogger to both, stdout and your file appender (I named it myAppender in the following code snippet):
log4j.rootLogger=INFO, stdout, myAppender
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
log4j.appender.myAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myAppender.Threshold=WARN
log4j.appender.myAppender.File=\\clientexceptionlog.txt
log4j.appender.myAppender.MaxFileSize=2048KB
log4j.appender.myAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
What does it do? It sends everything with a loglevel INFO or higher to stdout and myAppender. While stdout will output all (including higher loglevels like WARN and ERROR) the myAppender will filter out everything that is lower than WARN (due to log4j.appender.myAppender.Threshold=WARN).
BTW: Don't use java.lang.Exception as name for the appender. That may confuse any one reading this configuration (including me!)
# Root logger option
log4j.rootLogger=ALL, stdout, A1
log4j.logger.org.apache.jsp=ALL, stdout, A1
# 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} [%p] [%c:%L] - %m%n
# LOG4J daily rolling log files configuration
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=logs/file.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] [%c:%L] - %m%n
Most of the time I define several appenders... one for Console and some others into files. But most of the time I used the same layout for the output.
Is there a way to define a default one ?
Something like that ?
log4j.appender.layout=org.apache.log4j.PatternLayout
log4j.appender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] [%c:%L] - %m%n
# Root logger option
log4j.rootLogger=ALL, stdout, A1
log4j.logger.org.apache.jsp=ALL, stdout, A1
PATTERN=%d{yyyy-MM-dd HH:mm:ss} [%p] %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=${PATTERN}
# LOG4J daily rolling log files configuration
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=logs/file.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=${PATTERN}
It seems that the only way to do this is to set a variable, as I've done with PATTERN=%d{yyyy-MM-dd HH:mm:ss} [%p] %l - %m%n