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>
Related
First, I have found a SO question that seems very relevant:
How can I set up separate streams of logging for log4j?
The first answer to the question discusses using Markers and this is what I'd like to try to use. However, I have not been able to get the logging to actually work as expected. I don't know if I'm missing a step or using the wrong syntax.
I wrote a really simple program in an attempt to test this out.
package my.package;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
public class LoggingTest {
private static final Logger logger = LoggerFactory.getLogger(LoggingTest.class);
public static void main(String[] args) {
Marker marker = MarkerFactory.getMarker("MARKER");
logger.info(marker, "log info to marker log");
logger.info("log info to regular log");
}
}
log4j.xml:
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- APPENDERS -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p: %c{2}:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="file" value="session.log" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p: %c{2}:%L - %m%n" />
</layout>
</appender>
<appender name="marker-file" class="org.apache.log4j.FileAppender">
<param name="file" value="marker.log" />
<param name="append" value="true" />
<MarkerFilter marker="MARKER" onMatch="ACCEPT" onMismatch="DENY" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p: %c{2}:%L - %m%n" />
</layout>
</appender>
<!-- LOGGERS -->
<root>
<level value="INFO" />
<appender-ref ref="marker-file" />
</root>
</log4j:configuration>
The logging right now is just set to log to the marker appender. I would need to figure out how to set up the logger(s) to send "marker" output to the marker appender and the rest to the "regular" appender.
I don't know if the syntax of the usage of the MarkerFilter element is correct or not.
When I execute the program, I am specifying the VM argument -Dlog4j.configuration=file://<path to log4j.xml>.
I'm running this from within eclipse and I'm having a separate problem where it doesn't seem to be recognizing the log4j.xml file I'm giving it. I assume this is something I'm doing wrong (and is probably the reason why what I'm trying to do does not appear to work).
Any ideas as to what I might be doing incorrectly as far as getting the log4j.xml to be recognized, and then if that all checks out, whether I'm using the filter/marker mechanism correctly or not, would be greatly appreciated.
UPDATE:
I am doing this on a Windows box, so I suspect something is amiss with how I'm specifying the file URI to the log4j.xml file.
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.
This is a standalone java application.
I am using the configuration file below and having two problems.
1) I'm getting logs to stdout and I don't know why.
2) I'm getting ALL log messages in my error log even though I have tried to direct only error and higher to the error log.
I am using the BasicConfigurator without specifying an explicit path to the log4j.xml file. The xml file is in the same jar as my classes. It is creating and writing to the appropriate logs in addition to these problems so the configuration is being applied.
3) In addition, I have had no luck having the log4j.xml file outside of the jar so I can change it at runtime. How do I do that?
<!--appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p [%F:%L] - %m%n"/>
</layout>
</appender-->
<!-- working dir is $CATALINA_TMPDIR. send logs to log dir. -->
<appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/company/application.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ERRORLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/rocketvox/company/error.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<category name="com.company">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<category name="com.mattx">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<root>
<priority value="error"/>
<appender-ref ref="ERRORLOG"/>
</root>
Add -Dlog4j.debug to the application's JVM args to see exactly what log4j is doing and which configuration file it uses.
Your problem is using BasicConfigurer - for configuring using a file named log4j.xml, you don't need to use any explicit log4j Configurer in your application code, as the default log4j initialization logic will kick in to pick log4j.xml (or log4j.properties, if no xml is found) from the classpath.
See Default Initialization Procedure from the log4j manual.
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.
This question already has answers here:
Where does java.util.logging.Logger store their log
(3 answers)
Closed 9 years ago.
Netbeans thoughtfully sprinkles Logger.getLogger(this.getClass().getName()).log(Level. [...]
statements into catch blocks. Now I would like to point them all to a file (and to console).
Every logging tutorial and such only me tells how to get a specific logger to output into a file, but I assume there is a better way than fixing every automatically generated logging statement? Setting a handler for some sort of root logger or something?
I just add the following at startup
Handler handler = new FileHandler("test.log", LOG_SIZE, LOG_ROTATION_COUNT);
Logger.getLogger("").addHandler(handler);
You can specify your own values for LOG_SIZE and LOG_ROTATION_COUNT
You may need adjust the logging level to suit.
You have to define where the log is writting in the logger configuration file.
For example, if you use log4j, a log4j.xml (or log4j.properties) file will contain such information.
For example, here is a simple log4j.xml file that logs directly into a file (my-app.log) and in the console:
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="my-app.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%C] [IP=%X{ipAddress}] [user=%X{user}] %m%n" />
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%C] [user=%X{user}] %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="console" />
<appender-ref ref="rolling" />
</root>
</log4j:configuration>