The automatic reload was working up until I added a second file to the log4j.configurationFile property. I now get this error when updating the log4j2.xml file, or rather I get the same error message twice...
2017-10-02 12:06:44,461 Log4j2-TF-2-ConfiguratonFileWatcher-2 ERROR No logging configuration
2017-10-02 12:06:44,462 Log4j2-TF-2-ConfiguratonFileWatcher-2 ERROR No logging configuration
Here's the problematic property...
-Dlog4j.configurationFile=log4j2.xml,c:\tmp\override-log4j2.xml
Note, this only affects reload. Initially, the composite configuration works fine.
This is a maven project, ran in eclipse using jetty:run and log4j2 2.9.0.
Is this a bug? Is there a workaround?
Here's the log4j2.xml...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration monitorInterval="5">
<appenders>
<console name="console" target="SYSTEM_OUT">
<patternLayout pattern="%d %5p [%t] [%X{users}] (%F:%L) - %m%n" />
</console>
<rollingFile name="rollingFile" fileName="myApp.log" filePattern="myApp.log">
<patternLayout pattern="%d %5p [%t] [%X{users}] (%F:%L) - %m%n" />
<policies>
<SizeBasedTriggeringPolicy size="10mb" />
</policies>
<defaultRolloverStrategy max="10" />
</rollingFile>
</appenders>
<loggers>
<root level="warn">
<appenderRef ref="console" />
<appenderRef ref="rollingFile" />
</root>
<logger name="com.mycompany" level="info" />
</loggers>
</configuration>
Here's override-log4j2.xml...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<loggers>
<root level="warn" />
</loggers>
</configuration>
Thanks for the update #gaurav.
Apparently, (I've not tested) the ticket was resolved in 2.10.0.
Related
This is my configuration log4j2.xml with path to file - src/com/tarasiuk/task_01/log/dataLogger.log:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="FORMAT_MESSAGE">
%d{YYYY-MM-dd HH:mm:ss} [%t] Level:%-7p Class:%c{1}:%-5L - %msg%n
</Property>
<Property name="LOG_FILE">
src/com/tarasiuk/task_01/log/dataLogger.log
</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${FORMAT_MESSAGE}" />
</Console>
<File name="File" fileName="${LOG_FILE}" append="false">
<PatternLayout pattern="${FORMAT_MESSAGE}" />
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Structure of my project:
What I do:
change path to log file from src/com/tarasiuk/task_01/log/dataLogger.log to com/tarasiuk/task_01/log/dataLogger.log - no result.
change level in <Logger> from debug to info - no result.
Logs are output to the console - that ok. But why Log4j2 doesn't write logs to a file?
Try with below appender.
May be in your case it is not able to get path from property, so i had provided only name.
So automatically it will create file on same path as your application is.
<Appenders>
<File name="dataLogger" fileName="dataLogger.log" append="false">
<PatternLayout pattern="%level - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%level - %m%n"/>
</Console>
</Appenders>
This will help you.
Here is a simple configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="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>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Actually what is the purpose of Configuration status="warn" and <Root level="error">. How those level impact in log file?
<Configuration status="warn"> is for Log4j internal events only.
<Root level="error"> is configuration for the root logger, it will apply the error log level for all logs except the ones configured in separate Loggers (which you don't have in the above configuration).
I have a maven web project which requires logging.
I decided to use log4j2 for that purpose and added the required entries in the pom.xml file
Then I created the log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="RollingFile" fileName="${logs.path}/text.log"
filePattern="${logs.path}/%d{YYYYMMdd}-text.%i.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} %-5p %c{1}:%L - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="root" level="info" additivity="false">
<appender-ref ref="RollingFile" level="info" />
<appender-ref ref="Console" level="info" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I'm starting tomcat with -Dlogs.path="C:\mylogs", where C:\mylogs exists and has public read/write access.
The console logger is working fine, I can see the correct output in my console, the issue is that the file isn't being created, so I have no logfile, everything gets logged to catalina.out only, and the tomcat startup logs don't show errors for log4j.
What am I missing? is there some additional configuration I need to do in order to log to file?
I went through the documentation. You must refer to system properties with sys:, and it seems that tomcat properties are seen as system properties, so I replaced ${logs.path} with ${sys:logs.path} and it worked.
To create a log file with the log4j2 config you first need to define your appender. Here's an example:
<RollingFile name="MGMT"
fileName="${logdir}/mgmt.log"
filePattern="${logdir}/mgmt.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="mgmt.*.log" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
Afterwards you just need to define a logger that will use it:
<Logger name="the package or name that is displayed in the log line"
level="ALL"
additivity="false">
<AppenderRef ref="MGMT"/>
</Logger>
If you have a log that has com.company.test as package set that package as name for the logger. It's important that they match.
The field additivity will define if you want to pass the catched log to its parent ( true ) or just let this logger handle it ( false )
EDIT:
your config file might need this:
<Configuration status="info">
<Properties>
<Property name="logdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d [%t] %-5p %c- %m%n</Property>
</Properties>
<Appenders>
If you don't want to use this you have to define the path static like <RollingFile name="MGMT"
fileName="C:\path\file.log"
I've implemented async logging with log4j 2, but now I need to change log filename every hour, eg 2015-11-19/log-12.00.log, 2015-11-19/log-13.00, etc. (Soulutions I've found didn't work, may be I did something wrong).
I have following log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Don't forget to set system property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous. -->
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="async.log" immediateFlush="false" append="true">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<Root level="info" includeLocation="false">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
How to achive this?
You should have a look at TimeBasedTriggeringPolicy. Basically it causes a rollover once the date/time pattern no longer applies to the active file.
Have't tried this but this should work for you.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" >
<appenders>
<Async name="Async">
<AppenderRef ref="logfile" />
</Async>
<RollingRandomAccessFile name="logfile" fileName="async.log" filePattern="log-%d{HH}.00.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="24"/>
</RollingRandomAccessFile>
</appenders>
<loggers>
<root level="INFO" includeLocation="false">
<AppenderRef ref="Async"/>
</root>
</loggers>
</configuration>
The TimeBasedTriggeringPolicy interval attribute controls how often a rollover should occur based on the most specific time unit in the date pattern. In you case it is hours that is %d{HH} from file pattern.
Is it possible to define a (named) PatternLayout within a log4j2.xml configuration file?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR">
<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="trace">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>
In this example the PatternLayout is repeated. Could I define it somehow globally with a name, and then just use the name reference to set the pattern for each appender?
According to the official documentation, you can define a "property" and reuse its value.
In this case, I am defining the appenderPatternLayout property.
Here the full code (tested with Log4J 2.3) :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="appenderPatternLayout">%d %-5p %C{2} (%F:%L) - %m%n</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${appenderPatternLayout}" />
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="${appenderPatternLayout}" />
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="DEBUG_FILE" />
</Root>
</Loggers>
</Configuration>
Found it:
define:
<Properties>
<property name="path">c:/logs/</property>
</Properties>
use:
<RollingFile fileName="${path}mylog.log"