This question already has answers here:
Script to convert log4j.properties to log4j.xml
(5 answers)
Closed 9 years ago.
I couldn't find anywhere how to specify constants in log4j.xml. For example, I have this constant in my log4j.properties:
#Log directory
dal.log.dir=/var/log/pojodal/
# Log filename
dal.log.file=pojodal.log
And I use these constants as follows, in other parts of the properties file:
log4j.appender.DRFA1.File=${dal.log.dir}/${dal.log.file}
How to achieve the same behavior in log4j.xml?
You can convert your complete log4j.properties using this online service, where you can paste your log4j.properties, press convert and copy your new log4j.xml:
http://log4j-props2xml.appspot.com/
If that webapp is offline ... you can also start it in your own servlet container ... you find downloads and sources here:
https://github.com/jroyals/log4j-properties-converter/
Use of variables in log4j.xml:
This is explained in an answer to another question ... using XML internal entities here and using Java System Properties here.
You can set the log file location and name as a parameter element in the log4J xml
<param name="File" value="C:\\logs\\application\\ApplicationLog.log" />
Example file below:
<log4j:configuration>
<appender name="STDOUT" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\\logs\\application\\ApplicationLog.${user.name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{yyyy-MMM-dd HH:mm:ss}] [%t] %-5p %c{1}: %m %n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="all" />
<appender-ref ref="STDOUT"/>
</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.
My simple question is : If I have two statements like logger.info() and logger.error() in Java (using log4j), I want the results of these two lines to be printed in two separate files. That is, logger.info(...) should print to a file say myLog.info and logger.error(...) should print to myLog.error file. I am using rolling file appender for this task. Also I want just one logger object to do the task. Someone might suggest two or more different loggers one for each file, but that's not the case.
I tried searching a solution for the problem. One of the links Creating multiple log files of different content with log4j says about "threshold" and I even tried to add threshold in my xml configuration file. But what it actually is doing that : info log is being printed in myLog.info file but error log gets printed in both the files. Can it be done through xml configuration file alone or a separate properties file is needed? If xml file is sufficient, then what needs to be done?
I am preferring xml file over properties file. If there is a working solution using only xml configuration file, that would be sufficient. Thanks in advance.
You could use filters to deny any messages except those of the level you want. Here is an example of how to do this:
First a class to test our efforts:
package test;
import org.apache.log4j.Logger;
public class Main {
private static final Logger logger = Logger.getLogger(Main.class);
public static void main(String[] args) {
logger.debug("here's some debug");
logger.info("here's some info");
logger.warn("here's some warn");
logger.error("here's some error");
logger.fatal("here's some fatal");
}
}
Next a log4j.xml config file to set up the appenders and loggers:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
</appender>
<appender name="debugLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/debug.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="DEBUG" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="infoLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/info.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<logger name="test" additivity="false">
<level value="DEBUG" />
<appender-ref ref="consoleAppender" />
<appender-ref ref="debugLog" />
<appender-ref ref="infoLog" />
</logger>
</log4j:configuration>
This pattern will allow you to generate a separate log for each log level, just repeat the configuration that I have provided for either debug or info logging. Note that the console appender will accept all levels.
I was able to gain some insight from this post so I thought I should give credit.
My log4j logger does not want to use the log4j.xml file to be configured. This file is located in the src folder and looks like that:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="ROLLING" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\debug\myproject\logfile.log" />
<param name="MaxFileSize" value="10024KB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L) -%m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ROLLING" />
</root>
</log4j:configuration>
But logfile.log is still empty and there is no DEBUG line in the console.
NB: it is a Java EE project on JBoss 7.1.0 and using Struts2.
The following answer is not entirely mine. It was posted in the question itself by the OP, so I'm moving it into this community wiki answer.
I resolved it by explicitely configuring the logging configuration.
Note that you won't be able to change the configuration at runtime if you configure it using a file.
For more information, see: https://docs.jboss.org/author/display/AS71/How+To#HowTo-HowdoIuselog4j.propertiesorlog4j.xmlinsteadofusingtheloggingsubsystemconfiguration%3F
Make sure the log4j.xml file is located in the /WEB-INF/classes directory of your web application.
The problem was with the log file. If you use the absolute file name make sure to replace backslashes with slash. For example
<param name="File" value="C:/debug/myproject/logfile.log" />
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.
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>