2

I have the following log4j2 configuration:

<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Properties> <Property name="LOG_DIR">${sys:web.root}/logs</Property> <Property name="ARCHIVE">${LOG_DIR}/archive</Property> </Properties> <Appenders> <Console name="console-log" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/> </Console> <RollingFile name="paypal-log" fileName="${LOG_DIR}/paypal.log" filePattern="${ARCHIVE}/paypal.log.%d{yyyy-MM-dd-hh-mm}.gz"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/> <Policies> <TimeBasedTriggeringPolicy interval="5" modulate="true"/> </Policies> </RollingFile> </Appenders> <Loggers> <Logger name="com.retailstore.paypal" level="info" additivity="false"> <appender-ref ref="paypal-log" level="info"/> </Logger> <Root level="error" additivity="false"> <AppenderRef ref="console-log"/> </Root> </Loggers> 

In my (Eclipse) web application I have a class com.retailstore.paypal.paypalfunctions in which I have

private static final Logger logger = LogManager.getLogger(paypalfunctions.class.getName()); ... logger.error("paypal error"); 

But there is no logs/paypal.log under the Webcontent folder. Where should it appear? If I remove the logger the console output seems to work.

OUTPUT with TRACE:

2016-05-23 19:24:03,231 http-nio-8080-exec-2 TRACE PatternProcessor.getNextTime returning 2016/05/23-19:25:00.000, nextFileTime=2016/05/23-19:24:00.000, prevFileTime=2016/05/23-19:14:00.000, current=2016/05/23-19:24:03.230, freq=EVERY_MINUTE 2016-05-23 19:24:03,247 http-nio-8080-exec-2 TRACE DefaultRolloverStrategy.purge() took 15.0 milliseconds 2016-05-23 19:24:03,254 http-nio-8080-exec-2 DEBUG RollingFileManager executing synchronous FileRenameAction[${sys:web.root}/logs/paypal.log to ${sys:web.root}/logs/archive/paypal.log.2016-05-23-07-14, renameEmptyFiles=false] 2016-05-23 19:24:03,256 http-nio-8080-exec-2 DEBUG RollingFileManager executing async GzCompressAction[${sys:web.root}/logs/archive/paypal.log.2016-05-23-07-14 to ${sys:web.root}/logs/archive/paypal.log.2016-05-23-07-14.gz, deleteSource=true] 
0

2 Answers 2

1

Try setting <Configuration status="trace">. This will show log4j-internal debug statements giving you insight into how log4j is being configured.

Here is how you can define a default value in case the system property is not set correctly:

<Properties> <Property name="web.root">/path/to/web/root</Property> <Property name="LOG_DIR">${sys:web.root}/logs</Property> <Property name="ARCHIVE">${LOG_DIR}/archive</Property> </Properties> 

LOG_DIR will now look in the system properties, but if not found it will fall back to the value defined in the configuration.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I will try that and reply
Still no idea where it is??
I tried ~/logs but nothing shows up in my home dir.
OK, it works with /Users/xxxx/logs but not with ~/logs. Would like to have a portable path, how do I do that?
0

What is the web.root system property set to? The file should appear under the logs directory under wherever that directory is pointing. If it is undefined Log4j will create a directory named ${sys:web.root} and the file will be in the logs directory under that.

As Remko suggests, enabling trace will tell you definitively where it was created.

2 Comments

I'm not sure what you mean. Yes this technique will work under tomcat so long as you define the web.root system property. Under tomcat you can also use ${sys:catalina.home} or ${sys:catalina.base} if you want the file located somewhere under where tomcat is running.
Create a Jira issue for Log4j2 and we can look at them there. issues.apache.org/jira/browse/LOG4J2