I'm trying to save log4j2 logs into user home folder because the user has not right to write where the application is installed.
This is the relevant part of my log4j settings. Unfortunately it doesn't resolve user.home and so it creates the folder $user.home where the application is running.
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="defaultConfiguration" status="warn" strict="true" monitorInterval="270">
<properties>
<property name="patternlayout">%d{dd/MM/yyyy HH:mm:ss} %5p %c{1}:%L - %m%n
</property>
<property name="filename">${user.home}/niro/logs/niroDesk.log</property>
<property name="filenamePattern">${user.home}/niro/logs/niroDesk.log-%d{yyyy-MM-dd}.log.gz
</property>
</properties>
<appenders>
<appender name="Console" type="Console" target="SYSTEM_OUT">
<layout type="PatternLayout" pattern="${patternlayout}" />
</appender>
<!-- Log su file ogni giorno, e mantenimento degli ultimi 5 files -->
<RollingFile name="File" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true" append="true">
<PatternLayout pattern="${patternlayout}" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${user.home}/niro/logs" maxDepth="2">
<IfFileName glob="niroDesk*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<appender name="AsyncFile" type="async" blocking="true" bufferSize="128">
<appender-ref ref="File" />
</appender>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="Console" />
<appender-ref ref="AsyncFile" />
</root>
<logger level="warn" name="org.springframework" additivity="false">
<appender-ref ref="Console" />
<appender-ref ref="AsyncFile" />
</logger>
</loggers>
</configuration>
%d{dd/MM/yyyy HH:mm:ss} %5p %c{1}:%L - %m%n
${user.home}/niro/logs/niroDesk.log
${user.home}/niro/logs/niroDesk.log-%d{yyyy-MM-dd}.log.gz
<appenders>
<appender name="Console" type="Console" target="SYSTEM_OUT">
<layout type="PatternLayout" pattern="${patternlayout}" />
</appender>
<!-- Log su file ogni giorno, e mantenimento degli ultimi 5 files -->
<RollingFile name="File" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true" append="true">
<PatternLayout pattern="${patternlayout}" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${user.home}/niro/logs" maxDepth="2">
<IfFileName glob="niroDesk*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<appender name="AsyncFile" type="async" blocking="true" bufferSize="128">
<appender-ref ref="File" />
</appender>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="Console" />
<appender-ref ref="AsyncFile" />
</root>
<logger level="warn" name="org.springframework" additivity="false">
<appender-ref ref="Console" />
<appender-ref ref="AsyncFile" />
</logger>
</loggers>
There is a way to solve this issue?
I solved the issue. You sould use ${sys:user.home} instead of ${user.home}.
Related
I have to migrate an old Java 8 app from Log4j 1.x to Log4j2 due to vulnerability, application is running fine without issues but logs are not appearing properly in log file.
This is the old log4j.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="ERROR" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] [%c] %m%n" />
</layout>
</appender>
<appender name="APPLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="#LOG_DIR#/#LOG_FILE_NAME#" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="15MB" />
<param name="MaxBackupIndex" value="10" />
<param name="Threshold" value="#THRESHOLD#" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] [%c] %m%n" />
</layout>
</appender>
<appender name="AUDITLOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#AUDIT_LOG_DIR#/#AUDIT_LOG_FILE_NAME#"/>
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %m%n"/>
</layout>
</appender>
<category name="com.abc" additivity="false">
<priority value="#THRESHOLD#" />
<appender-ref ref="APPLOG" />
</category>
<category name="org.httpclient" additivity="false">
<priority value="INFO" />
<appender-ref ref="APPLOG" />
</category>
<category name="org.springframework" additivity="false">
<priority value="INFO" />
<appender-ref ref="APPLOG" />
</category>
<category name="com.mchange.v2" additivity="false">
<priority value="INFO" />
<appender-ref ref="APPLOG" />
</category>
<category name="org.mortbay" additivity="false">
<priority value="INFO" />
<appender-ref ref="APPLOG" />
</category>
<category name="org.apache" additivity="false">
<priority value="INFO" />
<appender-ref ref="APPLOG" />
</category>
<category name="AUDIT_LOGGER" additivity="false">
<priority value="INFO" />
<appender-ref ref="AUDITLOG"/>
</category>
<!-- Setup the Root category -->
<root>
<priority value="#THRESHOLD#" />
<appender-ref ref="APPLOG" />
</root>
</log4j:configuration>
This is converted log4j2.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<!--<Properties>
<Property name="logPath">target/cucumber-logs</Property>
<Property name="rollingFileName">cucumber</Property>
</Properties>-->
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] [%c] %m%n"/>
<ThresholdFilter level="ERROR"/>
</Console>
<RollingFile name="APPLOG" append="true"
fileName="#LOG_DIR#/#LOG_FILE_NAME#"
filePattern="#LOG_DIR#/#LOG_FILE_NAME#.%d{yyyy-MM-dd-HH}">
<PatternLayout pattern="%d %-5p [%t] [%c] %m%n"/>
<ThresholdFilter level="#THRESHOLD#"/>
<SizeBasedTriggeringPolicy size="15 MB"/>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="AUDITLOG" append="true"
fileName="#AUDIT_LOG_DIR#/#AUDIT_LOG_FILE_NAME#"
filePattern="#AUDIT_LOG_DIR#/#AUDIT_LOG_FILE_NAME#.%d{yyyy-MM-dd-HH}">
<PatternLayout pattern="[%t] %m%n"/>
<ThresholdFilter level="INFO"/>
<!-- Causes a rollover once the date/time pattern no longer applies to the active file -->
<TimeBasedTriggeringPolicy modulate="true"/>
</RollingFile>
</Appenders>
<Loggers>
<!-- Category definitions -->
<Logger name="com.abc" level="#THRESHOLD#" additivity="false">
<AppenderRef ref="APPLOG"/>
</Logger>
<Logger name="org.httpclient" level="INFO" additivity="false">
<AppenderRef ref="APPLOG"/>
</Logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<AppenderRef ref="APPLOG"/>
</Logger>
<Logger name="com.mchange.v2" level="INFO" additivity="false">
<AppenderRef ref="APPLOG"/>
</Logger>
<Logger name="org.mortbay" level="INFO" additivity="false">
<AppenderRef ref="APPLOG"/>
</Logger>
<Logger name="org.apache" level="INFO" additivity="false">
<AppenderRef ref="APPLOG"/>
</Logger>
<Logger name="AUDIT_LOGGER" level="INFO" additivity="false">
<AppenderRef ref="AUDITLOG"/>
</Logger>
<!-- Setup the Root category -->
<Root level="DEBUG">
<AppenderRef ref="APPLOG" level="#THRESHOLD#"/>
</Root>
</Loggers>
</Configuration>
Not sure what is missing/wrong in the log4j2 file but some logs only appear in Console and not in log file even though I am expecting exactly opposite.I have configured Console for only Error messages, and log file to contain messages like Info,Debug.
I am clueless, can someone help here?
Note: #LOG_DIR#/#LOG_FILE_NAME# - These variable values are replaced during build time.
Here is my current log4j2.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="logFilename">abc</Property>
<Property name="metricsLogFilename">abc-metrics</Property>
<Property name="pattern">%d [%X{idreq} %X{iduser} %X{codeenv}] %-5p
[%c{1}] : %m%n</Property>
<Property name="metricsPattern">%d : %m%n</Property>
<Property name="level">${sys:LOG4J_LEVEL:-info}</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true" >
<PatternLayout pattern="${pattern}" />
</Console>
<RollingRandomAccessFile name="file"
fileName="${tex:appLogFilePath}${logFilename}.log"
filePattern="${tex:appLogFilePath}${logFilename}-%i.log.gz">
<PatternLayout pattern="${pattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingRandomAccessFile>
<RollingRandomAccessFile name="metricsFile"
fileName="${tex:appLogFilePath}${metricsLogFilename}.log"
filePattern="${tex:appLogFilePath}${metricsLogFilename}-%i.log.gz"
immediateFlush="false">
<PatternLayout pattern="${metricsPattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="com.myClass.common" level="info" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="${level}" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="metrics" level="info" additivity="false">
<AppenderRef ref="metricsFile" />
</Logger>
<Root level="${level}">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Root>
</Loggers>
I want to switch to stdout instead of file logs. What should I change ? (I've never used log4j)
The reason why I want to have it stdout is because I'll need to deploy my app on heroku, and on heroku we need to use system out logging.
As per current configuration, except metrics, your are logging at both places - in a file as well as on console. For metrices, you are logging in file only.
If you can want to log only at console, remove other appender and their references from logger configuration. For metrices, you have to refer console appender.
Update file may look like -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="pattern">%d [%X{idreq} %X{iduser} %X{codeenv}] %-5p
[%c{1}] : %m%n</Property>
<Property name="level">${sys:LOG4J_LEVEL:-info}</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true" >
<PatternLayout pattern="${pattern}" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.myClass.common" level="info" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="${level}" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Logger name="metrics" level="info" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Root level="${level}">
<AppenderRef ref="console" />
</Root>
</Loggers>
I am using log4j in my project. The project is working fine but it is not creating a log file.
Here is the log4j.xml
<Configuration status="warn">
<Properties>
<Property name="logHome">D:/logs</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p: %c - %m%n" />
</Console>
<RollingFile name="rollingFile" immediateFlush="true" fileName="${logHome}/mySample.log" filePattern="${logHome}/mySample-%i.log">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} |[%t]| %-5p | %c{1} | %L - %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="info" includeLocation="true" />
<Logger name="org.hibernate" level="info" includeLocation="true" />
<Logger name="org.project" level="debug" includeLocation="true" />
<Root level="info" includeLocation="true">
<AppenderRef ref="rollingFile" />
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
Any help is appreciated. Thanks in advance.
I tried all the ways out but nothing worked for me. At the end I had to switch to slf4j.Logger and slf4j.LoggerFactory. With the help of this I was able to generate logs.
Example:
private static final Logger LOGGER=LoggerFactory.getLogger(MyClass.class);
In Pingfederate, which internally uses log4j2 to log events. I am trying to rollover and delete older logs. While rollover functionality works, and the log is rolled over, but the deletion functionality doesn't seem to work. What do i don't understand ? Kindly explain and help me fix this.
Also, I am trying to log log4j2 itself, but for some reason it is not getting logged in the console logs.
Thanks a bunch.
If you need more information, kindly comment.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
</Console>
<!-- Main log : A size based file rolling appender -->
<RollingFile name="FILE" fileName="${sys:pf.log.dir}/server_${sys:pf.ip}.log" filePattern="${sys:pf.log.dir}/server_${sys:pf.ip}.log.%i" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d %X{trackingid} %-5p [%c] %enc{%m}%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000 KB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<!-- Security Audit log : A time/date based rolling appender -->
<RollingFile name="SecurityAudit2File" fileName="${sys:pf.log.dir}/audit_${sys:pf.ip}.log" filePattern="${sys:pf.log.dir}/audit_${sys:pf.ip}.%d{yyyy-MM-dd:HH:mm}.log" ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:pf.log.dir}" maxDepth="1">
<IfFileName glob="audit*.log" />
<IfLastModified age="2m" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<!-- Admin Audit log : A time/date based rolling appender -->
<!--
<RollingFile name="AdminAudit" fileName="${sys:pf.log.dir}/admin.log" filePattern="${sys:pf.log.dir}/admin.%d{yyyy-MM-dd}.log" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d|%X{user}|%X{roles}|%X{event}|%m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
-->
<!-- Admin API Audit log : A time/date based rolling appender -->
<!--
<RollingFile name="AdminApiAudit" fileName="${sys:pf.log.dir}/admin-api.log" filePattern="${sys:pf.log.dir}/admin-api.%d{yyyy-MM-dd}.log" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d|%X{api.user}|%X{api.authtype}|%X{api.clientip}|%X{api.httpmethod}|%X{api.url}|%X{api.status}%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
-->
<!-- Runtime API Audit log : A time/date based rolling appender -->
<RollingFile name="RuntimeApiAudit" fileName="${sys:pf.log.dir}/runtime-api.log" filePattern="${sys:pf.log.dir}/runtime-api.%d{yyyy-MM-dd}.log" ignoreExceptions="false">
<PatternLayout>
<charset>UTF-8</charset>
<pattern>%d|%enc{%X{api.user}}|%X{api.authtype}|%X{api.clientip}|%X{api.httpmethod}|%X{api.url}|%X{api.status}%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="httpclient.wire.content" level="WARN" />
<!-- Adjust the priority value to DEBUG to get additional logging to help troubleshoot XML Signature problems -->
<Logger name="org.sourceid.common.dsig" level="WARN" />
<!-- set to DEBUG to see inbound and outbound protocol messages -->
<Logger name="org.sourceid.saml20.bindings.LoggingInterceptor" level="WARN"/>
<Logger name="org.eclipse.jetty" level="WARN" />
<Logger name="org.eclipse.jetty.io.nio" level="WARN" />
<Logger name="org.apache.logging.log4j" level="TRACE">
<appender-ref ref="CONSOLE" />
</Logger>
<Logger name="org.sourceid.websso.profiles.sp.SpAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<Logger name="org.sourceid.websso.profiles.idp.IdpAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<Logger name="org.sourceid.websso.profiles.idp.AsAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<Logger name="org.sourceid.wstrust.log.STSAuditLogger" level="INFO" additivity="false">
<appender-ref ref="SecurityAudit2File" />
</Logger>
<!--
<Logger name="AuditLogger" level="INFO" additivity="false">
<appender-ref ref="AdminAudit" />
</Logger>
<Logger name="AdminApiAuditLogger" level="INFO" additivity="false" includeLocation="false">
<appender-ref ref="AdminApiAudit" />
</Logger>
-->
<Logger name="RuntimeApiAuditLogger" level="INFO" additivity="false" includeLocation="false">
<appender-ref ref="RuntimeApiAudit" />
</Logger>
<Root level="TRACE">
<AppenderRef ref="CONSOLE" />
</Root>
</Loggers>
</Configuration>
I am getting a lot of DEBUG messages in my logs. I want to suppress these, while preserving the INFO and ERROR messages. Here is my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- Author: Crunchify.com -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %5p %c - %m%n" />
</Console>
<RollingFile name="RollingFile" filename="test.log"
filepattern="${LOG_PATH}${LOG_NAME}Log.%d{yyyyMMdd}.log.gz">
<PatternLayout pattern="%d [%t] %5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<NoSql name="elasticAppender">
<Elasticsearch cluster="test" host="test" port="test" index="test" type="log4j2"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="NoSql" />
<AppenderRef ref="RollingFile" />
</Root>
<Logger name="test" level="info" additivity="false">
<AppenderRef ref="RollingFile" />
</Logger>
<Logger name="test" level="info">
</Logger>
<Logger name="test" level="INFO">
</Logger>
<Logger name="com.datastax.driver.core" level="INFO">
</Logger>
</Loggers>
</Configuration>
Does anyone know what change needs to be made here?
According to the Log4j manual, you might want to change <Root level="debug"> to <Root level="info">.