This is mt Log4j:
log4j.rootLogger=ERROR, CA, FA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.Target=System.out
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n
log4j.appender.FA=org.apache.log4j.RollingFileAppender
log4j.appender.FA.File=${catalina.base}/logs/Z2.log
log4j.appender.FA.MaxFileSize=5MB
log4j.appender.FA.MaxBackupIndex=10
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] [%c:%L]: %m%n
log4j.logger.com.datastax.driver.core=ERROR
log4j.logger.com.dcf=DEBUG
Now i am using tomcat8, befor i was using tomcat7 it works great, and save the log file under /var/log/tomcat7/logs/Z2.log
After i uninstall tomcat7 and install tomcat8, i can't find the z1.log file.
log4j.appender.FA.File=${catalina.base}/logs/Z2.log
I don't have enough reputation to comment but is 'CATALINA_BASE' being set as an environmental property?
ie either inferred from 'CATALINA_HOME' or explicitly set?
What happens when you enable debugging for log4j?
This may show why the log file is not being created etc
What I generally do is leave the container logging as it stands but use log4j in any web / EE apps. By doing this, container logging will work with no changes and any web / EE apps are independent ie it is easier to change web / EE containers if necessary.
For Tomcat 8, the native logging library is 'JULI' which implements several key elements of the java.util.logging API.
The following configuration works for
Tomcat 8.0.30
jdk1.8.0_66
log4j 2.5
[web-app-name] / WEB-INF / lib
commons-logging-l.2.jar
log4j-api-2.5.jar
log4j-core-2.5.jar
log4j-jcl-2.5.jar
[web-app-name] / WEB-INF / classes / log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="LOG_FILE_PATH">${sys:catalina.home}/logs</Property>
<Property name="PATTERN_JBOSS" >%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %m%n</Property>
<Property name="PATTERN_TOMCAT">%d{dd-MMM-yyyy HH:mm:ss.SSS} %-5p [%t] %m%n</Property>
<Property name="PATTERN" >${PATTERN_JBOSS}</Property>
</Properties>
<Appenders>
<Console name="console">
<PatternLayout pattern="${PATTERN_TOMCAT}"/>
</Console>
<RollingFile name="daily-file" fileName ="${LOG_FILE_PATH}/globaltrax.log"
filePattern="${LOG_FILE_PATH}/globaltrax-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${PATTERN_JBOSS}"/>
<TimeBasedTriggeringPolicy/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="console"/>
</Root>
<logger name="lpms.web.PurchaseOrderAction" level="TRACE">
<AppenderRef ref="console"/>
</logger>
<logger name="lpms.web.PurchaseOrderAction" level="TRACE">
<AppenderRef ref="daily-file"/>
</logger>
</Loggers>
</Configuration>
This defines one console logger and one daily log file logger which is typical.
Note that ‘CATALINA_HOME’ must be set as a ‘system’ environmental variable - this is used in the line:
<Property name="LOG_FILE_PATH">${sys:catalina.home}/logs</Property>
Related
I have upgraded my log4j from 1.2.17 to 2.16.0. Therefore I had to rewrite my log4j.xml as well as to rename it to log4j2.xml. I have made the necessary changes. However it seems that my new config is not loaded. I have tried to configure log4j through both web.xml :
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:log4j2.xml</param-value>
</context-param>
and -Dlog4j2.configurationFile. Both approaches failed and I am not getting any logs in mylog.log file. However when running the application locally the Console display the logs.
This is my log4j2.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="trace">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p %c{1} - %m%n"/>
</Console>
<RollingFile name="DAILY_LOG" fileName="${catalina.base}/logs/mylog.log"
filePattern="${catalina.base}/logs/mylog.log.%d{yyyy-MM-dd}">
<PatternLayout pattern="%d %-5p [%c] [%X{sid}:%X{uid}] %m%n "/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- Limit the org.springframework category to INFO as its DEBUG is verbose -->
<Logger name="org.springframework" level="INFO"/>
<!-- Limit the org.hibernate category to INFO as its DEBUG is verbose -->
<Logger name="org.hibernate" level="INFO"/>
<!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
<Logger name="org.apache" level="INFO"/>
<Logger name="org.myproject" level="INFO"/>
<!-- Root Logger -->
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="DAILY_LOG"/>
</Root>
</Loggers>
</Configuration>
I have tried to debug log4j configuration by using : -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=trace. I have noticed that the LoggerContext instance is well created and all listed loggers are as well built.
I wonder if my config is wrongly written. Can someone please confirm or deny this to me.
I found the solution. This took me a while to figure out. The problem was due to the new syntax used by log4j 2.x to resolve system properties. Unlike log4j 1.x which used ${some.property} syntax for property substitution , log4j 2.x uses ${sys:some.property}. I had to add that sys prefix to make it work.
Source : https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution
I have a webapp with it's own classes under its WEB-INF folder, which is using log4j2 for logging. I configured it with log4j2-web and can see logs from those classes. So far so good.
I am wrecking my brain, however, getting to see logs from libs (jars) that are defined in the common/shared classpath of tomcat. A jar (one for example) I am trying, is using log4j2 api too for logging.
Note that only when I move the jar from the common classpath to lib folder under the webapp's WEB-INF it does show logs. Meaning the configuration file is fine regarding appender and logger names...
Is there something I'm missing or this is how log4j2 works?
I am using Tomcat 7
Log4j2 2.11.0
Any help would be welcomed! Thanks.
The (simplified) log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<RollingFile name="sample.appender" fileName="server.log"
filePattern="server.log.%i.gz" bufferedIO="false" bufferSize="0">
<PatternLayout pattern="%d [%t] (%F:%L) %-5p - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="5242880"/>
</Policies>
<DefaultRolloverStrategy max="13"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.my.server" level="DEBUG" additivity="false">
<AppenderRef ref="sample.appender"/>
</Logger>
<Logger name="com.3rd.party.sample" level="DEBUG" additivity="false">
<AppenderRef ref="sample.appender"/>
</Logger>
<Root level="FATAL"/>
</Loggers>
Background: I have log4j.xml file configured for our spring based application, which looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="FATAL" shutdownHook="disable" packages="com.gemstone.gemfire.internal.logging.log4j">
<Properties>
<Property name="gemfire-pattern">[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid %C{1.}] %message%n%throwable%n< /Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${gemfire-pattern}"/>
</Console>
<RollingFile name="eventLogFile" fileName="/opt/data/service/logs/events.log"
filePattern="/opt/data/service/logs/events-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS} %p - %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="20" fileIndex="max"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.gemstone" level="INFO" additivity="true">
<filters>
<MarkerFilter marker="GEMFIRE_VERBOSE" onMatch="DENY" onMismatch="NEUTRAL"/>
</filters>
</Logger>
<Logger name="com.app.mypackage" level="INFO" additivity="true">
<AppenderRef ref="eventLogFile"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
Now, I want log4j to write log statements with let's say 'countryName'. And, this 'countryName' should be configured via external property file.
For e.g, the "gemfire-pattern" will have this externalised property name $${countryName}.
<Property name="gemfire-pattern">[$${countryName} %level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid %C{1.}] %message%n%throwable%n< /Property>
Considering this log4j system properties, in my case, the log4j.component.properties is not being picked up by log4j.
Any thoughts on how to fetch a property value from external properties file in log4j.xml?
References:
how-to-read-property-variable-from-file-into-log4j2
log4j system properties
log4j properties substitution
Thanks in advance.
log4j.component.properties is used to add log4j specific system properties like log4j.configurationFile, org.apache.logging.log4j.level etc.
To refer to user defined properties, include the property file inside the logback configuration and refer to the keys using ${KEY}
<configuration>
<property file="country.properties" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${countryName}</file>
...
Logback also allows you to externalise parts of the configuration using File inclusion feature.
https://logback.qos.ch/manual/configuration.html#fileInclusion
<configuration>
<include file="src/main/java/chapters/configuration/includedConfig.xml"/>
...
Make sure the content in the external xml file is encolsed with <included> </included> tag
Note- System properties(-Dcountry="") and Environment variables can also be referred using ${PROPERTY_NAME} inside the logback configuration.
I just started using log4j2. I am using it for multiple JVMs that use RMI. I have two problems.
I start up the rmiregistry as it's own executable using the same CLASSPATH as the application. It starts without any errors, but once an RMI client talks to it, rmiregistry outputs this error: ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath... But the log4j-core jar file is already in the CLASSPATH and if there was a problem, then why does the application not output this error as well?
I am using the RollingFile appender with OnStartupTriggeringPolicy and SizeBasedTriggeringPolicy. In theory at startup I should get logs like:
foo.log - active log
foo.log.1 - old log rolled if I already started it before
foo.log.2 - older log rolled if I already started it more than once
But instead when I start it the first time, it rolls a bunch of times, even though the size limit has not been reached and the active log is one that should have been rolled. In other words, I get this:
foo.log
foo.log.1
foo.log.2
foo.log.3 - active log
foo.log.4
Config file (log4j2.xml) contents:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- ********** APPENDERS *********** -->
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d (%-4r) [%t] %-5p %c %x - %m%n"/>
</Console>
<RollingFile
name="FOO_OUT"
fileName="/foo/log/foo.log"
filePattern="/foo/log/foo.log.%i"
append="true">
<PatternLayout>
<Pattern>%d (%-4r) [%t] %-5p %c %x - %m%n</Pattern>
</PatternLayout>
<DefaultRolloverStrategy max="24" fileIndex="min"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<asyncLogger name="au.com" level="DEBUG">
<AppenderRef ref="FOO_OUT"/>
</asyncLogger>
<asyncRoot level="debug">
<AppenderRef ref="FOO_OUT"/>
</asyncRoot>
</Loggers>
</Configuration>
The CLASSPATH includes a lot of jars, but main ones related to lo4j2 are:
/foo/lib/slf4j/slf4j-api-1.7.21.jar
/foo/lib/slf4j/jcl-over-slf4j-1.7.21.jar
/foo/lib/slf4j/log4j-slf4j-impl-2.5.jar
/foo/lib/log4j2/log4j-core-2.5.jar
/foo/lib/log4j2/log4j-api-2.5.jar
/foo/lib/log4j2/disruptor-3.3.4.jar
I am using log4j 2. I am trying to determine why my log file is not being written to, but my console is. This is the output I have to the console:
2016-04-25 12:26:07,142 INFO [main] helperCode.LogPlus (LogPlus.java:50) -
----------------------------------------------------------------------------
------------ The test is starting now at 2016-04-25-12-26-07-135 -----------
----------------------------------------------------------------------------
2016-04-25 12:26:07,151 INFO [main] helperCode.LogPlus (LogPlus.java:50) -
--------------------------------------------------------------------------
------------ METHOD loginBadPasswordGoodUsername_3 starting: ------------
--------------------------------------------------------------------------
The issues I've considered already:
The output is not repeating itself, so the log output being redirected is not an issue.
I am NOT using java.util.logging.Logger anywhere in my project;
The output of the log file is formatted in log4j style rather than JUL style, so I know that JUL is not overriding it;
Here is my XML config file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FileLogger" fileName="${sys:logFilePath}" append="false">
<PatternLayout pattern="%d %t %-5p %c{2} - %m%n" />
</File>
<Async name="Async">
<AppenderRef ref="FileLogger" />
</Async>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="automationFramework" level="trace">
<AppenderRef ref="FileLogger" />
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
I am directing the output to the file located at the system variable "logFilePath".
I am not sure what is going on, and the config files shown on answers for similar questions are not in XML format, so I am not sure how to commute the config file code to XML.
In your case as mentioned in the comment you do not have the package called applicatiionFramework. So change the name attribute by the package name of the classes where you want to record log events.
<Logger name="your package" level="trace">
<AppenderRef ref="FileLogger" />
<Root level="trace">
<AppenderRef ref="STDOUT" />
</Root>
</Logger>
For more information visit log4j-manual-configuration
Glad the other answer helped. Do you need the named logger though? Why not just have the root logger? Also, you declared an Async Appender but are not using it. Is that on purpose? This is what I would suggest:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FileLogger" fileName="${sys:logFilePath}" append="false">
<PatternLayout pattern="%d %t %-5p %c{2} - %m%n" />
</File>
<Async name="Async">
<AppenderRef ref="FileLogger" />
</Async>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="STDOUT" level="info" />
<AppenderRef ref="Async" />
</Root>
</Loggers>
</Configuration>
Note that you can also specify a level on the AppenderRef. I made the Console Appender level INFO to illustrate this point.
If you want the log to show location information and use Async loggers or appenders then you need to specify includeLocation="true" on the Async Appender.
For the complete noob, like myself, this line in Unknown's answer above:
<Logger name="your package" level="trace">
is the key. When it says "your package" it really means the "package foo.bar" you declared at the top of the .java file where you created the Logger, not the class name you passed to create the Logger, like:
LOGGER = LogManager.getLogger(MyClass.class.getName());
It took me a ridiculous amount of time today to get my log4j2 output going to a file, playing with various "name" values until the light bulb went on.