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);
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'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>
I have a Cron and a Webservice, both implemented using spring. The cron and the webservice use a set of classes A, B and C to achieve their objective.
In each class, I use log4j 2 as the logging mechanism as so:
Logger log = LogManager.getLogger(A.class.getName());
In the log4j.xml, I have a single RollingAppender which logs to a file.
Now, I would like the Cron to log to a different file i.e. use a different appender. But if I set the category for the cron to use a different appender, that still doesn't cause the logs from A, B and C to go into that appender.
Update: log4j configuration:
<Configuration status="warn" name="mylogger" packages="">
<Properties>
<Property name="baseDir">/var/log/tomcat</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout><Pattern>%5p %d{ISO8601} [%t][%x] %c - %m%n</Pattern></PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
You can use below mentioned configuration if you want to log into different files using same class.
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="/data/abc.log"
filePattern="/data/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<RollingFile name="rollingFilesAppender"
fileName="/data/cde.log"
filePattern="/data/fgh-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="rollingFilesLogger" additivity="false" level="WARN">
<AppenderRef ref="rollingFilesAppender" />
</Logger>
<Logger name="com.log4jtest" additivity="false" level="INFO">
<AppenderRef ref="rollingFileAppender" />
</Logger>
</Loggers>
In Java file you can use like :
private static final Logger LOGGER = LogManager.getLogger(Hello.class);
private static final Logger SECOND_LOGGER = LogManager.getLogger("rollingFilesLogger");
Using this you will be able to send logs in two different files.
Ref : https://github.com/ragnar-lothbrok/log4j2-example
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">.