Can we dsable log print for some customm creating Logger?
I have two loggers:
Root logger
Metrics Logger (which prints into file)
<Configuration status="WARN">
<Appenders>
<File name="file" fileName="app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<logger name="metrics" level="info" additivity="true">
<AppenderRef ref="file"/>
</logger>
<root level="info">
<AppenderRef ref="STDOUT"/>
</root>
</Loggers>
</Configuration>
I want to disable print messages in console for File logger
The key to understanding is here
http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity
The root logger is a parent of the logger "metrics". The log event of "metrics" is then passed to root logger's appender ("STDOUT"). This is known as additivity. It is possible to disable it by setting the additivity attribute on the logger "metrics" to false.
<logger name="metrics" level="info" additivity="false">
<AppenderRef ref="file"/>
</logger>
Related
I have this xml config file for log4j2 but the loggers don't work. If I set the referince for root logger as "Errors" of "Info" (my new appenders) when I run the program the logger works and writes the data to logger files but if I set thouse appenders to my new loggers ("Information" and "ErrorLog") the logger doesn't write any data to my log files.
Here is my configuration xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="Info" fileName="logs/info.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="Errors" fileName="logs/errors.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="Information" level="debug">
<AppenderRef ref="Info"/>
</Logger>
<Logger name="ErrorLog" level="error">
<AppenderRef ref="Errors"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Solved!
I modified the Loggers section and I deleted those 2 Loggers.
<Loggers>
<Root level="debug">
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="Info" />
<appender-ref ref="Errors" />
</Root>
</Loggers>
I'm using log4j2 for logging in my app. Basically it's logging uncatched exceptions (Error.log) and logging changes of my data at service layer (journal.log; journalJSON.log).
And here's the thing, when I'm starting service layer tests every log appears in the file and console, but when I'm using application deployed in tomcat they're only in console. What's happening?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="Error" fileName="logs/error.log"
immediateFlush="true" append="true">
<PatternLayout pattern=" \n\n %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<RandomAccessFile name="Journal" fileName="logs/journal.log" immediateFlush="true" append="true">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %msg%n"/>
</RandomAccessFile>
<File name="JournalJSON" fileName="logs/journalJSON.log" immediateFlush="true" append="false">
<JSONLayout complete="true" charset="UTF-8" compact="false" eventEol="false"/>
</File>
</appenders>
<Loggers>
<Logger name="com.user.controller" level="error" additivity="false">
<AppenderRef ref="Error" level="error"/>
<AppenderRef ref="Console" level="error"/>
</Logger>
<Logger name="com.user.service" level="info" additivity="false">
<appender-ref ref="JournalJSON" level="info"/>
<appender-ref ref="Journal" level="info"/>
<appender-ref ref="Console" level="info"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
Well, there are a couple of possibilities I can think of:
Your configuration file isn't being found and it is using the default configuration.
The logger for your application isn't com.user.controller or com.user.service and the root logger is being used.
get a logger instance named "com.foo"
Logger logger = Logger.getLogger("com.foo");
Now set its level. Normally you do not need to set the level of a logger programmatically. This is usually done in configuration files.
logger.setLevel(Level.INFO);
this is an example of log4j2 configuration
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Appender type="File" name="File" path="location/to/logging/space/file.txt">
<Layout type="PatternLayout" pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Appender>
</Appenders>
<Loggers>
<Logger name="com.foo" level="trace" additivity="false">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
I am trying out log4j2 configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="info">
<appenders>
<Console name="console-log" target="SYSTEM_OUT" append="false">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</Console>
<File name="debug-log" fileName="${catalina.base}/logs/debug.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</File>
<File name="trace-log" fileName="${catalina.base}/logs/trace.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</File>
<File name="error-log" fileName="${catalina.base}/logs/error.log" append="false">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n"/>
</File>
</appenders>
<loggers>
<root level="debug" additivity="false">
<appender-ref ref="console-log"/>
</root>
<Logger name="com.mypackage" level="debug" additivity="false">
<appender-ref ref="debug-log" level="debug"/>
<appender-ref ref="trace-log" level="info"/>
<appender-ref ref="error-log" level="error"/>
</Logger>
</loggers>
</configuration>
I am getting logging and data to catalina.out but the rest of the files are not getting created.
Couldn't figure out the silly mistake I might be doing here?
and How do I create one more file which can have the appended logs of debug-log,trace-log,error-log only.
You have an error in your config:
Console appenders shouldn't have an append attribute. Removing that should fix your config.
Additionally the additivity attribute on the Root logger is unnecessary. It does nothing, as there is no higher logger to add to.
You should definitely have seen a line like the following in your output, warning you about the bad config:
ERROR Console contains an invalid element or attribute "append"
I have a log4j2 config file I was handed and have no clue what half of it means due to that it doesn't follow what is stated on their site about their configuration file layout. Can anybody decipher what the config file is saying?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="${env:ADSSSDIR}/log/error.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="${env:ADSSSDIR}/log/event.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="mikros.utils.MikrosLoggerTest" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="mikros.adsss.logger.AdsssLogger" level="debug"
additivity="false">
<AppenderRef ref="ERROR_FILE" level="ERROR"/>
<AppenderRef ref="EVENT_FILE" level="INFO"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Config file looks fine to me. Are you looking at the right web site?
User manual links:
Status Logger (showing internal Log4j2 WARN-level messages)
monitorInterval
Console Appender
Pattern Layout
File Appender
Environment Variable Lookup
Loggers
additivity
... That should get you started.