Log4j2 not logging to file - java

I'm using Log4j2 in my application and it seems to load and initialize fine.
Console gets logged to, the 2 specified file appenders create their respective files, but those files stay empty!
Is my config faulty? Did I start Log4j2 the wrong way?
Here's the config:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
<Appenders>
<File name="file" fileName="${sys:logdir}ftm.log">
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="file_error" fileName="${sys:logdir}ftm_error.log">
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="file" level="ALL"/>
<AppenderRef ref="STDOUT" level="ALL"/>
</Root>
<Logger name="FTM" level="ALL" additivity="true">
<AppenderRef ref="file_error" level="ERROR"/>
</Logger>
</Loggers>
</Configuration>
And here's the code:
LoggerContext contextLogging = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("log4j2.xml");
if (file != null && file.isFile() && file.canRead()){
contextLogging.setConfigLocation(file.toURI());
}
I start logging at various points throughout the application after this initialization bit.

I suspect your call to obtain the LoggerContext is causing Log4j to initialize. You should see a message saying the default configuration is being used. Why not just use one of the Configurator.initialize() methods?

Maybe LOG4J2 is buffering your log messages and will write them later.
Have you tried shutting down your app server?
This will force any buffered messages to be written to the file.

Related

setting up log4j2 in new web app

I'm upgrading my web app to include log4j2 and having difficulty configuring. I have my log4j2.xml file located in WEB-INF/classes folder. I want log4j2 to write to my log file named jed.log in WEB-INF/logs/ folder. My xml file looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="/WEB-INF/logs/jed.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
Log4j2 is definitely logging to the console so I know that part is working, but I can't seem to make it see the log file. For the fileName attribute I've tried setting it to "/WEB-INF/logs/jed.log", "WEB-INF/logs/jed.log", "../logs/jed.log". Nothing seems to work.

Can you redirect stdout, stderr to a fileappender in log4j2 v2.3?

This is my log4j2.xml configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="Log4JFile" fileName="/log/Log4JFile.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
<AppenderRef ref="Log4JFile"/>
</Root>
</Loggers>
</Configuration>
I'm using Jboss and if you do not include a configuration file, log4j logs everything to server.log. I need to log all of the levels of log4j plus stdout and stderr to a separate file that is not the default one. How can I accomplish this without overriding the print method in Java?
You cannot redirect from the Log4j ConsoleAppender to a Log4j FileAppender.
What you can do is in the scripts/commands that start your application, redirect the output of that command to a file. The syntax for this is OS dependent but usually looks something like command > file. Multiple > characters can append to an existing file rather than overwrite it. (Also OS dependent.)

Logging file is not created

I use log4j2 in my eclipse plugin for logging. I created the following log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<File name="File" fileName="MyLog.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="File" immediateFlush="true"/>
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I can see the logs in the console, but the log file seems not even to be created. In the mean time, I'm quite desperated. What am I doing wrong?
Nothing wrong with the configuration file. If you are using eclipse to test log file will get generated in root folder of the project. You may want to use fileName="target/MyLog.log" or absolute file path to to move file to anticipated folder.

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.

Defining file location in log4j2.xml after deploying WAR

I have gotten my print to file to work for log4j2 logging, but when deploying my project as a WAR onto a tomcat server, I cant seem to find where the log file is sent. I have the following log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="AE" fileName="/AE.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</File>
<File name="BP" fileName="/BP.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</File>
<File name="CBK" fileName="/CBK.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" level="debug" />
<AppenderRef ref="AE" level="debug"/>
<AppenderRef ref="BP" level="debug"/>
<AppenderRef ref="CBK" level="debug"/>
</Root>
</Loggers>
</Configuration>
Is there a way that I can properly define my filename paths so that after having deployed my WAR file, my logs are sent to somewhere such as WEB-INF?
About the disappearing log files, could it be that the user that is running the tomcat server does not have permission to write files to the root directory?
About your filename path question, it is possible to use system properties in the path by using the ${sys:some.property} syntax.

Categories

Resources