I am using log4j2 and passing path to following configuration in my java program
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" monitorInterval="30" shutdownHook="disable">
<Properties>
<Property name="folder">.</Property>
<Property name="customFormat">[%d{ISO8601}] [%5p] [%X{loggingId}] - [%t] - [%c] - %m%n%rEx%n</Property>
<Property name="accessFormat">%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n</Property>
</Properties>
<Appenders>
<RollingRandomAccessFile name="access" fileName="${folder}/access.log"
filePattern="${folder}/access.log.%d{MM-dd-yyyy}.log.gz" immediateFlush="false">
<PatternLayout>
<pattern>${accessFormat}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingRandomAccessFile>
<RollingRandomAccessFile name="service" fileName="${folder}/service.log"
filePattern="${folder}/service.%i.log.gz" immediateFlush="false">
<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout>
<pattern>${customFormat}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="3" />
</RollingRandomAccessFile>
<RollingRandomAccessFile name="all" fileName="${folder}/all.log"
filePattern="${folder}/all.%i.log.gz" immediateFlush="false">
<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout>
<pattern>${customFormat}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="2" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.mysvc.netty.handlers.LoggingHandler" level="error"/>
<AsyncLogger name="org.apache.http" level="info" additivity="false">
<AppenderRef ref="access" />
</AsyncLogger>
<AsyncLogger name="com.mysvc.tns" level="INFO" additivity="false">
<AppenderRef ref="service" />
</AsyncLogger>
<AsyncLogger name="com.mysvc.netty" level="info" additivity="false">
<AppenderRef ref="service" />
</AsyncLogger>
<Root level="info">
<AppenderRef ref="all"/>
</Root>
</Loggers>
</Configuration>
I want to write access logs, service related logs and all other logs in files as specified in my configuration but for some reasons log4j2 is writing everything in my console, I can see all of the required files getting created but file size is 0. What's wrong in my configuration?
Related
I have log4j2.xml properties:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<RollingFile name="RollingFile"
fileName="/opt/tomcat/mylogs/logs.log"
filePattern="/opt/tomcat/mylogs/$${date:yyyy-MM}/log-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches 50 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" level="info" />
<AppenderRef ref="RollingFile" level="info" />
<AppenderRef ref="SmtpAppender" level="exception"/>
</Root>
<Logger name="com.kot" level="debug">
<AppenderRef ref="RollingFile"/>
</Logger>
</Loggers>
</Configuration>
But this properteis create logg files on another folder on server: "/opt/tomcat/mylogs/logs.log"
I want to split catalina.out logs. This logs are located on "/opt/tomcat/logs/catalina.out" by default
Also, how to split server logs and application logs?
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);
I am trying to set EST time zone in log4j.xml. Here is my log4j.xml file. Any suggestion exeperts
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" xmlns:xi="http://www.w3.org/2001/XInclude" name="testApp">
<Appenders>
<RollingRandomAccessFile name="AppLogFile" fileName="${sys:myLogDir}/dcs.log" filePattern="${sys:myLogDir}/myApp-%d{yyyy-MM-dd}.log.gz">
<PatternLayout>
<Pattern>%d{ABSOLUTE} [%-10.10t] %-5p %-32.32c{2} %x- %msg%n</Pattern>
</PatternLayout>
<DefaultRolloverStrategy max="14"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="${sys:Log4jLevel}" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="AppLogFile" />
</Root>
</Loggers>
</Configuration>
I'm using log4j2 in a java program..
this is the line of code where it is initialized
private static final Logger logger = LogManager.getLogger("application-
log");
and this is the configuration file where the format is decided
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
<Property name="logApplicativo">../logs/logApplicativo.log</Property>
<Property name="logCdr">../logs/logCdr.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%m MDC%X%n"/>
</Console>
<RollingRandomAccessFile name="fileLogApplicativo"
fileName="${logApplicativo}" filePattern="${log-Applicativo}-%d{yyyy-MM-
dd}-%i.log" immediateFlush="false" append="true"
ignoreExceptions="false" >
<PatternLayout>
<pattern>%d [%-6p] %C.%M(%F:%L) - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile >
<RollingRandomAccessFile name="fileLogCdr" fileName="${logCdr}"
filePattern="${log-Cdr}-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d %-5p %c{2} - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncRoot level="error">
<AppenderRef ref="STDOUT"/>
</AsyncRoot>
<AsyncLogger name="application-log" level="debug" additivity="false">
<AppenderRef ref="fileLogApplicativo" level="debug"/>>
</AsyncLogger>
<AsyncLogger name="cdr-log" level="debug" additivity="false">
<appender-ref ref="fileLogCdr" level="debug"/>
</AsyncLogger>
</Loggers>
I do not understand why, but class name, code name, line code arent printed. Probably the problem is trivial but I'm going crazy from one day.
Thank you all
If you're using asynchronous loggers or asynchronous appenders try adding includeLocation="true" to your logger or appender configuration.
You can read more about it in the log4j2 manual
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>