Log4j | Updating the Log Level for the Appender - java

My log4j.properties file -
log4j.rootLogger=INFO, stdout
# =============== console output appender =====================
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %5p: [%c{1}] %m%n
# =================== common logging =========================
# The log level for all classes that are not configured below.
log4j.logger.petascope=INFO
log4j.logger.petascope.wcps=DEBUG
log4j.logger.petascope.wcst=DEBUG
log4j.logger.petascope.wcs=DEBUG
log4j.logger.petascope.wcs2=TRACE
I want to display even DEBUG and TRACE messages on stdout, so I changed the following line
log4j.rootLogger=TRACE, stdout
But I don't see any changes when I view the logs echoes on Tomcat Console, I still see only INFO, WARN ... messages.

Well, even if you have your root logger as TRACE, your log4j.logger.petascope (pointing to INFO) will override the default root logger's TRACE for petascope.* packages, which is the reason you are not seeing any DEBUG and TRACE.
To keep things simple, try this... set the root logger to trace:-
log4j.rootLogger=TRACE, stdout
Then, comment out the following lines:-
#log4j.logger.petascope=INFO
#log4j.logger.petascope.wcps=DEBUG
#log4j.logger.petascope.wcst=DEBUG
#log4j.logger.petascope.wcs=DEBUG
#log4j.logger.petascope.wcs2=TRACE
Let me know if that works for you.

Try this:
log4j.appender.stdout.Threshold=TRACE
(as described in another StackOverflow question).

Related

Log4j.properties does not producing logs with timestamp for LOGGER.info(), LOGGER.error() statements

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.

log4j showing error when it's picking up my configuration

I'm having the same issue described here: Log4j Warning while initializing?
My application has a log4j.properties that allows me to configure log4j.rootLogger=INFO, stdout correctly. However I still see:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
When running the application.
My log4j.properties currently reads:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c:%L - %m%n
#log4j.category.org.springframework=DEBUG,stdout
I've also tried adding the properties file to my config via:
#PropertySource(value= {
"classpath:/log4j.properties",
"classpath:/application.properties"
})
How can I make this error go away? I can toggle between DEBUG and INFO just fine via this file, so I know it's being read.
You can make this error go away by adding the line
log4j.logger.org.jboss.logging=INFO, stdout
to your log4j.properties.
I don't know why, but it seems like something sets the additivity of org.jboss.logging to false, causing it to ignore the root appender(s). So you can either find that magical spot or you add the the line above.

log4j, want to show only debug(not info) in console and clear log files when app starts

I am new to this log4j and managed to setup on eclipse and get it running. I understand the chain of priority in the levels and right now this is my properties file config:
log4j.rootLogger = DEBUG, rollingFile, console
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.Threshold=INFO
log4j.appender.rollingFile.File=logs/logFile.log
log4j.appender.rollingFile.MaxFileSize=1MB
log4j.appender.rollingFile.MaxBackupIndex=5
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c: %m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
I have 2 questions for this log4j
1) Is it possible for the log4j to clear my log file each time I launch the application? I am not sure how to do this.
2) Well from the config I setup my console to print debug but what i really wanted it to print is pure debug msg instead of INFO message as well. Is there anyway to control this? Like only print debug and errors if the threshold is set to debug?
Add the below line in your log4j.properties to make it fresh everytime app starts
log4j.appender.rollingFile.Append=false
You can add logging level to your custom package also like this.
Suppose you have a package foo.bar.MyPack.You want to specify logging level for this package as info then you have to add the below line in your log4j.properties
log4j.logger.foo.bar.MyPack=info
In this way you can controll which package should be in info or which should be in debug etc.
set loglevel in your code. i.e.
private static org.apache.log4j.Logger log = Logger
.getLogger(LogClass.class);
log.setLevel(Level.Debug);
it will show only debug message
<param name="Append" value="false" />
If you set the append parameter to false, the base log file will be "started fresh" when the application restarts.

How to log stdout output in Tomcat?

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 :/

Application is not logging log, how to debug log 4j starting or not

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.

Categories

Resources