I previously had this working where it would log to a file on my C: drive however trying to get this working into a logs folder using the CATALINA_HOME environment variable doesn't seem to work.
CATALINA_HOME environment variable is set to C:\apache-tomcat\bin and I was hoping to store the logs in C:\apache-tomcat\logs
Have I done something silly with my configuration file?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_DIR">${sys:CATALINA_HOME}../logs</Property>
<Property name="ARCHIVE">${LOG_DIR}/archive</Property>
<Property name="PATTERN">%d{dd/MMM/yyyy HH:mm:ss.SSS} [%t] %X{id} %X{username} %-5level %c{36} %l: %msg%n</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}" />
</Console>
<RollingFile name="FileAppender" fileName="${LOG_DIR}/application.log"
filePattern="${ARCHIVE}/application.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="${PATTERN}" />
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="FileAppender" />
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="uk.co" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
</Loggers>
<Logger name="org.hibernate" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
<Logger name="org.apache" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
</Configuration>
Resolved by changing ${sys:CATALINA_HOME} to ${env:CATALINA_HOME}
Also added a missing / thank to RC
Related
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>
My log4j2.xml looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%sn] [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RandomAccessFile name="randomAccessFile" fileName="temp\\logs.log" immediateFlush="false" append="false">
<!-- <PatternLayout pattern="%d %p %class{2.} [%t] %location %m %ex%n" /> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</RandomAccessFile>
<JDBC name="databaseAppender" tableName="app_logs" bufferSize="1000">
<!-- <DataSource jndiName="java:/comp/env/jdbc/testLog" /> -->
<!-- <BurstFilter level="TRACE" rate="20" maxBurst="2" /> -->
<ConnectionFactory class="com.mc.loggerProvider.ConnectionFactory" method="getDatabaseConnection" />
<Column name="LOG_ID" pattern="%u{TIME}" isUnicode="false" />
<Column name="LOG_TIMESTAMP" isEventTimestamp="true" isUnicode="false" />
<!-- <Column name="CORR_ID" pattern="%X{CORR_ID}" isUnicode="false" /> -->
<Column name="LOG_LEVEL" pattern="%level" isUnicode="false" />
<Column name="LOGGER_NAME" pattern="%logger" isUnicode="false" />
<Column name="MESSAGE" pattern="%msg %ex{full}" isUnicode="false" />
<!-- <Column name="EXCEPTION" pattern="%ex{full}" isUnicode="false" /> -->
</JDBC>
</Appenders>
<Loggers>
<Logger name="console" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="console" />
</Logger>
<AsyncLogger name="asyncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="randomAccessFile" />
</AsyncLogger>
<Logger name="syncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="randomAccessFile" />
</Logger>
<AsyncLogger name="asyncDB" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
</AsyncLogger>
<Logger name="syncDB" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
</Logger>
<AsyncLogger name="asyncDBasyncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
<AppenderRef ref="randomAccessFile"></AppenderRef>
</AsyncLogger>
<Logger name="syncDBsyncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
<AppenderRef ref="randomAccessFile"></AppenderRef>
</Logger>
<Root level="info" includeLocation="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
As there are couple of loggers I have defined already for sync/async file/DB logging purpose. The problem which I am facing here when I want to use any of the loggers I cant define a name for those.
LogManager.getLogger("asyncDB") -- will give me logger with name asyncDB
However, the convention says to create loggers with class name as it will be easier to debug if you write %logger in logging output.
LogManager.getLogger(MyClass.class)
My requirement asks me to create a logger with class name however that should be asynchronous or synchronous based on users requirement.
I have created a small class name LogProvider having method registerFor which is again having arguments of type AppendersType. Below is the code.
public class LogProvider {
public static Logger registerFor(AppendersType type) {
switch (type) {
case DATABASE_ASYNC:
return LogManager.getLogger("asyncDB");
case DATABASE_SYNC:
return LogManager.getLogger("syncDB");
case FILE_ASYNC:
return LogManager.getLogger("asyncFile");
case FILE_SYNC:
return LogManager.getLogger("syncFile");
case FILE_DATABASE_SYNC:
return LogManager.getLogger("syncDBsyncFile");
case FILE_DATABASE_ASYNC:
return LogManager.getLogger("asyncDBasyncFile");
default:
break;
}
return null;
}
I want to make use of asyncLoggers but having a name of class and not the name which is defined in XML file.
I am using the following log4j2 configuration to restrict the logging from one of the ApacheDS classes to ERROR and lesser.
<logger level="error" name="org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex">
<appender-ref ref="ARTFile" />
</logger>
The messages from this particular class are still getting logged at DEBUG level. A similar setting for the following package worked.
org.apache.directory.server.core
Please help.
Here is the complete configuration.
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="defaultConfiguration" strict="true" monitorInterval="5">
<properties>
<property name="patternlayout">%-5p %d{ISO8601} [%t][%4C][%L] - %m%n</property>
<property name="filename">C:\\Releases\\RCL\\RLKSAdmin\\work\\rcladmin\\logs\\RLKS_ART.log</property>
<property name="filenamePattern">C:\\Releases\\RCL\\RLKSAdmin\\work\\rcladmin\\logs\\RLKS_ART_%i.log</property>
</properties>
<appenders>
<RollingFile name="ARTFile" fileName="${filename}" filePattern="${filenamePattern}" append="true">
<PatternLayout pattern="${patternlayout}" charset="UTF-8" />
<Policies>
<SizeBasedTriggeringPolicy size="3 MB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="ARTFile" />
</root>
<logger level="error" name="org.springframework.core">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.beans">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.context">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.web">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.security">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.ldap">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.jndi">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.validation">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.ui">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.util">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.schema">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex"/>
<logger level="error" name="org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmTable"/>
<logger level="error" name="org.apache.directory.server.core">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.core.partition">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.protocol">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.shared">
<appender-ref ref="ARTFile" />
</logger>
</loggers>
</configuration>
If your goal is to restrict the log level it may be better not to specify the appender-ref: that way the restriction applies to the root logger and all associated appenders. In the configuration snippet you showed, the restriction only applies to the "ARTFile" appender.
I am facing below error on upgradation of log4j 1.x to log4j2.
As per documentation on log4j2 site I have prepared below log4j2 file, but still I am getting this exception.
Log4j2 version: 2.6.1, Weblogic: 10.3.6, jdk 1.7.x
Please help with this exception:
2016-06-25 05:49:36,002 [ACTIVE] ExecuteThread: '70' for queue: 'weblogic.kernel.Default (self-tuning)' ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.config.LoggerConfig for element Logger. org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element Logger are invalid
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:270)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:131)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
Log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" >
<Properties>
<Property name="theHostName">${hostName}</Property>
</Properties>
<!-- bufferedIO=true by default, bufferSize=8192bytes
-->
<Appenders>
<RollingFile name="FILE" filename="${sys:weblogic.Name}.log" filepattern="${sys:weblogic.Name}.log.%i" append="true" >
<PatternLayout pattern="[%-5p][%d{yyyy-MM-dd HH:mm:ss,SSS}][${sys:weblogic.Name}:${hostName}][%t][%X{MessageInfo}][%c{1}:%M:%L][%msg]%n" />
<Policies>
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
<DefaultRolloverStrategy max="100" fileIndex="min"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger level="DEBUG" includeLocation="true">
<AppenderRef ref="FILE"/>
</Logger>
<Root level="INFO" includeLocation="true">
<AppenderRef ref="FILE"/>
</Root>
<!-- Package specific log level defines -->
<logger level="WARN" name="org.springframework" />
<logger level="WARN" name="org.jboss" />
<logger level="OFF" name="org.hibernate" />
<logger level="WARN" name="com.company.project.eligibility" />
</Loggers>
</Configuration>
Only the root logger can have no name, so this Logger needs to have a name:
<Logger level="DEBUG" includeLocation="true">
<AppenderRef ref="FILE"/>
</Logger>
I guess package specific levels should be commented out or declared like this:
<Logger level="WARN" name="org.springframework" />
<Logger level="WARN" name="org.jboss" />
<Logger level="OFF" name="org.hibernate" />
<Logger level="WARN" name="com.company.project.eligibility" />
I have two appenders. One is attached to the console and one is to a log file. But that's not really that important. The issue I have is I want everything to go to the file at INFO level. The console is different however. There a bunch of loggers I don't want touching the console at WARN or INFO level because they spray a lot more information than the user needs.
So lets say I have three loggers A, B, C. A, B, and C should all go to the file appenders at INFO level. A should go to the console for INFO, B should go to the console for WARN and C should go to the console for ERROR. What does a log4j config to do this look like?
Something like this should work:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="A" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="info" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Logger name="B" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="warn" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Logger name="C" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="error" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT" level="trace" />
<AppenderRef ref="RollingFile" level="trace"/>
</Root>
</Loggers>