I have the below configured for log4j which outputs a csv log file. Every time my program executes I wish to start this log file a fresh by overwriting not appending to the log file. I thought I could achieve this by using the append=false. I know that I have correctly set up log4j as other logs are outputting fine but these are daily rolling logs that are appending which is the desire affect.
Can anyone tell me why the append=false doesn't seem to work. Is there another setting I've missed?
Here's my config code:
#Image output
log4j.logger.fetch.FetchDirectHolidays=debug, S
log4j.appender.S=org.apache.log4j.FileAppender
log4j.appender.S.File=xml\\logs\\FetchDirectHolidays.csv
log4j.appender.S.append=false
# Keep one backup file
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern= %p , %m%n
What is wrong with my configuration?
I forgot to state that my application is scheduled and I have just read that the Append=false only clears the log file if the whole application is shutdown and restarted. This does not help as I need to clear this log file each time the internal processes executes.
Try
log4j.appender.S.Append=false
with a capital A for Append
# Define the root logger with appender file R
log4j.rootLogger = INFO, FILE,stdout
# Define the file appender (File)
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:/user/FileName.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%5p ] - %m%n
# Direct log messages to stdout (Console)
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=[%5p ] - %m%n
Related
I have recently added Log4j in my project and all the logs are printing as expected.
Now I have added the timestamp in the log4j.properties file and server-generated logs are printing with the timestamp but which statements I am printing from Logger.info() and Logger.error() it is not printing with the timestamp. This is how my log4j.properties look like:
logFileLoc = ${catalina.base}/logs/
log4j.rootLogger=INFO, stdout, fileAppender
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
# Redirect Log Messages To A Debug Log File, Support File Rolling !!
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File=${logFileLoc}/catalina.out
log4j.appender.fileAppender.MaxFileSize=50MB
log4j.appender.fileAppender.MaxBackupIndex=10
log4j.appender.fileAppender.Append=true
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}: [%-5p] %c{1}:%L - %m%n
Whenever I try something like this in my code:
LOGGER.debug("creating HomePage");
This is what I see in my log file:
creating HomePage
Can anyone please suggest how can I print the timestamp in LOGGER statements? It will be highly appreciable.
I checked your configuration with log4j 1.2.17 and everything works correctly.
I think you have other log4j config file in classpath or you are using system variables(like log4j.defaultInitOverride or some others) which have impact on log4j configuration.
I recommend debugging log4j initialization in static block of the LogManager class and then you will see from which place log4j is loading configuration.
if I'm wrong, please provide code for reproducing your issue.
I'm using org.slf4j:slf4j-api:1.7.26 to write logs from a Java application. My log4j.properties looks like:
log4j.rootLogger=WARN, console
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.console=org.apache.log4j.ConsoleAppender
# use a simple message format
log4j.appender.console.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern={\
"time":"%d{ISO8601}",\
"level":"%p",\
"class":"%c{1.}",\
"message":"%m",\
"thread":"%t",\
"fileName":"PLACEHOLDER_FILE_NAME"\
}%n
I need to replace PLACEHOLDER_FILE_NAME with the name of a file that is fed into the application as a CLA at startup. rather How can this be done? I've looked into java.util.logging.Formatter but I'm not convinced that's the best way as it seems to override log4j.properties.
You can read system property eg. ${file.name} in log4j config:
Then, you just need to set this property before log4j reads this config eg. at startup specyfying it as -Dfile.name=filenamePath
log4j.rootLogger=WARN, console
# add a ConsoleAppender to the logger stdout to write to the console
log4j.appender.console=org.apache.log4j.ConsoleAppender
# use a simple message format
log4j.appender.console.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern={\
"time":"%d{ISO8601}",\
"level":"%p",\
"class":"%c{1.}",\
"message":"%m",\
"thread":"%t",\
"fileName":"${file.name}"\
}%n
I'm trying to log certain messages on a different file on my tomcat installation, using log4j, but although it does log on my rootLogger it doesn't do it on the separate file I am creating.
Here is my log4j.properies file, where the file I'm trying to use is dataflow_logging.log:
# Root logger option
log4j.rootLogger=INFO, stdout, mainlogger
# Direct dataflow specific messages to specific file.
log4j.logger.org.estat.nsiws.dataflows=INFO, dataflowlogger
log4j.appender.dataflowlogger=org.apache.log4j.RollingFileAppender
log4j.appender.dataflowlogger.File=${catalina.home}/logs/nsi/dataflow_logging.log
log4j.appender.dataflowlogger.MaxFileSize=10MB
log4j.appender.dataflowlogger.MaxBackupIndex=10
log4j.appender.dataflowlogger.layout=org.apache.log4j.PatternLayout
log4j.appender.dataflowlogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Disable dataflow log messages in root logger.
log4j.additivity.org.estat.nsiws.dataflows=false
# Direct main logging messages to standard output.
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{HH:mm:ss,SSS} [%p]: %m%n
# Also direct main logging messages to file.
log4j.appender.mainlogger=org.apache.log4j.RollingFileAppender
log4j.appender.mainlogger.File=${catalina.home}/logs/nsi/nsiws.log
log4j.appender.mainlogger.MaxFileSize=10MB
log4j.appender.mainlogger.MaxBackupIndex=10
log4j.appender.mainlogger.layout=org.apache.log4j.PatternLayout
log4j.appender.mainlogger.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%p]: %m%n
My WebService's properties file:
## This file contains properties for the Web Service
# The absolute path to the logs directory
log.directory=logs/nsi
# The log file prefix
log.file.prefix=nsiws_
# The log file suffix
log.file.suffix=.log
# The prefix of the temporary files created for the buffering of responses.
tempfile.buffer.prefix=ws_resp
# Enable logging dataflow access
log.df.file.activation=true
# The absolute path to the dataflow logs directory
log.df.file.directory=logs/nsi
# The dataflow log file prefix
log.df.file.name=dataflow_logging
# The dataflow log file maximum size
log.df.file.max.size=100MB
# The dataflow max backup file
log.df.file.max.backup=10
# The dataflow log file separator
log.df.file.separator=;
# The dataflow log file date pattern
log.df.date.pattern=%d{yyyy-MM-dd HH:mm:ss,SSS};%m%n
The method that initialises the dataflow logger:
private static Logger initDataflowLogger() {
Logger dflogger = null;
try {
if (Boolean.parseBoolean(nsiProps.getProperty("log.df.file.activation"))) {
dflogger = org.apache.log4j.Logger.getLogger("org.estat.nsiws.dataflows");
dflogger.setLevel(Level.INFO);
logger.debug("Initializing dataflow logging...");
}
} catch (Exception e) {
logger.error(getInstance().getMessage("error.df.log.properties.not.set"));
}
return dflogger;
}
Thanks a lot for your help!
Check if this expression is true and if you have set your properties file correctly, so your project can read from it.
Boolean.parseBoolean(nsiProps.getProperty("log.df.file.activation"))
Your root logger is in INFO mode.
Which will not log the debug level statement.
You have taken the instance of org.estat.nsiws.dataflows (dataflowlogger Appender), which is "dflogger" but you are actually calling "logger.debug"
Is there a way to log all stdout output to the catalina.log file in Tomcat? (i.e. everything that gets printed to System.out.println())
The console window that opens when you run TOMCAT/bin/startup.bat displays output from stdout, but it's not saved to TOMCAT/logs/catalina.<date>.log.
My specific problem is that I have a console appender defined in log4j to output to the console. These log messages appear correctly in the Tomcat console window, but they are not written to catalina.log. I'm running Tomcat 5.5 on Windows. Thanks.
EDIT:
Here is my log4j.properties file. It is located at TOMCAT/webapps/app/WEB-INF/classes/log4j.properties:
log4j.rootCategory=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{ABSOLUTE} %-5p %c{1}]: %m%n
I've come across similar questions before, and haven't found a way to do this by logging System.out in Windows unless you are running Tomcat as a Windows service. This seems to work by default in Unix since startup.sh points to catalina.sh which logs stdout to the catalina.out file like below
org.apache.catalina.startup.Bootstrap "$#" start >> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
In log4j, ConsoleAppender by itself does not append to a File, only to System.out
However, I've modified your log4j properties to add a FileAppender and this config works, but of course this logs into a separate log file.
New config
# Set root logger level to DEBUG.
log4j.rootLogger=DEBUG, console, myFile
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{ABSOLUTE} %-5p %c{1}]: %m%n
# myFile writes to file
log4j.appender.myFile=org.apache.log4j.RollingFileAppender
log4j.appender.myFile.File=logs/tomcatlog4j.log
log4j.appender.myFile.MaxFileSize=100KB
log4j.appender.myFile.layout=org.apache.log4j.PatternLayout
log4j.appender.myFile.layout.ConversionPattern==[%d{ABSOLUTE} %-5p %c{1}]: %m%n
Output
=[15:24:03,819 INFO A1]: In my.jsp
=[15:24:03,975 INFO A1]: Out of my.jsp
=[15:24:04,880 INFO A1]: In my.jsp
=[15:24:04,880 INFO A1]: Out of my.jsp
also see
How to log exceptions from a specific package deployed in tomcat
log select events into a separate file
https://serverfault.com/questions/201178/tomcat-5-5-how-to-redirect-the-logging-output-to-one-file-per-web-application
Did you checked, whether the log4j.properties file can be found from your application?
Maybe you can check, by setting a hardcoded file path like
-Dlog4j.configuration=file:///C:\Dev\log4j.properties
If the logs are written after these change, the relativ path to the log4j file is wrong.
If I look at the default logging config of tomcat 5.5 in logging.properties:
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler
That looks to me as if stdout of web applications might be logged to files only for level INFO and above, regading that http://tomcat.apache.org/tomcat-5.5-doc/logging.html states that in tomcat JULI logging configuration loggers do not use parent's handlers when they are assigned their own handlers. Also the file should be prefixed localhost and not catalina. But then I do not understand how the output comes to your output window :/
How do I log from within my web application deployed on Tomcat 6? Where should I expect the logging output to go (internal tomcat log files, or will another logfile be generated)? I see a ton of documentation but am having a hard time finding a direct answer to the above questions. Where should I expect the logging to show up (currently it is log4j is not generating a log file and it is not showing up in my console). I am trying to follow http://www.laliluna.de/articles/log4j-tutorial.html .
### 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{ABSOLUTE} %5p %c{1}:%L - %m%n
### file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=test.log
log4j.appender.file.threshold=info
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
In my application I define a log object:
private static org.apache.log4j.Logger log = Logger.getLogger(MyClass.class);
log.error("LOGGING!");
Thanks for the help.
2 things to try:
1: Change test.log to /tmp/test.log so you know exactly where the file is supossed to be.
2: Put your log4j.properties config file in your apache-tomcat-6.0.x/lib directory together with the log4j-1.2.15.jar file. And don't have any log4j files in your webapps/*/WEB-INF/lib
That's the way I am doing it, and its working for me. Here is a usefull snippet from my log4j.properties (Remember to do a mk /tmp/logs if you use this config)
log4j.rootLogger=debug, root
log4j.appender.root=org.apache.log4j.FileAppender
log4j.appender.root.layout = org.apache.log4j.PatternLayout
log4j.appender.root.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.root.file = /tmp/logs/root.log
log4j.appender.root.append = true
log4j.category.mside = DEBUG,msideAppender
log4j.category.javashare = DEBUG,msideAppender
log4j.additivity.mside = false
log4j.additivity.mside.msideAppender = false
log4j.additivity.javashare = false
#Define msideAppender.
log4j.appender.msideAppender = org.apache.log4j.RollingFileAppender
log4j.appender.msideAppender.MaxFileSize=10MB
log4j.appender.msideAppender.MaxBackupIndex=7
log4j.appender.msideAppender.file = /tmp/logs/mside.log
log4j.appender.msideAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.msideAppender.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.msideAppender.append = true
IIRC Tomcat v4/v5 sends standard output to the catalina.out file, so any log4j output using a console appender would go to that file as well. Not sure if this still the case with newer versions of Tomcat, though.
Your log4j configuration will be picked up by log4j if you put it into the classpath of your web application, e.g. in WEB-INF/classes/. Make sure that your log4j.jar is in WEB-INF/lib.
The output of the ConsoleAppender that you defined, which is logging on stdout, will go to ${CATALINA_BASE}/logs/catalina.out, as any Tomcat stdout output.
As to the RollingFileAppender, you should define the correct path. If you want your web application's logs to appear in Tomcat's logs directory, change the file for this appender to:
log4j.appender.file.File=${catalina.base}/logs/test.log
BTW, use the system property -Dlog4j.debug system property. That should tell you where the heck log4j is sending its output.
Also, if your tomcat install is in a *nix system, or if you are running on Windows with cygwin installed, you could use the find command to detect what files get changed right after you send a HTTP request to Tomcat (which you know should produce a logging output)
cd <your tomcat install>
ls -ltr `find . -type f -ls` | tail -10
That should show you the last 10 files that were updated or changed. It won't work if there are files in your app with spaces in their file names, though.
I have tried following way and make sure it works well:
put your own log4j.properties place at a path;
then update your catalina.sh under tomcat, add below similar line:
JAVA_OPTS="-Dlog4j.configuration=file:${yourownlog4jpath}"