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>
Related
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 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
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">.
I'd like to log all incoming and outgoing CXF requests to a specific logfile. But all I get with the following configuration is a console output. What is wrong here?
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<RollingFile name="CXF" fileName="cxf.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<logger name="org.apache.cxf" additivity="false" level="info">
<AppenderRef ref="CXF"/>
</logger>
<logger name="org.apache.cxf.interceptor.LoggingInInterceptor" additivity="false" level="info">
<AppenderRef ref="CXF" />
</logger>
<logger name="org.apache.cxf.interceptor.LoggingOutInterceptor" additivity="false" level="info">
<AppenderRef ref="CXF" />
</logger>
<Root level="all">
<AppenderRef ref="CONSOLE" />
</Root>
</Loggers>
</Configuration>
src/main/resources/META-INF/cxf/org.apache.cxf.Logger:
org.apache.cxf.common.logging.Log4jLogger
As a workaround, I'm now using org.apache.cxf.common.logging.Slf4jLogger and the bridge dependency.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<property name="main">logs/main.log</property>
<property name="webservice">logs/webservice.log</property>
<property name="sql">logs/sql.log</property>
<property name="mongoexceptions">logs/mongoexceptions.log</property>
</Properties>
<Appenders>
<RollingFile name="mongoexceptions-all" fileName="${mongoexceptions}"
filePattern="${mongoexceptions}.%i" bufferedIO="false" bufferSize="8192">
<PatternLayout>
<pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m ---- %c%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000KB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="main-all" fileName="${main}"
filePattern="${main}.%i" bufferedIO="false" bufferSize="8192">
<PatternLayout>
<pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m ---- %c%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000KB" />
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="webservice-all" fileName="${webservice}" filePattern="${webservice}.%i" bufferedIO="false" bufferSize="8192">
<PatternLayout>
<pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m %n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10000KB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
<RollingFile name="sql-all" fileName="${sql}" filePattern="${sql}.%i" bufferedIO="true" bufferSize="8192">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="1000KB" />
</Policies>
<DefaultRolloverStrategy max="1"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="main-all"/>
</Root>
<Logger name="org.apache.cxf" additivity="false" level="warn">
<appender-ref ref="main-all"/>
</Logger>
<Logger name="org.springframework" additivity="false" level="warn">
<appender-ref ref="main-all"/>
</Logger>
<Logger name="org.eclipse.jetty" additivity="false" level="warn">
<appender-ref ref="main-all"/>
</Logger>
<Logger name="com.main.bam.db.edp.EventAuditDAO" level="error" additivity="false">
<appender-ref ref="mongoexceptions-all" />
</Logger>
<Logger name="com.main.bam.db.edp.ExceptionsLogDAO" level="error" additivity="false">
<appender-ref ref="mongoexceptions-all" />
</Logger>
<Logger name="com.main.bam.db.edp.ClaimDAO" level="debug">
<appender-ref ref="webservice-all" />
</Logger>
<Logger name="com.main.bam.db.edp.CPDSummaryDAO" level="debug">
<appender-ref ref="webservice-all" />
</Logger>
<Logger name="DBConnectionFactory" level="info">
<appender-ref ref="sql-all"/>
</Logger>
</Loggers>
</Configuration>
Here is the working example.
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>