Log4j File is not generating in struts web application? - java

I'm trying to log my web app using Log4j. I've configured everything correctly. Yet log4j logging file is not generating.
Kept log4j.properties file in web-inf/classes and also added log4j.jar to build path.
log4j.properties:
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C\:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# 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
# Root logger option
log4j.rootLogger=error, file, stdout

You might created a possibly right configuration but it useless if the underlying environment isn't use it and this framework as the logging mechanism. Struts2 has also priorities or preferences for the logging frameworks to use. This priorities might change from version to version. As far as I remember from the version 2.3.12 the priority moved toward slf4j. Before the commons-logging was the priority #1.
To fix your issue you should check if you have deployed slf4j libraries then you should remove it so, the commons-logging is still a valid option but #2. You may also configure commons-logging to use log4j using commons-logging.properties. By default it uses log4j if it find the log4j configuration.

Related

spring boot application is not supporting externalisation of log4j.properties

I have spring boot application deployed on IBM Websphere and it's an expanded war file.
Now i don't want my log4j.properties to be processed as part of war file. it should be externalised.
To do that i follow below steps but it is not creating any log file at the defined location.
application.properties:-
logging.config=/XX/YY/app_config/XX_cluster_config/YY_config/log4j.properties
contents of log4j.properties:-
log4j.rootLogger=info, rollingfile
log4j.logger.XX=info
log4j.logger.YY=debug
log4j.logger.XX=error
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output - note that outputting the caller's location is extremely slow
# See http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t] %m%n %d{ISO8601} %c %n%n
log4j.appender.rollingfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.rollingfile.datePattern='.'yyyy-MM-dd
log4j.appender.rollingfile.layout=org.apache.log4j.xml.XMLLayout
log4j.appender.rollingfile.file=/XX/YY/app_logs/spring-XX/default.YY.log.xml
log4j.appender.chainsaw=org.apache.log4j.net.SocketAppender
log4j.appender.chainsaw.RemoteHost=127.0.0.2
log4j.appender.chainsaw.Port=4485
log4j.appender.chainsaw.LocationInfo=true
Somehow it is not creating the default.YY.log.xml at /XX/YY/app_logs/spring-XX/default.YY.log.xml.
if i just replace the same log4j.properties inside war then it is working but i dont want to do that.
I would like to have logging independent from war/jar.
Any help would be appreciated on this.

How to filter log4j logs besides log levels?

I need to filter some log.info console statements meant for prod maven profile, while I'm in dev.
For example:
log4j.properties
# Root logger option
log4j.rootLogger = INFO,console,file
# Direct log messages to a log file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/application.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-5X{username} %c{1}:%L - %m%n
MyClass.java
public class MyClass{
private static final Log log = LogFactory.getLog(MyClass.class);
and then in some place we use log.info writing many info for production. I want to remove this info while developing. Maybe for such production case I could use different logger, turning his output on/off in properties (depending on maven profile).
Thanks
You can use differnt logger and configure log4j to use differnt log levels for them. If you do not want some of them just set NONE as log level.

how to setup log4j configuration file

I have just started using the log4j
if i have the below log4j properties file , what should i modify in the below code to make a file
log.out for the application "test-war" deployed in apache tomcat 7.0.
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${catalina-base}/logs/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
you config file is good, I think the problem is your jsp, that did you import the class that used, and also I recomment you to use xml config of log4j
This log4j property snippet will generate log file in tomcat log directory and save the log files in log.out.xxx-yy-mm format , this will help you to manipulate the log files easily.
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE=${catalina.base}/logs/log.out
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} %-5p %c %x - %m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd

Please initialize the log4j system properly. While running web service

Maybe it looks silly to ask this but I am confused. I referred to Configuring Log4j property but it doesn't seem to help.
I have written a simple web service HelloWorld. And while running it I am getting the error something like this :
log4j:WARN No appenders could be found for logger (org.apache.axis.transport.http.AxisServlet).
log4j:WARN Please initialize the log4j system properly.
I am not sure why its happening.
I am generating the web-service using Eclipse and deployed in Tomcat 6.0. I check on Axis Developer's Guide and according to them
log4j.configuration=log4j.properties
Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties. A log4j.properties file is provided in axis.jar.
I didn't find log4j.properties in the axis.jar.
Any help on this?
Those messages are something tricky, enough so that people created this to make it clearer:
https://issues.apache.org/bugzilla/show_bug.cgi?id=25747
What's tricky about them is that the warnings are written if Log4j can't find its log4j.properties (or log4j.xml) file, but also if the file is fine and dandy but its content is not complete from a configuration point of view.
The following paragraph is taken from here:
http://svn.apache.org/repos/asf/logging/log4j/tags/v1_2_9/docs/TROUBLESHOOT.html
Logging output is written to a target by using an appender. If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:
log4j: No appenders could be found for category (some.category.name).
log4j: Please initialize the log4j system properly.
Log4j does not have a default logging target. It is the user's responsibility to ensure that all categories can inherit an appender. This can be easily achieved by attaching an appender to the root category.
You can find info on how to configure the root logger (log4j.rootLogger) in the log4j documentation, basically adding something as simple as this at the beginning of the file:
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
This should clear those WARN messages you get on startup (make sure you don't already have an appender named stdout; also be carefull of what level you give the root logger, debug will be very verbose and every library in your app will start writing stuff to the console).
As about the log4j.properties/log4j.xml, I suggest you place this file in /WEB-INF/classes as it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels etc). You can have it inside a JAR in the classpath also (as you specified in your comment), but it will be enclosed in the archive (hopefully in the right place inside the archive) and won't be as easy to handle as if it were in /WEB-INF/classes.
You have to create your own log4j.properties in the classpath folder.
Well, if you had already created the log4j.properties you would add its path to the classpath so it would be found during execution. Yes, the thingy will search for this file in the classpath.
Since you said you looked into axis and didnt find one, I am assuming you dont have a log4j.properties, so here's a crude but complete example.
Create it somewhere and add to your classpath. Put it for example, in c:/proj/resources/log4j.properties
In your classpath you simple add .......;c:/proj/resources
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to 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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c:/project/resources/t-output/log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
If the below statment is present in your class then your log4j.properties should be in java source(src) folder , if it is jar executable it should be packed in jar not a seperate file.
static Logger log = Logger.getLogger(MyClass.class);
Thanks,
Warning No appenders could be found for logger means that you're using log4j logging system, but you haven't added any Appenders (such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, etc.) into your configuration file or the configuration file is missing.
There are three ways to configure log4j: with a properties file (log4j.properties), with an XML file and through Java code (rootLogger.addAppender(new NullAppender());).
If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.
classpath
Here are some command suggestions how to determine your classpath value:
$ echo $CLASSPATH
$ ps wuax | grep -i classpath
$ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf
or from Java: System.getProperty("java.class.path").
Tomcat
If you're using Tomcat, you may place your log4j.properties into: /usr/share/tomcat?/lib/ or /var/lib/tomcat?/webapps/*/WEB-INF/lib/ folder.
Solr
For the reference, Solr log4j.properties looks like:
# Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9
#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN
# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
If you are using Logger.getLogger(ClassName.class) then place your log4j.properties file in your class path:
yourproject/javaresoures/src/log4j.properties (Put inside src folder)
You can configure log4j.properties like above answers, or use org.apache.log4j.BasicConfigurator
public class FooImpl implements Foo {
private static final Logger LOGGER = Logger.getLogger(FooBar.class);
public Object createObject() {
BasicConfigurator.configure();
LOGGER.info("something");
return new Object();
}
}
So under the table, configure do:
configure() {
Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
}

JBoss 5.0.1: log4j.properties file not taking effect in EAR

I cannot get the settings in my log4j.properties file to take effect. I've already followed the advice in the following forum discussion:
http://community.jboss.org/message/198690#198690
Here is my log4j.properties file (in the root directory of the EAR):
#
# The root logger is set to INFO by default.
# This level can be changed programmatically at runtime.
#
log4j.rootLogger=INFO, stdout, file
# use the hibernate appender for audit logs.
log4j.logger.auditLogger.com.anfscd=INFO, hibernate, HBSS
# Console appender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d %c %x - %m %n
log4j.appender.stdout.Threshold=WARN
# Use this ConversionPattern to display thread.
#log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
# Daily rolling file appender.
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.File=c:/anfscd/log/anfscd-server.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout.ConversionPattern=%-5p %d %c %x - %m %n
# Use this ConversionPattern to display thread.
#log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
# Hibernate appender
log4j.appender.hibernate=com.anfscd.common.util.database.log.HibernateAppender
log4j.appender.hibernate.sessionServiceClass=com.anfscd.cmd.model.util.persistence.HibernateHelper
log4j.appender.hibernate.loggingEventClass=com.anfscd.cmd.model.audit.AuditLogRecord
# direct log messages to windows system logs #
log4j.appender.HBSS=org.apache.log4j.nt.NTEventLogAppender
log4j.appender.HBSS.Source=Project Name
log4j.appender.HBSS.layout=org.apache.log4j.PatternLayout
log4j.appender.HBSS.layout.ConversionPattern=%-5p %d %c %x - %m %n
# suppress org.hibernate messages
log4j.logger.org.hibernate=ERROR, stdout
Here is my jboss-app.xml (in [ear]/META-INF):
<?xml version="1.0" encoding="UTF-8"?>
<jboss-app>
<module-order>strict</module-order>
<loader-repository>
com.anfscd:loader=AnfscdLoader
<loader-repository-config>
java2ParentDelegation=false
</loader-repository-config>
</loader-repository>
</jboss-app>
We're using Hibernate for persistence, and it is quite verbose in the output console. I don't want to see Hibernate console output unless there's an error.
And by the way, I'm using log4j-1.2.15.jar.
Wow! Talk about chasing your tail!
First of all, bottom line, a Log4J running in JBoss 5.x does see and process a log4j.properties file in the root of an EAR.
... but only if there is nothing else to interfere with it. For instance, if you were to package a log4j.xml in the root of your EAR along with the log4j.properties, Log4J defaults to the .xml file over the .properties file. It loads the configuration from log4j.xml and doesn't even bother to look for a log4j.properties.
Alas, that particular scenario was not my problem.
When it was all said and done, my problem was a 3rd-party JAR that contained its own log4j.properties file. As with the .xml file, if Log4J stumbles across one log4j.properties, it does not bother to look for another one. Well, for whatever reason, in JBoss 4.2.x, Log4J took the log4j.properties in the root of the EAR over the one in the JAR. In contrast, in JBoss 5.x, Log4J took the log4j.properties file in the JAR rather than the one in the root of the EAR.
I have expunged the log4j.properties file from the 3rd-party JAR, and everything works like a charm.
I stumbled onto the cause of the problem by enabling Log4J's debug mode. FYI, you can do this by starting JBoss with the switch -Dlog4j.debug or by setting debug="false" in the log4j:configuration tag in jboss-log4j.xml.
AFAIK is the root folder of the ear not added to the classpath and is the properties file as such invisible.
A better place would be in the root of a common jar file or in a WEB-INF/classes of the main war file.

Categories

Resources