log4j2 not logging to a file - log4j2

Java 1.8, WebSphere Liberty 19.0.0.3 running in localhost, log4j v.2.17.1, Maven v.3.5.2
I have read some posts of similar issues, but I have not seen a solution that works for my case.
I cannot get anything to write to the log files. Presently, I am focusing on the root logger, as that writes to both the console and to file.
Pom file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.17.1</version>
</dependency>
Here is the configuration for the root logger:
<Root>
<!-- change level to EROR -->
<Level value="TRACE"/>
<AppenderRef ref="APS-FILE"/>
<AppenderRef ref="STDOUT"/>
</Root>
The root logger is correctly calling the STDOUT appender and writing to the console. Note the pair of asterisks. Those match the output I am seeing in the console.
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>"** %d{DATE} %5p %c{1}:%L - %m%n **"</Pattern>
</PatternLayout>
</Console>
console output:
"** 07 Jan 2022 09:50:42,331 INFO WSWebSsoFilter:44 - Exiting WSWebSsoFilter.doFilter **"
"** 07 Jan 2022 09:50:42,331 INFO WSWebSsoFilter:44 - Exiting WSWebSsoFilter.doFilter **"
This is the appender configuration for root logger to write to file.
<RollingFile name="APS-FILE" fileName="/logs/aps/${company-code}/aps-A.log"
filePattern="logs/aps/${company-code}/aps-1.log">
<PatternLayout>
<Pattern>"%d{DATE} %5p %c{1}:%L - %m%n"</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy minSize="0"/>
<!--SizeBasedTriggeringPolicy size="10 MB"/-->
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
The OnStartupTriggeringPolicy is firing, as you can see from this file:
LastWriteTime Length Name
------------------ ------ ---------
1/7/2022 9:49 AM 0 aps-A.log
Any ideas about how to fix this? Thanks.

Try to change the STDOUT target to your rolling file appender
<Console name="STDOUT" target="**APS-FILE**">
<PatternLayout>
<Pattern>"** %d{DATE} %5p %c{1}:%L - %m%n **"</Pattern>
</PatternLayout>
</Console>

Related

DefaultRolloverStrategy In log4j2

I am having trouble configuring the "DefaultRolloverStrategy" for log4j2.xml to do the following :-
Ensure ONLY the last 4 log files are kept and older ones should get deleted.
So just be clear, the last 4 log files could be over a number days or on the same day, therefore,
the last 4 log files could be with the same date or span over different dates.
Below is the contents of log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Appenders>
<!-- Console Appender -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- Rolling File Appender -->
<RollingFile name="File" fileName="app_log.log"
filePattern="app_log-%d{yyyy-MM-dd}.%i.log">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="2 KB" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="" maxDepth="1">
<IfFileName glob="app_log*.txt">
<IfAny>
<IfAccumulatedFileSize exceeds="5 KB" />
<IfAccumulatedFileCount exceeds="4" />
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.app.utilities" level="info" additivity="true">
<AppenderRef ref="File" />
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I run up my application as shown below
java -Dlog4j.configurationFile=./app-log4j2.xml -jar application.jar
The log is generated in the same directory from where the above command is invoked from.
Below is a sample history of log files :-
File Name Date Modified
app_log.log 8/27/2018 2:25 PM
app_log-2018-08-27.2.log 8/27/2018 2:25 PM
app_log-2018-08-27.1.log 8/27/2018 2:11 PM
app_log-2018-08-26.5.log 8/26/2018 2:01 PM
app_log-2018-08-26.4.log 8/26/2018 2:00 PM
app_log-2018-08-26.3.log 8/26/2018 1:58 PM
app_log-2018-08-26.2.log 8/26/2018 1:57 PM
app_log-2018-08-26.1.log 8/26/2018 1:56 PM
It seems the "DefaultRolloverStrategy" is having no effect.
I presume my configuration is wrong. However, I would very much appreciate for suggestions
to correct this please.
Also, if the requirenment was to change such that log files greater 20 days should be deleted.
How could that be acheived.
Thank you very much in advance for you help
Pete
Take a look at the following line:
<IfFileName glob="app_log*.txt">
But your log files don't end with .txt! See:
File Name Date Modified
app_log.log 8/27/2018 2:25 PM
You likely need to change it to:
<IfFileName glob="app_log*.log">
That's what really jumps out to me. There might be another few tweaks you have to make, but try that first.

log4j2 Logs not writing to file when running as jar

I have configured log4j2.xml within eclipse and all logs write correctly to a file.
When I export maven project as a jar and run from command promt the logs are displayed on the console instead of writing to a file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="file_all" fileName="C:/log/logsALL.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="file_all"/>
</Root>
<Logger name="com.api.main" level="INFO">
<AppenderRef ref="file_all"/>
</Logger>
</Loggers>
</Configuration>
When log4j starts up, it shows internal logging on the console (because status=trace in the configuration).
This internal log should show the location of the config file that is being used. Double-check that this is the correct location: I suspect that an old config file is being loaded that logs to the console...

ERROR StatusLogger No logging configuration

I am using log4j2
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
and a very basic configuration xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<category name="org.apache.log4j.xml">
<priority value="info" />
</category>
<Root>
<priority value ="debug" />
<appender-ref ref="STDOUT" />
</Root>
</log4j:configuration>
For some reason logging is not working. I am getting
ERROR StatusLogger Error parsing /home/sfalk/workspace/java/lazy-model-access/lamoa-parent/lamoa-server/target/classes/log4j2.xml
ERROR StatusLogger No logging configuration
What am I doing wrong here?
Your configuration file is for log4j 1.x, not 2.5

Log4j 2 not logging to file

I am trying to use log4j 2 for the first time but having trouble getting my code to log to a file.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
My code is not logging to my File Appender or Console Appender:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<Pattern pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</PatternLayout>
</Console>
<File name="File" fileName="resources/app-log.log">
<PatternLayout>
<Pattern pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="x.y" level="ALL" additivity="true">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>
Here is my Java code doping the logging:
package x.y;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Foo {
private static Logger logger = LogManager.getLogger(Foo.class.getName());
public static void main(String[] args) {
Foo foo = new Foo;
logger.info("Application starting");
}
}
Edit: My app is logging to the console but not using the pattern layout I specified. My app is a Maven app and I have put log4j2.xml in src/main/resources. I know my configuration xml is being read because when I put in something invalid I get an exception.
Figured it out. Changed to:
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
Now it logging to my file.

InvocationTargetException with RollingFileAppender in Log4J2

I'm trying to use a RollingFileAppender in Log4J2 and apparently I'm missing something. The file is created with 0 bytes, but no logging goes to it. When I turn on the trace I see the following error message logged (to the console) by log4J2:
2014-07-08 19:51:11,354 DEBUG Starting RollingFileManager c:/logs/blah.log
2014-07-08 19:51:11,464 DEBUG Generated plugins in 0.108079890 seconds
2014-07-08 19:51:11,465 ERROR Unable to invoke method createAppender in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:913)
This is my config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="TRACE" verbose="false">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="Blah" fileName="c:/logs/blah.log" filePattern="c:/logs/blah-%i.log">
<PatternLayout pattern="%d %p %c: %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</appenders>
<loggers>
<root level="INFO">
<appender-ref ref="Blah" level="INFO"/>
</root>
</loggers>
</configuration>
It's probably something basic but I can't find it. Can anyone help?
Could you create a ticket on the Log4j2 issue tracker so the team can take a look at it? It may be a bug in version rc2. (I'm assuming you are using rc2 and have both the api and core jars in the classpath.)
Thanks!
I took a look and the class at the beginning of the stack trace (org.apache.logging.log4j.core.config.BaseConfiguration) no longer exists in log4j-core-2.0-rc2. It was renamed to AbstractConfiguration.
So I think this error is caused by an old log4j-core-2.0-rc1 jar that is still floating around somewhere.

Resources