How to give separate log file only jar file package logs - java

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>

Related

No logs created unless it tests

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.

How to use log4j2 file appender feature in unix environment

I am new to unix. I have below log4j2 config file. When I use this file in unix environment, the file app.log is not created. Could any one tell me how to redirect the logs to file in unix environment
<?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} %method - %msg%n" />
</Console>
<File name="file" fileName="C:\Users\ppatrana\Desktop\app.log">
<!-- <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} %method - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
</Root>
<Root level="trace">
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
You have to change the filename :
<File name="file" fileName="/tmp/app.log">
More about linux filesystem : http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
I think your real problem is that you have two root loggers. Try the following instead:
<Loggers>
<Root level="trace">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="file" />
</Root>
</Loggers>
You also need to fix the filename as mentioned by #ToYonos.

log4j2 configuration file is confusing

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.

Log4j2 regular expressions

I am working with the log4j2 configuration file settings to try and make the config file use regular expressions so the it can use multiple classes in the logger property and name attribute. This is what I have tried so far that doesn't appear to work.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="../log/error.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="../log/event.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="foo.test" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="foo.*" 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>
Has anybody ever been able to successfully use regular expressions or even globs in the log4j2 config file?
I figured out I do not need to put regular expressions at all in the package name. For example if in my foo package i had foo.bar, foo.foo, and foo.test. I would put foo in the Logger property name attribute to log the ERROR and INFO level logs from any any of 3 packages that begins with foo as such.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="../log/error.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="../log/event.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="foo.test" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="foo" 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>

Disable custom logger print in console (Log4j2)

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>

Categories

Resources