I'm migrating from log4j 1.2 to log4j 2.0. Earlier all my console logs and logs were printed in specific log files. But after migrating, Console logs are printed in tomcat7-stderr.logs and other logs are printed in specific log file.
Is there a way I can print console logs also to the file I specified in log4j2.xml?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="all">
<appenders>
<Console name="STDOUT">
<PatternLayout pattern="<%X{UOWID}> [%t] %d{yyyy/MMM/dd HH:mm:ss} %-5p [%c{1}] %m%n"/>
</Console>
<RollingFile name="fileAppender" fileName="${sys:catalina.base}/logs/omp-web-services.log" filePattern="${sys:catalina.base}/logs/omp-web-services.log">
<PatternLayout pattern="<%X{UOWID}> [%t] %d{yyyy/MMM/dd HH:mm:ss} %-5p [%c{1}] %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level="INFO">
<appenderRef ref="STDOUT" level="info"/>
<appenderRef ref="fileAppender" level="info"/>
</root>
</loggers>
Related
I'm having a problem with the log4j2.xml configuration file not being found after a change to the configuration file was made. Before the change the file was found and the simple logging was working. I haven't used log4j in a few years and have been making use of the java.util logging for application logging on the weblogic server so I decided to put log4j to use again.
I first create a simple configuration file, tested it out and everything was working just fine.
The original test configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="filename">/opt/www/log</Property>
</Properties>
<Appenders>
<RollingFile name="StipLog" fileName="${filename}/stip-log.log" filePattern="${filename}/stip-log-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="stip-debug" level="debug" additivity="true">
<appender-ref ref="StipLog" level="debug"/>
</Logger>
<Root level="debug">
<AppenderRef ref="StipLog"/>
</Root>
</Loggers>
<Configuraiton>
Runing this code snippet in a backing bean for a page worked just fine with the original configuration file.
LOGGER.info("APP Home Page: User: ".concat(userInfoBean.getUserName()));
LOGGER.debug("DEBUG message!");
LOGGER.error("ERROR Message", new NullPointerException("I AM NULL"));
That all went well so I decided to try and configure different appenders for each log level I wanted to use. I changed the original configuration to the following.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<!-- For local logging change this porperty to a local directory. ex: C:\\Public\\log -->
<Property name="LOGGING_ROOT">C:/Public/log</Property>
</Properties>
<Appenders>
<!-- file appenders -->
<RollingFile name="debugLog" fileName="${LOGGING_ROOT}/app-debug.log" filePattern="${LOGGING_ROOT}/app-debug-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="infoLog" fileName="${LOGGING_ROOT}/app-info.log" filePattern="${LOGGING_ROOT}/app-info-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="errorLog" fileName="${LOGGING_ROOT}/app-error.log" filePattern="${LOGGING_ROOT}/app-error-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<!-- console appender -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="debug-logger" level="debug" additivity="true">
<appender-ref ref="debugLog"/>
</Logger>
<Logger name="info-logger" level="info" additivity="true">
<appender-ref ref="infoLog"/>
</Logger>
<Logger name="error-logger" level="error" additivity="true">
<appender-ref ref="errorLog"/>
</Logger>
<Root level="all">
<AppenderRef ref="console"/>
<AppenderRef ref="debugLog"/>
<AppenderRef ref="infoLog"/>
<AppenderRef ref="errorLog"/>
</Root>
</Loggers>
</Configuration>
After changing the configuration file I am now getting the following error message when the application is deploying.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
The configuration file is in the same location as it was when the loggin was working, it's in the source default package. I moved it WEB-INF to see if that would make a difference but the same problem remains. I even changed it back to the original configuration and still have the same problem.
I have a Cron and a Webservice, both implemented using spring. The cron and the webservice use a set of classes A, B and C to achieve their objective.
In each class, I use log4j 2 as the logging mechanism as so:
Logger log = LogManager.getLogger(A.class.getName());
In the log4j.xml, I have a single RollingAppender which logs to a file.
Now, I would like the Cron to log to a different file i.e. use a different appender. But if I set the category for the cron to use a different appender, that still doesn't cause the logs from A, B and C to go into that appender.
Update: log4j configuration:
<Configuration status="warn" name="mylogger" packages="">
<Properties>
<Property name="baseDir">/var/log/tomcat</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout><Pattern>%5p %d{ISO8601} [%t][%x] %c - %m%n</Pattern></PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
You can use below mentioned configuration if you want to log into different files using same class.
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="/data/abc.log"
filePattern="/data/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<RollingFile name="rollingFilesAppender"
fileName="/data/cde.log"
filePattern="/data/fgh-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="rollingFilesLogger" additivity="false" level="WARN">
<AppenderRef ref="rollingFilesAppender" />
</Logger>
<Logger name="com.log4jtest" additivity="false" level="INFO">
<AppenderRef ref="rollingFileAppender" />
</Logger>
</Loggers>
In Java file you can use like :
private static final Logger LOGGER = LogManager.getLogger(Hello.class);
private static final Logger SECOND_LOGGER = LogManager.getLogger("rollingFilesLogger");
Using this you will be able to send logs in two different files.
Ref : https://github.com/ragnar-lothbrok/log4j2-example
How do you configure log4j.properties to have exactly one logfile per run of an app.
I've read that you should use a timestamp in the filename but that will create many files per run as time goes by.
I tried DailyRollingFileAppender and RollingFileAppender but can't find a way to configure exctly one log per run. The log should not be broken into multiple logs and it shouldn't be truncated and files of old runs should be preserved.
Each class has a static org.slf4j.Logger for it's own class name:
private static final Logger log = LoggerFactory.getLogger(Foo.class);
This is my current log4j.properties
log4j.rootLogger=error, RootAppender, RootErrorAppender
#log4j.logger.com.example=info, qspaBackendAppender, stderr
log4j.logger.com.example=info, qspaBackendAppender
log4j.additivity.com.example=true
#log4j.logger.com.example.util=trace, qspaBackendAppender, stderr
#log4j.additivity.com.example.util=true
log4j.appender.qspaBackendAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.qspaBackendAppender.file=logs/qspaBackend.log
log4j.appender.qspaBackendAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.qspaBackendAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target=System.err
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=%-5p %c{1}:%L - %m%n
log4j.appender.RootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootAppender.file=logs/root.log
log4j.appender.RootAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootErrorAppender.file=logs/rootError.log
log4j.appender.RootErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootErrorAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender.threshold=error
I had troubles retrieving which Udo Klimaschewski's answer Udy was referring so I put here my solution.
log4j.properties:
# Root logger option
log4j.rootLogger=INFO, fileout
# Direct log messages to file
log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.File=/logs/myapp_${current.date}.log
log4j.appender.fileout.ImmediateFlush=true
log4j.appender.fileout.Threshold=debug
log4j.appender.fileout.Append=false
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=%5p | %d | %m%n
Then put in the main class this block:
public class Starter {
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhmmss");
System.setProperty("current.date", dateFormat.format(new Date()));
}
Combine the answer of Udo Klimaschewski and the answer from this question in order to get the desired result.
add the property append = false
add a current.time system property and use it in the file name
To start a new log file on app startup you may want to use OnStartupTriggeringPolicy
Triggering Policies
Example of XML config:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">C:/Logs/</Property>
</Properties>
<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} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<RollingFile name="RollingFile" fileName="${log-path}/app.log"
filePattern="${log-path}/app-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{dd/MM/yyyy HH:mm:ss} [%-5p/%t]: %C{1}(%L): %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
<OnStartupTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
<appender-ref ref="console" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Works well for me
Using filePattern Property
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<property name="filePattern">${date:yyyy-MM-dd-HH_mm_ss}</property>
</Properties>
<Appenders>
<File name="File" fileName="export/logs/app_${filePattern}.log" append="false">
<PatternLayout
pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
Someone can say me how I can change my log4j2.xml to add 1 log file: one level trace and one level info?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>
You could make the root logger TRACE level (all messages) and put a ThresholdFilter on the Console so that only some messages are displayed on the console.
This configuration will log only ERROR messages to the console, and at the same time will log all messages (TRACE, DEBUG, INFO, ERROR...) to the debug.txt file. Error and higher messages are logged both in the console and in the file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</File>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>
By the way, please be aware that your pattern layout contains conversion patterns that require location information (%C, %F and %L, to be exact).
This means that for every log message, log4j2 needs to take a snapshot of the stack, and then walk the stack trace to find the class and method that did the logging.
This is very, very slow.
Logging will be 2-5 times slower for synchronous logging and 4-20 times slower for asynchronous logging.
If performance is not an issue then it doesn't matter, but it is definitely something to be aware of. Personally I just use %c (the logger name) and my app's log messages are unique enough that I can quickly find where the message came from. Just my 2 cents.
--- update 2013-04-27 ---
I have since learned a simpler way to do this: you can set a level on the appender-ref.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p %c{2} - %m%n" /> <!--without location-->
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="%d %-5p %c{2} - %m%n" />
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="CONSOLE" level="ERROR" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>
I am trying log4j2 to create log file to the system I am developing right, I have followed the instruction on their site and there is no error occurred when I run it, but the log is not saved on where I set it (ex. "D:\logs\app.log").
Here is My log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<RollingFile name="MyRollingFile" fileName="D:/logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<logger name="Log_RollingFile" level="TRACE" additivity="false">
<appender-ref ref="MyRollingFile"/>
</logger>
<root level="ERROR">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
I tried to :
delete app.log to see if my configuration (D:\logs\app.log) works. When I run the application it creates app.log so I think it means that it sees the configuration and the only thing is it is NOT SAVING the log.info that I did in java application
Change root level to "TRACE", and it prints the log.info.
[EDIT:]
I have also these libraries on my classpath
log4j-api-2.0-beta3.jar
log4j-core-2.0-beta3.jar
Am I missing something on RollingFile configuration or a library (maybe)?
Thanks in advance.
Your logger name is incorrect.
As explained in the configuration instructions you linked to, the logger should be named according to the package/classes you wish to capture logging for.
In their example the logger named com.foo.Bar would log everything from the Bar class in package com.foo with TRACE level.