log4j, fileappender and tomcat6 logs question - log4j

I have seen many questions about the above topics but none that address this.
I am trying to use log4j to log to a custom file in $CATALINA_BASE/logs/ directory.
I configured the log4j.xml file and copied it into the $CATALINA_BASE/lib/ directory.
I use the following lines to create the logger -
PatternLayout layout = new PatternLayout();
FileAppender appender= new FileAppender("filename.txt");
This is where my problem is. How do I make the FileAppender take the file name I configured in the log4j.xml?
I was hoping it will automatically pick that up, but there is no consructor for FileAppender that will not take a filename.
Do I have to read the log4j.xml to get the name of the file? If so why in the world do I need to set that property in the xml at all?
Any help would be greatly appreciated.
Thanks,
- Vas

Hmm... why are you creating a FileAppender in code? Just create a logger using LoggerFactory specifying the string (usually in com.xxx.yyy format that you configured in the log4j XML/properties file) and start logging. Make sure the logger is configured to use the FileAppender implementation (Daily or RollingFile) in the config file and you are all set to go.

Related

Cannot change zookeeper log filename

Zookeeper is creating the logs with a name zookeeper-root-hostname.out but this is my log4j.properties:
zookeeper.root.logger=INFO, CONSOLE
zookeeper.console.threshold=INFO
zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=INFO
zookeeper.log.maxfilesize=256MB
zookeeper.log.maxbackupindex=20
zookeeper.tracelog.dir=${zookeeper.log.dir}
zookeeper.tracelog.file=zookeeper_trace.log
log4j.rootLogger=${zookeeper.root.logger}
#
# console
# Add "console" to rootlogger above if you want to use this
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}#%L] - %m%n
#
# Add ROLLINGFILE to rootLogger to get log file output
#
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize}
log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex}
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}#%L] - %m%n
If a change the property zookeeper.log.file=zookeeper.log the file is created with the same name, How can I change the filename of the log?
Update
I found that the file zkServer.sh set the variable ZOO_LOG_FILE and overrides the value that is define in the log4j.properties:
ZOO_LOG_FILE=zookeeper-$USER-server-$HOSTNAME.log
I can modify this file but Is it ok to change that manually?
In typical Java application fashion the logging situation with Zookeeper is incredibly complicated, custom, and poorly documented. The official Administrators manual logging section is a tiny paragraph with about zero information, but suggests you can rely on your existing knowledge of the third party library log4j configured to proxy through the other third party library SLF4J. This actually wouldn't be too bad since that complicated arrangement is common as many popular open source projects ignore the built-in logging framework that ships will Java, but later you'll discover using the log4j.properties file in the conf directory actually doesn't work as expected because custom properties are used in the provided start up script (zkServer.sh) that override many things.
In order to set the file name note the file conf/log4j.properties contains the following variable assignment that is overridden by the start up script (zkServer.sh):
zookeeper.log.file=zookeeper.log
Either set the environment variables externally, modify the start up script or update the log4j.properties to not use the variable:
#log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.File=/the/actual/path/goes/here.log
You may also want to enable the ROLLINGFILE like so:
#log4j.rootLogger=${zookeeper.root.logger}
log4j.rootLogger=INFO,ROLLINGFILE
After searching I found this link but I didn't found how to change the variable for the log, I noticed that inside the file zkServer.sh is defined the name of the file, so I needed to change this variable:
_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out"
I don't know if it's ok to change that variable but now is working as expected.

Dailyrollingfileappender not creating backup of more than 1 day

My log config is as follows,but i can see only 1 backup file being created and all the other backup just goes off / deleted.
log4j.appender.application=org.apache.log4j.DailyRollingFileAppender
log4j.appender.application.File=${log.root.path}/test.log
log4j.appender.application.DatePattern='.'yyyy-MM-dd
log4j.appender.application.layout=org.apache.log4j.PatternLayout
log4j.appender.application.layout.ConversionPattern=%d{dd MMM
HH\:mm\:ss\:SSS} [%t] %-5p [%c\:%M\:%L] srcIp=%X{srcIp} -
remoteHost=%X{remoteHost} -port=%X{port} - activityType=%X{activitytype} -
activityStatus=%X{activitystatus} -applicationUser=%X{applicationUser} -
APITXNID=%X{apiTxnId} -CustomerRefNum=%X{CustomerRefNum} -%m%n%n
Your configuration looks fine, there are several thing you could try to do:
Try replace log4j.appender.application.DatePattern from '.'yyyy-MM-dd to '.'yyyy-MM-dd-HH-mm and see if files would be created every minute. If it works, chances are that some external cleanup script deletes old logs.
Add -Dlog4jdebug.true to VM options to verify what is effective log4j conf file is used.
Also consider using API Documentation warning
DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.
You can have the same effect as your config above, by replacing log4j dependency with apache-log4j-extras, which implements log4j 1.2.x, but has extra features, such as org.apache.log4j.rolling.RollingFileAppender with TimeBasedRollingPolicy
Then replace in your log4j.properties
log4j.appender.application=org.apache.log4j.DailyRollingFileAppender
log4j.appender.application.File=${log.root.path}/test.log
log4j.appender.application.DatePattern='.'yyyy-MM-dd
with
log4j.appender.application=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.application.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.application.rollingPolicy.FileNamePattern=${log.root.path}/test.log.%d{yyyy-MM-dd-HH-mm}
log4j.appender.application.File=${log.root.path}/test.log

Can you set a property value within the log4net config?

I'm working on a C# application that will have quite a few log files. These log files will be created in the same file path, and this file path will be determined at configuration time.
Is there a way I could specify this log files basepath in a single location the log4net configuration e.g. in a property?
I'd, of course, be referencing this basepath in the file param of each logfile appender?
One option I've considered is setting this path in the app.config/web.config, which the application will transfer into a log4net global context property. However, that would mean making sure that all loggers are created AFTER this property is set.
Any comments on the preferred, or the fallback method are welcome.
cheers!
You can implement a log4net custom pattern converter to return the basepath for logfiles. This is explained here: https://stackoverflow.com/a/4389828/910348.
The difference for your scenario is that your converter can simply read the basepath from your app.config/web.config.
Cheers!

How can i configure Log4j to generate new log file every time the java application runs

How can I configure Log4j to generate new log file every time the java application runs?
I want the log name to vary every time like [Log file name]_[CurrentTime].log
I am using .property file to configure log4j
i am using RollingFileAppender but this did not worked for me.here is my configuration
log4j.appender.dest4=org.apache.log4j.RollingFileAppender
log4j.appender.dest4.File=.\\\\log4j\\\\user.log
log4j.appender.dest4.layout=org.apache.log4j.PatternLayout
log4j.appender.dest4.layout.ConversionPattern=[%d{dd/MMM/yyyy HH:mm:ss}] : %-5p: %m%n
log4j.appender.dest4.DatePattern='.'yyyy-MM
You might want to take a look at the RollingFileAppender
HereĀ“s an interesting post about the subject which applies to log4j as well: How do I configure a RollingFileAppender to roll by date and size with log4net?
Hope it helps.
You can set FileAppender dynamically
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"logname."+new Date().toLocaleString(),false);
logger.addAppender(appender);

Log4j referring to external properties file in class path

I have a log4j.properties file
log4j.appender.BigBrotherLog=org.apache.log4j.RollingFileAppender
log4j.appender.BigBrotherLog.File=${userprofile.broker.bigbrother.log4j.file.path}
log4j.appender.BigBrotherLog.MaxFileSize=100MB
log4j.appender.BigBrotherLog.MaxBackupIndex=10
log4j.appender.BigBrotherLog.layout=org.apache.log4j.PatternLayout
log4j.appender.BigBrotherLog.layout.ConversionPattern=%d{yy/MM/dd} %d{HH:mm:ss} ALARM CRITICAL SITA ESB (SOAESB) [%-t] (%F:%L) %-5p %-c{1} %x- %m%n
log4j.appender.BigBrotherLog.Threshold=FATAL
where I'm passing ${userprofile.broker.bigbrother.log4j.file.path} from a external properties file. But I want to have put this external properties file in the class path. How can I make it work? Thanks.
When you invoke the java command, pass in the system property -Dlog4j.configuration=file:[path-to-your-external-file]. The important part is the file:, otherwise log4j will only attempt to load from the class path and system resources.
You can also use -Dlog4j.debug=true to see where log4j has attempted to source it's configuration file from.

Resources