Overwriting log4j not working - java

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)

Related

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.

Prevent Log4j create more than 5 log files

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.

How do I change dropwizard's access log format?

I want to output the access logs to console. I also want to log files with the following configuration:
server:
requestLog:
appenders:
- type: file
currentLogFilename: D:/Log/access.log
archivedLogFilenamePattern: D:/Log/access-%d.log.gz
logFormat: "%d{yyyy-MM-dd HH:mm:ss.SSS,Asia/Shanghai} %-6level [%t] %c %msg %n"
- type: console
logFormat: "%d{yyyy-MM-dd HH:mm:ss.SSS,Asia/Shanghai} %-6level [%t] %c %msg %n"
template: Hello, %s!
defaultName: Stranger
I can run my application normally, but the logformat output is not like my configuration in the configuration file. I can rewrite the class using the org.eclipse.jetty.server.AbstractNCSARequestLog's log method, but obviously this is a very bad method. It solves the problems but if I want to upgrade my application I have to modify all of my AbstractNCSARequestlog class, which is very troublesome and messy.
What is another way I can go about this problem?

log4j:ERROR while writing the logs statements in file

I have created a java project called Project1. for this project i created a log4j.properties file. For writing the log statements in log file, i used DailyRollingFileAppenders. I am using log4j-1.2.16.jar. The log file is :
log4j.logger.com.gridsense.server.automode=DEBUG, stdout,Rollfile
log4j.rootLogger=off
log4j.logger.com.gridsense.server.automode=debug,Rollfile,stdout
log4j.appender.Rollfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Rollfile.ImmediateFlush=false
log4j.appender.Rollfile.Append=true
log4j.appender.Rollfile.Threshold=DEBUG
log4j.appender.Rollfile.bufferedIO = true
log4j.appender.Rollfile.File=D:/logs/AutoGS.log
log4j.appender.Rollfile.layout=org.apache.log4j.PatternLayout
log4j.appender.Rollfile.layout.ConversionPattern=[%t] %-5p %c %d{dd/MM/yyyy HH:mm:ss} %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x %m%n
When i run the project i get the following error
log4j:ERROR Could not close org.apache.log4j.helpers.QuietWriter#1105348
java.io.IOException: The handle is invalid
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:282)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:263)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:106)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.BufferedWriter.close(BufferedWriter.java:246)
at java.io.FilterWriter.close(FilterWriter.java:87)
at org.apache.log4j.FileAppender.closeFile(FileAppender.java:185)
at org.apache.log4j.FileAppender.reset(FileAppender.java:343)
at org.apache.log4j.WriterAppender.close(WriterAppender.java:207)
at org.apache.log4j.AppenderSkeleton.finalize(AppenderSkeleton.java:144)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
log4j:ERROR Failed to write [[DefaultQuartzScheduler_Worker-2] DEBUG com.gridsense.server.automode.backgroundWorker.TimerTaskToBeFired 03/05/2013 17:34:45 ? TimerTaskToBeFired.execute - firing timer job with Key [DropBoxFileFetchingSchedulerGroup.JobDetail-ID-1]].
I don't understand what is the problem.
My reading of the stacktrace is that something has leaked a FileAppender object, and that appender's finalize method is attempting to clean it up. But it looks like that is failing because the resource in question has already been closed.
That's the easy part. The hard part is identifying the source of the leak. My guess is that it is caused by this line:
log4j.logger.com.gridsense.server.automode=debug,Rollfile,stdout
which appears to be configuring a second logger tree duplicating the one configure a couple of lines earlier.

How to give environmental variable path for file appender in configuration file in log4j

I have a log4j.xml config file. and a RollingFileAppender to which I need to provide file path for storing logs. The problem is my code will be deployed on Unix machine as a runnable jar. So if I pass parameter something like this:
value=logs/messages.log"
it creates folder named logs inside my HOME directory and writes all the messages to file inside this directory.
I have a environmental variable set to some value. I want to use path of that variable and write messages under that path. How can I achieve it?
I had tried using this:
value="${MY_HOME}/logs/message.log"
but this does not work. Can anyone suggest a solution for this problem?
When parsing its configuration file, the expression ${MY_HOME} will be expanded to the value of the system property named MY_HOME, not the system environment variable. There's a difference between the two.
To achieve this in a clean way, you'll have to add something like this to the JVM invocation line:
-DMY_HOME=$MY_HOME
That would define the Java system property MY_HOME to contain the value of the environment variable MY_HOME.
you CAN give it environment variables. Just preppend env: before the variable name, like this:
value="${env:MY_HOME}/logs/message.log"
This syntax is documented only in log4j 2.X so make sure you are using the correct version.
<Appenders>
<File name="file" fileName="${env:LOG_PATH}">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</File>
</Appenders>
http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup
I got this working.
In my log4j.properties. I specified
log4j.appender.file.File=${LogFilePath}
in eclipse - JVM arguments
-DLogFilePath=C:\work\MyLogFile.log
java -DLOG_DIR=${LOG_DIR} -jar myjar.jar "param1" "param2" ==> in cmd line if you have "value="${LOG_DIR}/log/clientProject/project-error.log" in xml
Maybe... :
datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
layout=%d{${datestamp}} ms=%-4r [%t] %-5p %l %n%m %n%n
# infoFile
log4j.appender.infoFile=org.apache.log4j.RollingFileAppender
log4j.appender.infoFile.File=${MY_HOME}/logs/message.log
log4j.appender.infoFile.layout=org.apache.log4j.PatternLayout
log4j.appender.infoFile.layout.ConversionPattern=${layout}
Log4j entry
#- File to log to and log format
log4j.appender.file.File=${LOG_PATH}/mylogfile.log
Java program
String log4jConfPath = "path/log4j.properties";
File log4jFile = new File(log4jConfPath);
if (log4jFile.exists()) {
System.setProperty("LOG_PATH", "c:/temp/");
PropertyConfigurator.configure(log4jFile.getAbsolutePath());
logger.trace("test123");
}
To dynamically change a variable you can do something like this:
String value = System.getenv("MY_HOME");
Properties prop = new Properties("log4j.properties");
prop.put("MY_HOME", value); // overwrite with value from environment
PropertyConfigurator.configure(prop);
Since you are using unix you can use a path like this.
/home/Production/modulename/logs/message.log
path should start with /

Categories