I have the following log.properties file in my Java project:
handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$s] %5$s %n
I am declaring the Logger as follows in one file which generates most of the log:
public static final Logger logger = LogManager.getLogger();
The log4j2.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval ="5">
<Properties>
<Property name="log-path">${LOGDIR}</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile"
fileName="workerApplication.log"
filePattern="workerApplication-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<!-- <DefaultRolloverStrategy max="4" /> -->
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="warn" additivity="false">
<appender-ref ref="RollingFile" />
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
In my workerApplication.log, I am still getting the debug statements too that are in my code. I only want INFO and above level. I have changed in both places the level of the log but it is not working. Can anyone point out what am I doing wrong? The project is being deployed as an RPM in Linux
You set the loglevel for each logger in log4j2.xml. Since you only have one logger you can change the loglevel for that logger to only get logging for level info and higher.
Please change
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
to
<Root level="info" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
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>
Root logger is configured to be a synchronous logger at INFO level. I also have a couple of Async loggers. One of the Async logger is configured at a customized higher log level TOAST (789). This is higher than the TRACE level (600).
The reason for doing this is to have a log level that can be used for special events to be logged via Async logger and their own appenders.
We dont want the TOAST events to be going to the Synchronous Root loggers or other loggers.. but only to the Async TOAST logger and its corresponding appenders .. is it possible?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="120" packages=“com.pack.toast”>
<CustomLevels>
<CustomLevel name=“TOAST” intLevel=“789” />
</CustomLevels>
<Appenders>
<RollingFile fileName="${sys:catalina.base}/logs/packiu.log"
append="true" name="DRFILE"
filePattern="${sys:catalina.base}/logs/packiu-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d [%t] %c %M - %p: %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="30" />
</RollingFile>
<RollingFile name="RollingFile" fileName="/Users/toasty/logs/roll_file_app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz" immediateFlush="true" >
<PatternLayout>
<pattern>%m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Rewrite name="Rewrite" ignoreExceptions = "true" >
<ToastAppenderPolicy toastNeeded="true">
</ToastAppenderPolicy>
<AppenderRef ref="RollingFile"/>
</Rewrite>
</Appenders>
<Loggers>
<Logger name="net.rubyeye" level="off" />
<Logger name="com.google.code" level="off" />
<AsyncLogger name="com.pack.toast.ToastLoggerImpl" level=“TOAST” includeLocation="true">
<AppenderRef ref="Rewrite" />
</AsyncLogger>
<Root level="info">
<AppenderRef ref="DRFILE" />
</Root>
</Loggers>
</Configuration>
It sounds to me like you really want a named logger not a custom level because you only ever want your messages to go to one logger and its appenders. Perhaps you should use something more like:
<AsyncLogger name="TOAST_LOGGER" level=“INFO” includeLocation="true">
<AppenderRef ref="Rewrite" />
</AsyncLogger>
and then in your code
private static final Logger TOAST_LOGGER = LogManager.getLogger("TOAST_LOGGER");
...
TOAST_LOGGER.info("Toast message");
...
Otherwise, if you must use a custom log level then you can accomplish what you want by filtering with a Threshold Filter.
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 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>
I have a problem applying the log4j2.xml auto configuration properly, and I think it has something to do with my folder arrangement.
I'm using maven to add log4j2 libs and arrange my projects as follows:
- one project to contain all "common" classes, used by server and client side of my system.
- another "core" project - the server side application.
Both projects use the same general package hierarchy (like com.foo.specific.package)
In the Common project I define a logger wrapper:
public class LogWrapper
{
static Logger systemParentLogger = LogManager.getLogger("com.foo");
public static Logger getLogger(Class<?> cls)
{
return LogManager.getLogger(cls.getName());
}
}
Moreover, the Common project contains the log4j2.xml file under META-INF (alongside the persistence.xml file for Hibernate usage).
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">
<appenders>
<appender type="RollingFile"
name="MyFileAppender"
fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n"/>
</appender>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="MyFileAppender"/>
</root>
<logger name="com.foo" level="info" additivity="false">
<appender-ref ref="MyFileAppender"/>
</logger>
<logger name="org.hibernate" level="error">
<appender-ref ref=MyFileAppender"/>
</logger>
</loggers>
</configuration>
While running a sample code in the Core project (using the LogWrapper I wrote and some JPA voodoo), I could still see INFO hibernate logs, and no log file was created. I should state that while debugging the code, I could see that the logger fetched was given some weird value "com.foo.core.persistence.PersistenceXMLTest:ERROR in sun.misc.Launcher$AppClassLoader#2f600492"
The log4j2.xml was placed in a "Folder" which in eclipse terms is "not on classpath".
Changing META-INF to be a "source folder" solved the problem.
In addition, the log4j2.xml file was not defined properly.
These are the modifications needed:
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">
<appenders>
<RollingFile name="MyFileAppender"
fileName="../Logs/app.log"
filePattern="../Logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level="error">
<appender-ref ref="MyFileAppender"/>
</root>
<logger name="com.foo" level="info" additivity="false">
<appender-ref ref="MyFileAppender"/>
</logger>
</loggers>
</configuration>
Still couldn't make the org.hibernate logger to be redirected to my logs, but at least I got the logger to work