Prevent Log4j create more than 5 log files - java

As the title says, I want to create no more than 5 log files with Log4j2. Take in mind that I want to use a .properties file. Here is what I have so far:
rootLogger.level=INFO
property.filename=/data/standalone/ldap_proxy_server/logs/ldap_server_proxy.log
appenders=rollingAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{dd/MM/yyyy HH:mm:ss} %5p %c{20} > %m%n
appender.rollingAppender.type=RollingFile
appender.rollingAppender.name=File
appender.rollingAppender.fileName=/data/standalone/ldap_proxy_server/logs/ldap_server_proxy.log
appender.rollingAppender.filePattern=/data/standalone/ldap_proxy_server/logs/ldap_server_proxy.log.%d{yyyy-MM-dd}
appender.rollingAppender.layout.type=PatternLayout
appender.rollingAppender.layout.pattern=%d{dd/MM/yyyy HH:mm:ss} %5p %c{20} > %m%n
appender.rollingAppender.policies.type=Policies
appender.rollingAppender.policies.time.type=TimeBasedTriggeringPolicy
appender.rollingAppender.policies.time.interval=1
appender.rollingAppender.policies.time.modulate=true
appender.rollingAppender.policies.size.type=SizeBasedTriggeringPolicy
appender.rollingAppender.policies.size.size=50MB
appender.rollingAppender.strategy.type=DefaultRolloverStrategy
appender.rollingAppender.strategy.max=5
rootLogger.appenderRefs=rollingAppender,console
rootLogger.appenderRef.console.ref=STDOUT
rootLogger.appenderRef.rollingAppender.ref=File
I thought that this two lines does the magic:
appender.rollingAppender.strategy.type=DefaultRolloverStrategy
appender.rollingAppender.strategy.max=5
But it's not working. What I'm doing wrong?

I think you have to specify to delete old log files if no. of files become more than 5 after rotation.
Try changing rollover strategy as per below lines in your configuration file -
appender.rollingAppender.strategy.type = DefaultRolloverStrategy
appender.rollingAppender.strategy.delete.type = Delete
appender.rollingAppender.strategy.delete.basePath = /data/standalone/ldap_proxy_server/logs/
appender.rollingAppender.strategy.delete.maxDepth = 1
appender.rollingAppender.strategy.delete.ifAccumulatedFileCount.type = IfAccumulatedFileCount
appender.rollingAppender.strategy.delete.ifAccumulatedFileCount.exceeds = 5
You can also specify to delete files older than 5 days but in that case, 5 or more than 5 files can exist as you are using SizeBasedTriggeringPolicy also.

Related

log4j2 properties file for a custom appender

I created a custom appender and it's not getting called when I run my test. Here's what the properties look like:
name=config
appenders=console, myCustomAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
#appender.console.layout.pattern =%d{HH:mm:ss} [%t] %c{1} [%-5level] - %msg%n
appender.console.layout.pattern=%d{dd-MM-yyyy HH:mm:ss} [%-5p] (%F:%L) - %m%n
appender.myCustomAppender = com.myCompany.logging.log4j.WindowsEventLogAppender
appender.myCustomAppender.name = WindowsEventLogAppender
appender.myCustomAppender.type = WindowsEventLogAppender
rootLogger.level=info
rootLogger.appenderRefs=stdout, myCustomAppender
rootLogger.appenderRef.stdout.ref=STDOUT
My appender is called a WindowsEventLogAppender. Any idea what's wrong with my properties file? I see the console test messages but none of the messages from my appender. Right now I'm just doing a System.out.println in my custom appender to verify it's getting called.
BTW, I've found lot's of XML examples out there for log4j2 configurations with custom appenders but none for using a properties file for configuration.
Thanks,
-Mike
I might be quite late here, but I think my answer can help other people looking for answers. Please accept this as an answer if this is correct!
If you have created a custom appender having annotation like this:
#Plugin(name = "MyCustomAppender", category = "Core",
elementType = "appender", printObject = true)
public final class MyCustomAppenderImpl extends AbstractAppender {
// other code for the plugin....
}
The log4j2 manual about Configuring Appenders states that:
"An appender is configured either using the specific appender plugin's name or with an appender element and the type attribute containing the appender plugin's name"
Meaning the type for appender should be Appender Plugin's Name attribute value.
Which in above case is MyCustomAppender (appender.identifierName.type=MyCustomAppender)
So, the Properties file configuration for this to work should be:
(Note : I have added a stdout(console) appender just to show
relevance/similarity of usage with OOTB appenders, and 2 example
usages with RootLogger and a custom logger)
# this packages attribute is important, please put comma seperated package(s) to the
# plugin(s) you have created
packages = com.package.to.your.plugin
# Example: Declare and Define OOTB Console appender, which sends log events to stdout
appender.console.name = stdout
appender.console.type = Console
# Declare and define the custom appender like this
# Note that the "abc" in "appender.abc.type" can be anything
# and the value for "appender.abc.type" should be the same as
# "Name" attribute value given in custom appender plugin which is "MyCustomAppender"
appender.abc.name=arbitrary_name
appender.abc.type=MyCustomAppender
rootLogger.appenderRef.stdout.ref = stdout
rootLogger.appenderRef.abc.ref = arbitrary_name
logger.loggeridentifier.name = com.test.SomeClass
logger.loggeridentifier.appenderRef.stdout.ref = stdout
logger.loggeridentifier.appenderRef.abc.ref = arbitrary_name
# Also note that the value of appenderRef should be the same name given to your
# appender in properties file, which in this case is "arbitrary_name" (as given above)
Try adding the packages property.
Like: packages = com.myCompany
You haven't included the package info
try the below configuration.
name=config
appenders=console, myCustomAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
#appender.console.layout.pattern =%d{HH:mm:ss} [%t] %c{1} [%-5level] - %msg%n
appender.console.layout.pattern=%d{dd-MM-yyyy HH:mm:ss} [%-5p] (%F:%L) - %m%n
appender.myCustomAppender = com.myCompany.logging.log4j.WindowsEventLogAppender
appender.myCustomAppender.name = WindowsEventLogAppender
appender.myCustomAppender.type = WindowsEventLogAppender
rootLogger.level=info
rootLogger.appenderRefs=stdout, myCustomAppender
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.com.mycompany.example=INFO,STDOUT

Why does my log4j2 properties configuration not log to console nor a separate file?

I have been trying to log messages from a specific class. I only want the specific messages from that class to log to a separate file and nothing else. I cant seem to get it right and I have tried multiple configurations including XML format. Id be helpful for feedback on my configuration. (see Below )
log4j2.appender.solrindexlog.type=RollingFile
log4j2.appender.solrindexlog.name=SolrIndexLog
log4j2.appender.solrindexlog.fileName=${NAME_LOG_DIR}/tomcat/solrindex.log
log4j2.appender.solrindexlog.filePattern=${NAME_LOG_DIR}/tomcat/solrindexlog-%d{yyyyMMdd}.log
log4j2.appender.solrindexlog.layout.type=PatternLayout
log4j2.appender.solrindexlog.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
log4j2.appender.solrindexlog.policies.type=Policies
log4j2.appender.solrindexlog.policies.time.type=TimeBasedTriggeringPolicy
log4j2.appender.solrindexlog.policies.time.interval=2
log4j2.appender.solrindexlog.policies.time.modulate=true
log4j2.appender.solrindexlog.policies.size.type=SizeBasedTriggeringPolicy
log4j2.appender.solrindexlog.policies.size.size=100MB
log4j2.appender.solrindexlog.strategy.type=DefaultRolloverStrategy
log4j2.appender.solrindexlog.strategy.max=5
log4j2.logger.solrindexlog.name=se.package.name
log4j2.logger.solrindexlog.level=debug
log4j2.logger.solrindexlog.appenderRefs=stdout
log4j2.logger.solrindexlog.appenderRef.stdout.ref=STDOUT
log4j2.rootLogger.level=info
log4j2.rootLogger.appenderRef.stdout.ref=STDOUT
log4j2.rootLogger.appenderRef.solrindexlog.ref=SolrIndexLog
log4j2.rootLogger.appenderRefs=stdout,solrindexlog
Try below configuration file -
appender.solrindexlog.type=RollingFile
appender.solrindexlog.name=SolrIndexLog
appender.solrindexlog.fileName=${NAME_LOG_DIR}/tomcat/solrindex.log
appender.solrindexlog.filePattern=${NAME_LOG_DIR}/tomcat/solrindexlog-%d{yyyyMMdd}.log
appender.solrindexlog.layout.type=PatternLayout
appender.solrindexlog.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.solrindexlog.policies.type=Policies
appender.solrindexlog.policies.time.type=TimeBasedTriggeringPolicy
appender.solrindexlog.policies.time.interval=2
appender.solrindexlog.policies.time.modulate=true
appender.solrindexlog.policies.size.type=SizeBasedTriggeringPolicy
appender.solrindexlog.policies.size.size=100MB
appender.solrindexlog.strategy.type=DefaultRolloverStrategy
appender.solrindexlog.strategy.max=5
logger.se.package.name.name=se.package.name
logger.se.package.name.level=debug
logger.se.package.name.additivity = false
logger.se.package.name.appenderRef.solrindexlog.ref= SolrIndexLog
logger.se.package.name.appenderRef.stdout.ref= STDOUT
rootLogger.level=info
rootLogger.additivity = false
rootLogger.appenderRef.solrindexlog.ref= SolrIndexLog
rootLogger.appenderRef.stdout.ref= STDOUT
Please change other parts of the configuration file that are not written in above configuration file like ConsoleAppender etc.
Also, as per this configuration file, all logs message will go in file as well as in console.

how to make Log4j configuration properly

I am new to the Log4j framework and after reading some stuff i got some fare idea about logging mechanism, but still
i have some doubt about the following properties.
log4j.category.com.cloud.sample=INFO, file, C
log4j.additivity.com.cloud.sample=true
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n
#log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
### direct messages to file ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/var/basic/logs/sample.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
# mylog.log.10 \u307e\u3067\u4fdd\u6301
log4j.appender.file.MaxBackupIndex=50
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] %c{1} - %m%n
log4j.rootLogger=INFO, C, file
In the first line of the above code contains two appenders(file, C) after that we will be having appender for both file and C. So as per my understanding logs will of category will be stored to Console and sample.log. Please let me know if i am wrong.
log4j.rootLogger=INFO, A1 and respective properties are not used right?
log4j.rootLogger=INFO, C, file: This line is about root logger, I think in my case it is not useful, because it is defined at the last line and there is no properties defined over here.
Please could any body confirm my understanding and suggest me if any changes required in the above configuration
The root logger resides at the top of the logger hierarchy. It is exceptional in three ways:
it always exists,
its level cannot be set to null
it cannot be retrieved by name.
The rootLogger is the father of all appenders. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy (including rootLogger).

log4j not logging to some files

I've seen many questions concerning log4j not writing any files, but my problem is that, log4j does (seem to) work, but not for all files.
My configuration is as follows (I have omitted a few loggers for simplicity, the below lines appear in the order they are in in the file):
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 %5p %c{1}:%L - %m%n
# this logger works without any problems
log4j.appender.errorFile=org.apache.log4j.RollingFileAppender
log4j.appender.errorFile.File=${LOG_HOME}/errors.log
log4j.appender.errorFile.MaxFileSize=20MB
log4j.appender.errorFile.MaxBackupIndex=10
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.appender.errorFile.Threshold=ERROR
# this file is created but always empty
log4j.appender.barcodeScanner=org.apache.log4j.RollingFileAppender
log4j.appender.barcodeScanner.File=${LOG_HOME}/barcodeScanner.log
log4j.appender.barcodeScanner.MaxFileSize=20MB
log4j.appender.barcodeScanner.MaxBackupIndex=5
log4j.appender.barcodeScanner.layout=org.apache.log4j.PatternLayout
log4j.appender.barcodeScanner.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
# this logger also works
log4j.appender.DoubleDtpaList=org.apache.log4j.RollingFileAppender
log4j.appender.DoubleDtpaList.File=${LOG_HOME}/DoubleDtpaList.log
log4j.appender.DoubleDtpaList.MaxFileSize=20MB
log4j.appender.DoubleDtpaList.MaxBackupIndex=1
log4j.appender.DoubleDtpaList.layout=org.apache.log4j.PatternLayout
log4j.appender.DoubleDtpaList.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.rootLogger=error, errorFile
log4j.logger.scanner.BarcodeScanner=debug, barcodeScanner
log4j.logger.util.DetectDoubles=trace, DoubleDtpaList
Using the logger within the BarcodeScanner class yields correct results when using logger.error() (this logs to the errors.log file), but all calls to logger.debug() and logger.info() have no effect.
The variable LOG_HOME is passed as an argument/option when executing the *.jar file.
EDIT 1: I added another (working) logger/appender configuration for clarification. Another piece of information I unfortunately left out ist that I want all logs for the scanner to go to the barcodeScanner.log file (with the errors additionally going to errors.log (root Logger)

Overwriting log4j not working

I have a log4j properties with the following configuration:
log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=${directory}/log/app.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOG.Threshold=DEBUG
log4j.appender.LOG.append=false
log4j.appender.LOG.bufferedIO=false
log4j.appender.LOGHISTORY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGHISTORY.File=${directory}/log/old-logs/app.log
log4j.appender.LOGHISTORY.File.DatePattern='.'yyyy-MM-dd
log4j.appender.LOGHISTORY.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGHISTORY.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOGHISTORY.Threshold=DEBUG
log4j.appender.LOGHISTORY.append=true
log4j.appender.LOGHISTORY.bufferedIO=false
I want to save a history of previous day's logs in the "log/old-logs" folder. This is working great with the DailyRollingFileAppender.
I also want to have a log of the current day in the "log" folder. This is working fine on my localhost (Windows + Eclipse + Geronimo), but is not working properly on my testing server (Linux + WebSphere). In this case the "app.log" is not being overwritten and everything is being appended at the end of the log.
AFAIK Rolling file appender will only roll the file when a specified a max size is reached .
The above scenario might be working for you in the local copy each time application is restarted, the log file will overwritten because of .append = false . On a production environment I do not think that the server is restarted that is why the file is written as per .append = false . Could this be your case why overwriting did not work ?
Check this.
You should use Append with a capital A.
log4j.appender.LOG.Append=false
log4j.appender.LOGHISTORY.Append=true
For the RollingFileAppender, you need to specify the property "MaxFileSize". This will tell log4j to roll the file over when it reaches that size.
eg: Below will roll over when log file size reaches 2MB
log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=${directory}/log/app.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOG.Threshold=DEBUG
log4j.appender.LOG.MaxFileSize=2MB
log4j.appender.LOG.MaxBackupIndex=2 (This is optional, tells log4j the maximum backup files to take)

Categories