I have a problem with log4j under tomcat.
When Eclipse deploys new version of application to tomcat, it reloads application context. After reloading context is completed, web application level logger stops to write to console.
Only tomcat logger still works.
My configuration in log4j.xml in web appplication is following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
I've put log4j-1.2.16.jar to /WEB-INF/lib directory of my application. Besides that I've put another copy of log4j-1.2.16.jar to tomcat's /lib directory, according to tutorial presented on official tomcat site.
Use the follow option of org.apache.log4j.ConsoleAppender. This options determines if the appender honors reassignments of System.out or System.err made after configuration. By default, false.
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Follow" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
Thanks to Paul's answer I've discovered the real reason why logger didn't log after reloading context. The problem was that on application initialization I was invoking the following code:
System.setErr( new PrintStream( new LoggingOutputStream( Logger.getLogger("outLog"), Level.ERROR ), true));
System.setOut( new PrintStream( new LoggingOutputStream( Logger.getLogger("outLog"), Level.INFO ), true));
This code is supposed to write all output written to console (like System.out.println) to xml log on production server, but during devlopment I'm using console so it has to be turned off.
When I added <param name="Follow" value="true" /> to my configuration, there was no output at all in any case, so thanks to this option I managed to discover the real problem.
Related
I want to change the log level dynamically based on TEST/PROD server accessing the application.
Our web app has a lot of DEBUG and INFO,error log statements. Once we deploy the .war file to the test / production servers,
the log4j.xml also gets deployed which results in a lot of DEBUG and INFO statements in the log files.
How to show only error logs in PROD server and debug and error logs in DEV/TEST server. Apache Tomcat is the server we are using to deploy the application.
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<!-- This appender logs to the console -->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p] [%c{1}] [%M:%L] - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.home}/logs/myProject.log"/>
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<!-- Set "debug" log level for project code -->
<logger name="com.choonchernlim.myproject">
<level value="info"/>
</logger>
<!-- Set "info" log level for Spring framework -->
<logger name="org.springframework">
<level value="info"/>
</logger>
<!-- Set "debug" log level for Hibernate framework -->
<logger name="org.hibernate">
<level value="debug"/>
</logger>
<root>
<!-- The default log level is "warn" -->
<priority value="warn"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
Included below tags in web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
One approach i could think of is to edit setenv.bat from apachetomcatserver\bin and add loglevel. But how to set loglevels dynamically in the log4j.xml file???
in tomcatserver : catalinahome\bin\setenv.bat
set loglevel=error
How to dynamically set the loglevel so that when application is deployed in TEST all logs are shown and when deployed in PROD only error log messages are shown in the log file.
I am using log4j1.2,spring,java.
You would have to create different log4j.xml-files for the corresponding environments. And then during startUp, you can simply provide the path to the log4j.xml configuration it should use.
Application parameter:
-Dlog4j.configuration=path/to/env/folder/log4j.xml
If you are using spring framework, you can use logback for logging configuration and then you can control log level based on your active spring profile.
You can get some help form following examples-
http://www.baeldung.com/spring-profiles
https://springframework.guru/using-logback-spring-boot/
I have my webapp some-app.war running under Tomcat 8.5. I use Slf4j and Logback for logging within the webapp. When I log using log.info() or similar, the output gets written to catalina.out, the same log file as Tomcat uses for its internal logging. I want the logs from my application to be written to a separate log file. But I do NOT want to use logback's FileAppender to do it. I'd prefer to leave Logback using ConsoleAppender as I may deliver the WAR to others who won't have the same logging needs. I see the location of the log file as an aspect of the deployment environment, not the code.
Is there a way that I can manage per-application log files from the configuration of the Tomcat server itself -- while still using Slf4j and Logback in my code?
You can't know about others' logging needs, so there's not a silver bullet solution for this. However I'm sure the best option is not "I'm gonna leave it configured really badly because someone might log differently".
You can configure it how you wish for yourself, using FileAppenders and document the necessary steps to reconfigure it (e.g. edit the war).
You can also use more advanced configurations such as making use of syslog. Now other users have a standardized configuration option, just as long as they use syslog.
There are other options as well, but the common thread here is that you can't know what others may want, so don't waste time trying to guess it. Just do what works well for you at least.
If you placed logging.properties at [CATALINA_HOME/conf/logging.properties] then it should use it as your root logging properties file. Put you logging.properties or log.xml file in your application at [MyApp/WEB_INF/classes/logging.properties] and configure it like blow.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stderr" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.err"/>
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.out"/>
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="2MB" />
<param name="maxBackupIndex" value="100" />
<param name="file" value="${catalina.base}/logs/MyApp/MyApp.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
I'm facing strange issue with log4j when deployed in IBM WebSphere Portal Server. Its working pretty well when tested as standalone application link here.
Its pretty straight forward RollingFileAppender xml configuraion file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="file" value="E:/logs/Mylog.log"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
<root>
<level value="DEBUG"/>
<appender-ref ref="fileAppender"/>
</root>
All I needed is when the log size Mylog.log exceeds 10MB, then it has to create Mylog.1.log, Mylog.2.log, Mylog.3.log and so on.. where as its creating Mylog.1.log, after some time Mylog.2.log will be created and Mylog.1.log will be deleted automatically (when Mylog.3.log would have been created then Mylog.1.log, Mylog.2.log would have been deleted).
Am I doing something wrong, As its enterprise web application, log4j-config.xml will be at business layer and on top of those multiple web applications will be accessing it using singleton factory pattern(One instance of log for all web application modules).
Tried many ways, but in vain, any help is highly appreciable. Thanks.
I am new to log4j and was just wondering if someone could help me with a configuration that I am currently guessing. I'm trying to test it but it doesn't seem to be working.
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/usr/local/apache-tomcat-7.0.35/logs/sample.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMax" value="DEBUG" />
</filter>
</appender>
<logger name="com.webservice" additivity="false">
<priority value="DEBUG" />
<appender-ref ref="debugAppender" />
</logger>
<root>
<priority value="ERROR" />
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
What I am trying to do here is set the root logger to ERROR so that it does not pick up anything unless there is an error. But then I have a logger named com.webservice that I would like to have pick up DEBUG statements from my packages(and not other libraries) only for development purposes.
When I go to production I want to change the com.webservice priority back to INFO so that it does not pick up any logging.
Have I set this file up correctly?
thanks
Try to set debug attribut in log4j:configuration node to true.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
It prints out information as the configuration file is read and used to configure the log4j environment. You may be got more details to resolve your problem.
I havent worked on xml but I use log4j.properties which works perfectly fine. Just place this in your classpath and add jar for log4j. It should work. You can change log levels as you want.
Also there is no need to specify ERROR level as by default ERROR level gets logged.
Sample :
log4j.debug=TRUE
log4j.logger.com.XXX.RemoteCacheManager=FATAL, Logs
log4j.logger.com.XXX.RedisCacheImpl=FATAL, Logs
log4j.logger.com.XXX.utils=DEBUG, TimeItLogs
log4j.logger.com.XXX=DEBUG, Logs
log4j.logger.org.hibernate=INFO, Logs
log4j.logger.org.springframework=INFO, Logs
log4j.logger.org.apache.velocity=ERROR, Logs
log4j.logger.org.apache.commons=ERROR, Logs
log4j.logger.org.apache.tiles=ERROR, Logs
log4j.appender.Logs=org.apache.log4j.RollingFileAppender
log4j.appender.Logs.File=${catalina.base}/logs/XXX.log
log4j.appender.Logs.MaxFileSize=10MB
log4j.appender.Logs.MaxBackupIndex=50
log4j.appender.Logs.layout=org.apache.log4j.PatternLayout
log4j.appender.Logs.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n
Right now we are using a customized logger method for our application but now we have a stand alone code for which we need to write to a separate log file. We have little idea about log4j. I just want to know where to change properties if any so that i don't disturb the existing logger application as well as we write our logs into new log file.
First define a file appender:
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/mylogfile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
Then point your package to use this appender:
log4j.logger.mypackage=LOGFILE
log4j.additivity.mypackage=false
The last line is important if you do not want your package to inherit the global appender. Doing so will result in the log messages from mypackage also printed at the default appender.
in log4j you can direct log entries to separate files based upon pacakge name.
log4j.logger.your.package1= LOG,STDOUT
log4j.additivity.your.package1=false
log4j.logger.your.package2= DEBUG, STDOUT
log4j.additivity.your.package2=false
I use XML configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="file" value="app.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d-%5p(%F:%L)-%m%n"/>
</layout>
</appender>
<appender name="myLogfile" class="org.apache.log4j.FileAppender">
<param name="file" value="myFile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d-%5p(%F:%L)-%m%n"/>
</layout>
</appender>
<logger name="org.myApp">
<level value="INFO"/>
<appender-ref ref="myLogfile" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="logfile" />
</root>
</log4j:configuration>
Just use FileAppender and specify a new filename in the constructor.