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
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 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 :/
I have created a very simple application, where I am trying to use the Log4J, but my application is not logging any log.
Can anyone please tell me how can I debug the same as my log4j started or not?
I have kept the file in classes folder of WEB-INF/classes
Thanks
following is my log4j.properties
log4j.rootLogger=debug, stdout, ABC
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.ABC=org.apache.log4j.RollingFileAppender
log4j.appender.ABC.File=D://abc//dams_workflow_application.log
log4j.appender.ABC.MaxFileSize=3000KB
# Keep one backup file
log4j.appender.ABC.MaxBackupIndex=10
log4j.appender.ABC.layout=org.apache.log4j.PatternLayout
log4j.appender.ABC.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
following is the sample line for using log 4j..
Logger log = Logger.getLogger("ABC");
log.info("my message");
If you're writing a web application, you can view the Log4J Default Initialization Under Tomcat. Even it states Tomcat, the same applies in other Containers/Web application servers.
Console will print this if log4j is not started :
log4j:WARN No log4j configuration information found.
log4j:WARN Changed non-configured level from DEBUG to ERROR.
log4j:WARN The log4j system is not properly configured!
You could have a look at Log4j - Looking for a good 'Getting started' tutorial or blog
to get started.
Regards,
Stéphane
Make sure the log4j jar is in the lib directory for your webapp or in the lib directory in tomcat.
Log4j doesn't seem to work properly for me unless i put the log4j properties file in the default package in the webapp's source directory within the project.
All of my log4j properties files typically look similar to the following
# Define a logger called "processLog" using the FileAppender implementation of the
# Log interface
log4j.appender.processLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.processLog.File.DateFormat='.'yyyy-ww
# Define the output location
log4j.appender.processLog.File=C:/logs/myapp.log
# Define what the output is going to look like for your log
log4j.appender.processLog.layout=org.apache.log4j.PatternLayout
log4j.appender.processLog.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm a} %5p %c{1}: Line#%L - %m%n
# log4j.rootLogger specifies the default logging level and output location.
# The first parameter is the level (debug > info > warn > error > fatal).
log4j.rootLogger=INFO, processLog
That's how I do it anyway. There may be a better way but this one always works for me.
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