how to create log file under project base directory using log4j2 - log4j

I wanted to create log file in my maven project under project base directory.I am using log4j2 framework kept the log4j2.xml file under src/mian/resources folder. below is the file. please update what is wrong in it.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
packages="com.viveo.rtcc.common.client.traces" status="WARN">
<Properties>
<Property name="log-path">appLogs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!-- <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}
- %msg%n"/> -->
<PatternLayout pattern="[ %d ] %p %t %c : %m%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="${log-path}/reLog.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d [%t] %p %c - %m%n</Pattern>
</PatternLayout>
<Policies>
<!-- <OnStartupTriggeringPolicy /> <TimeBasedTriggeringPolicy /> -->
<SizeBasedTriggeringPolicy size="10000 KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<MessageAreaAppender name="MessageAreaAppender" />
</Appenders>
<Loggers>
<!-- <Root level="DEBUG"> <AppenderRef ref="RollingFileAppender" /> <AppenderRef
ref="Console" />
<AppenderRef ref="MessageAreaAppender" /> </Root> -->
<Logger name="com.yourloggername" level="debug"
additivity="false">
<!-- <AppenderRef ref="ConsoleAppender" /> -->
<AppenderRef ref="rollingFileAppender" />
</Logger>
<Root level="info">
<!-- <AppenderRef ref="ConsoleAppender" /> -->
<AppenderRef ref="rollingFileAppender" />
</Root>
</Loggers>
</Configuration>

Here is what I see wrong in your configuration:
Maven builds run with the working directory set to the root directory of the project. Any files creates will be respective of that. In your configuration that means it would create the logs in a directory named appLogs. This is a poor practice in Maven builds. You should change it to target/applogs.
Your filePattern contains %d{dMM-dd-yy} but none of your triggering policies are time based. Without a time-based trigger I would expect that time to never changed while the application is running.

Related

Log4J doesn't log Axis package

I cannot find the config error in this XML of Lo4jJ:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="console-log" target="SYSTEM_OUT" >
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c - %msg%n" />
</Console>
<RollingFile name="file-log" fileName="app.log" filePattern="app.%i.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="console-log" />
<AppenderRef ref="file-log" />
</Root>
<logger name="org.apache.axis" value="debug" />
<Logger name="com.myapp" level="debug"/>
</Loggers>
</Configuration>
I expect to log all org.apache.axis messages with debug level. But I can see only debug from com.myapp.
I tried to set <Root level="debug"> and between billions of messages I can see axis logs... so I'm sure that package is logging.
I'm using Log4J 2.16.0 in a tomcat container Java 1.8

Log4J change configuration runtime

I have to switch Log4J configuration at runtime during tomcat webapp startup, I have tried this solution:
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.setConfigLocation(ResourceUtils.toURI(getClass().getResource("/mynewconfig.xml")));
ctx.start();
ctx.reconfigure();
It seems to work in a Windows environment, but this doesn't work for ubuntu/linux.
I added a System.out to check the existing appender just after this code, and in windows env I can find my new configured appenders, for linux I can see only the default startup one.
I have already checked file path permissions.
This is the default log4j.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<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>
</Appenders>
<Loggers>
<Root level="warn" additivity="false">
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</Configuration>
And this is the new one (mynewconfig.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<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="file-log" fileName="${sys:home}/log/app.log" filePattern="${sys:home}/log/app.%i.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
<RollingFile name="file-critico-log" fileName="${sys:home}/log/critico.log" filePattern="${sys:home}/log/critico.%i.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.mypackage" level="info" additivity="false">
<appender-ref ref="file-log" />
<appender-ref ref="console-log" />
</Logger>
<Logger name="critico" level="info" additivity="false">
<appender-ref ref="file-log" level="error" />
<appender-ref ref="file-critico-log" level="error" />
<!-- appender-ref ref="console-log" level="error" / -->
</Logger>
<!-- Logger name="org.springframework.jdbc.core.JdbcTemplate" level="debug">
<appender-ref ref="console-log" />
</Logger-->
<Root level="warn" additivity="false">
<AppenderRef ref="file-log" />
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</Configuration>
The problem was the Sandboxing filesystem of tomcat service in Ubuntu.
Take a look at https://www.freedesktop.org/software/systemd/man/systemd.exec.html
I hade to explicit set this configuration:
ReadWritePaths=/var/mypath
in /lib/systemd/system/tomcat.service file

How to add uuid in log filename .I am using log4j2?

i want to create log filename which includes uuid in name .
<Configuration status="DEBUG">
<Appenders>
<RollingFile name="file" fileName="log/${uuid:TIME}-test.log"
append="true" filePattern="log/${uuid:TIME}-test.log.%i">
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="5" />
<PatternLayout>
<Pattern>%d %-5p [%c{3}] (%t) %m%n</Pattern>
</PatternLayout>
</RollingFile>
<Async name="async">
<AppenderRef ref="file" />
</Async>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%c{2}]%m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="Console" />
<AppenderRef ref="async" />
</Root>
<Logger name="com.citigroup" level="INFO" additivity="false">
<Appender-ref ref="async" />
</Logger>
</Loggers>
</Configuration>
but it is creating file "uuid:TIME-test.log".I have also try %uuid , %u but none of them is creating file with uuid .
This can be accomplished with a custom lookup. This answer gives example code for how to create a custom lookup in Log4j2 (it's only a few lines of code).

log4j2 custom layout not working with Rolling appender file

I'm working with log4j2 and rolling appender file.
I want to use a customize layout but it's not working properly.
I'm working with JBoss. I have put the lib with the cusotmize layout in the libs directory, so it's in the class path.
In the log4j2.xml, I have put the following configuration:
<Configuration status="trace" packages="mypackage.audit">
...
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:FlowName">
<Route>
<RollingFile name="Rolling-${ctx:FlowName}" fileName="logs/Audit-${ctx:FlowName}.log"
filePattern="./logs/Audit-${ctx:FlowName}-%d{yyyy-MM-dd}-%i.log.gz" >
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c{3} - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
</Policies>
<Layout type="AuditLayout" locationInfo="true"/>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Logger name="CustomizeAuditing" level="info" >
<AppenderRef ref="RoutingAppender"/>
</Logger>
</Loggers>
</Configuration>
But nothing is logged correctly!
Thanks in advance
Jamila
following worked for me:
log4j2 version 2.2
and
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.redknee.bssauto.helpers">
<Appenders>
<RollingFile name="Rolling-default" fileName="logs/bssauto.html"
filePattern="logs/$${date:yyyy-MM}/bssauto-%d{MM-dd-yyyy}-%i.log.gz">
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Rolling-default"/>
</Root>
</Loggers>
</Configuration>
Notice following
<Configuration packages="com.redknee.bssauto.helpers">
here packages should have all packages containing custom class for layouts
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
and CustomHTMLLayout is custom class created by extending AbstractStringLayout

log4j logging to console but not File blazeDS

I am seeing strange behavior using log4j that I am completely stumped on. This is my first time using it with BlazeDS and I am hoping it is a small issue I have missed.
In this web app, I am using log4j for logging from 2 java classes, as well as blazeDS. I am hooking BlazeDS into commons-logging via org.springframework.flex.core.CommonsLoggingTarget, and then using log4j as the underlying logger.
I have set appenders for both "Console" and "MyFile" under the blazeds logger, and while the console output is working as expected, nothing is written to MyFile. The file is in fact created, but stays at 0KB. On the other hand, my two java classes are logging just fine both to the console and their rolling file. I am seeing this behavior both when I test locally and also when I deploy to Tomcat.
One last detail - when testing locally, I changed the MyFile path to an absolute path, and saw that the file was beign appended to. This would lead me to believe it is an issue with the relative path however it has been working fine for my java logging.
commons-logging.properties:
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="trace" debug="true">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/ImpactTradeQuery.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="25 MB"/>
</Policies>
<DefaultRolloverStrategy max="25"/>
</RollingFile>
<File name="MyFile" immediateFlush="true" fileName="logs/app.log">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="Console"/>
</root>
<logger name="flex.samples.trade.tradeService" level="trace" additivity="False">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="flex.samples.ConnectionHelper" level="trace" additivity="False">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="blazeds" level="trace" additivity="True">
<appender-ref ref="MyFile"/>
<appender-ref ref="Console"/>
</logger>
</loggers>
</configuration>
services-config.xml:
<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<properties>
<prefix>[BlazeDS] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
<target class="org.springframework.flex.core.CommonsLoggingTarget" level="All">
<properties>
<categoryPrefix>blazeds</categoryPrefix>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>
Turned out to be a missing jar file in Tomcat (log4j-jcl) that was causing the issue. Once the library was properly packaged with the app the logging worked as expected.

Resources