spring boot application is not supporting externalisation of log4j.properties - java

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.

Related

How to log with same log4j.properties files for two different Spring-Boot Applications?

Suppose I have two Spring-Boot applications called config and eureka.
config is made up of:
config
/src/main
/java
/com.example
Demo.java
/resources
application.properties
eureka is made up of:
eureka
/src/main
/java
/com.example
Demo.java
/resources
application.properties
I have a log4j.properties files
log4j.rootLogger=INFO, stdout, file
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.SSS}] - %p [%t] --- %c : %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log.out
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{yyyy:MM:dd HH:mm:ss.SSS}] - %p [%t] --- %c : %m%n
One way to log both config and eureka is by keeping a copy of this properties file in resources folder of both applications and giving a common path for the log file in both. I have tried this and this works correctly.
I want to do something different. I want to maintain a single log4j.properties file instead of two for both applications. My question is what is the proper way to do this? How?
You can keep log4j.properties in common location (in Windows c:\boots), refer it in application.properties.
logging.config = C:\boots\log4j.properties

Log4j File is not generating in struts web application?

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.

log4j property change not getting reflected in Eclipse

This was the log4j file I used for my project till I checked in my project to a SVN repository. This was working fine and I saw log information on Eclipse's console
log4j.rootLogger=debug, stdout, R
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=%d - %p %t %C.%M (%F:%L) %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=x.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %p %t %C.%M (%F:%L) %m%n
I checked out my project from repository and disabled logging by making this change in my log4j property file.
#log4j.rootLogger=debug, stdout, R
log4j.rootLogger=OFF
But this change didn't work as expected, I still got the log information on Eclipse's console. When I cross checked the same on Terminal, it was fine, I didn't get log info on Terminal. Any problem with Eclipse? I searched a lot on this issue, didn't get any solution. Help me in finding out what's wrong here!!!
There are two possible issues here:
the configuration should say (to tell it no appenders, instead of looking for OFF appender):
log4j.rootLogger=
Eclipse is using an old version of the file, likely because you edited the file outside of Eclipse. Just refresh the file (Right click / Refresh) and it should start working as you expect.
Use the -Dlog4j.debug property to output log4j-internal debugging information and see which logging configuration is really loaded on startup
You will get some information like this (for xml based configuration but I suppose you will get similar informations with property file configurations):
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader#11b86e7.
log4j: Using URL [file:/C:/develop/workspace/foobar/target/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
The 'log4j: Using URL ...' line comes from the LogManager class.

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