Use log4j2 for multiple classes - java

I found many link that helped me building the logger. Some here in SO and others in other pages.
This answer here: https://stackoverflow.com/questions/7624895/how-to-use-log4j-with-multiple-classes#= is the best that I have found.
It is already some years old thought and some things now are different.
My goal is to have one single logger shared among all of the java classes that print messages to the log, both console and file.
I am using LOG4J2: http://logging.apache.org/log4j/2.x/manual/configuration.html
main():
import org.apache.logging.log4j.Logger;
public class App {
private static final Logger LOGGER = Logger.getLogger("GLOBAL");
public static void main(){
...calling other classes
}
}
anyOtherClass:
import org.apache.logging.log4j.Logger;
public class secondClass {
private final Logger LOGGER = Logger.getLogger("GLOBAL");
public void writeLog(){
LOGGER.log(Level.INFO, "A simple string");
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">/var/log</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${basePath}/myApp.log" filePattern="${basePath}/myApp-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="java.util.logging.Logger" level="ALL" additivity="true">
<appender-ref ref="fileLogger" level="ALL" />
</Logger>
<Root level="ALL" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
I (more or less) know that I should use LogManager, truth is I should call it as:
private static final Logger logger = LogManager.getLogger(MyApp.class);
or, in the main(), probably like this:
private static Logger LOGGER = null;
#BeforeClass
public static void setLogger() {
System.setProperty("log4j.configurationFile","log4j.xml");
LOGGER = LogManager.getLogger();
}
Because, I believe, using LogManager I can point the Log to the xml file for its settings.
But when I build it and run it, the CLI rage quits at the first ever LOG, reporting: Exception in thread "main" java.lang.NullPointerException at myapp.modules.Database.<init>(Database.java:67)
Expected Result:
All I want to do is being able to have the log on all of my classes and that this is written to a file.
I just can't get this working. And when I do not use LogManager, I only see the logs on console but no file is created.
I'm using both Windows and Linux (that's why /var/log/ but I change it to C:\ too).
Other website I used:
https://howtodoinjava.com/log4j2/configure-log4j2-for-junit/
Log4J loggers for different classes <-- did not help me
..And many other search results are more than 6 years old.
POM.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>

You're wrong on several levels: you're mixing JDK's logger with Log4j2's Logger, you're not logging to several appenders, etc.
Here's what you should do:
/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>log4j2test</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
</project>
This is basically the only place where you have no error.
/src/main/java/so47656300/App.java
package so47656300;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class App {
private static final Logger logger = LogManager.getLogger(App.class);
public static void main(String[] args) {
logger.info("App.main - starting");
new SecondClass();
logger.info("App.main - ending");
}
}
Here, your error was to use java.util.logger.Logger instead of org.apache.logging.log4j.Logger.
/src/main/java/so47656300/SecondClass.java
package so47656300;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SecondClass {
private static final Logger logger = LogManager.getLogger(SecondClass.class);
public SecondClass() {
logger.info("SecondClass - starting");
logger.info("SecondClass - ending");
}
}
Here, your error was also to use the wrong Logger class. Use only classes from org.apache.logging.log4j.*.
/src/main/resources/log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="basePath">/var/log</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${basePath}/myApp.log" filePattern="${basePath}/myApp-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="fileLogger"/>
</Root>
</Loggers>
</Configuration>
Here, your errors are the following:
Wrong file name: Log4J 2 requires a name log4j2.xml. For your tests, create a file /src/test/resources/log4j2-test.xml.
You want only one logger, so use only one: the root one. You created several, but you don't need them.
Add appenders to the logger (the Root-one)
Output
Console
[INFO ] 2017-12-05 16:52:32.218 [main] App - App.main - starting
[INFO ] 2017-12-05 16:52:32.221 [main] SecondClass - SecondClass - starting
[INFO ] 2017-12-05 16:52:32.222 [main] SecondClass - SecondClass - ending
[INFO ] 2017-12-05 16:52:32.222 [main] App - App.main - ending
/var/log/myApp.log
[INFO ] 2017-12-05 16:52:32.218 [main] App - App.main - starting
[INFO ] 2017-12-05 16:52:32.221 [main] SecondClass - SecondClass - starting
[INFO ] 2017-12-05 16:52:32.222 [main] SecondClass - SecondClass - ending
[INFO ] 2017-12-05 16:52:32.222 [main] App - App.main - ending

Related

Log4j 2: How to enable ANSI colours on Windows console?

I try to use Windows 10 command line to print colored messages on console, but with no success. According to the Log4j 2 documentation, I should add the Jansi jar to my print application and set property log4j.skipJansi to false for enabling ANSI support on Windows. Could you, please, check and say what I did wrong:
Following code is my current work:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LoggerTest {
private static final Logger logger = LogManager.getLogger();
public static void main(String[] args) throws Exception {
logger.info("Hello!");
}
}
Following code is Log4j 2 configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="log4j.skipJansi" value="false"/>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout>
<disableAnsi>false</disableAnsi>
<Pattern>%style{%d [%t] %c %p: %m}{yellow}%n%ex</Pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="ConsoleAppender"/>
</Root>
</Loggers>
</Configuration>
The contents of \lib directory:
log4j-core-2.12.1.jar
log4j-api-2.12.1.jar
jansi-1.18.jar
And the Windows compileAndRun.bat file:
#echo off
javac -cp lib\*;. LoggerTest.java
java -cp lib\*;. LoggerTest
pause
exit
However, the output in the command line is still not coloured. This is what I see:
So, the message contains ANSI escape codes, but they are not interpreted by command line. Though, if I try to copy/paste this output and echo it directly using another bat-file, then I see the coloured message.
It worked for when I add disableAnsi="false" propriety to <PatternLayout>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{%p} %logger{-2} - %m{FATAL=red blink,ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold}%n" disableAnsi="false"/>
</Console>
</Appenders>
This is answer how to enable colors with latest log4j on windows 7:
openjdk 14
log4j 2.14.1
jansi 1.18
Configuration:
<?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] %highlight{%-5level}{FATAL=red blink, ERROR=red bold, WARN=yellow bold, INFO=magenta, DEBUG=green, TRACE=blue} %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
The key point is to pass -DLOG4J_SKIP_JANSI=false to JVM. Solution was found in this user guide.
Try executing this command: "reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1"
In Java:
Runtime.getRuntime().exec("reg add HKCU\\Console /v VirtualTerminalLevel /t REG_DWORD /d 1");
I had to include both Jansi and Jcabi for cross-OS rendering -
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.18</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-log</artifactId>
<version>LATEST</version>
<optional>true</optional>
</dependency>

How to open log4j2 logs with Apache Chainsaw

I'm having hard time opening my logs with Apache Chainsaw v2.
I wasn't able to succesfully open my logs with zeroconf, neither with default xmllayout. It worked like a charm for me in .net. But in Java it's a nightmare.
Main class:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Start {
public static Logger logger = LogManager.getLogger(Start.class);
public static void main(String[]args){
logger.info("Example log info");
logger.trace("Example log trace");
logger.debug("Example log debug");
logger.error("Example log error");
logger.fatal("Example log fatal");
logger.warn("Example log warn");
return;
}
}
Log4j2 configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL" advertiser="multicastdns">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="custom layout %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="log4j2Chainsaw" fileName="output.log" bufferedIO="false" advertiseURI="file:///C:/Users/gswiec/IdeaProjects/Log4j2ChainsawExample/output.log" advertise="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="xmlLayoutForChainsaw" fileName="output.xml" bufferedIO="false" >
<XmlLayout/>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
<AppenderRef ref="log4j2Chainsaw"/>
<AppenderRef ref="xmlLayoutForChainsaw"/>
</Root>
</Loggers>
</Configuration>
Here is pom, all needed dependencies seems to be there.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>swiec.eu</groupId>
<artifactId>Log4j2ChainsawExample</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--Log4j2 and apache chainsaw zeroconf dependencies-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>javax.jmdns</groupId>
<artifactId>jmdns</artifactId>
<version>3.4.1</version>
</dependency>
<!--XmlLayout dependencies-->
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
Whole example project is available at https://github.com/swiec/eu.swiec.log4j2apacheexample
I would appreciate your help, also if you suggest different approach to get logs more readable. Apache chainsaw doesn't seems to be live project, I'm not sure if it's good idea using it.
You should test with an app that doesn't start and immediately exit, or it won't have time to advertise & have Chainsaw receive events.
I just added this to your Start.java file:
import java.util.Scanner;
...
public static void main(String[]args){
Scanner in = new Scanner(System.in);
int i = in.nextInt();
I also updated your log4j config - you need to use %m not %msg for the PatternLayout used to generate output.log
<File name="log4j2chainsawappender" fileName="output.log" bufferedIO="false" advertiseURI="file://Users/scott/eu.swiec.log4j2apacheexample/output.log" advertise="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m"/>
I then started Chainsaw v2 (very latest version in git), and started your app.
Once your app was started, I selected Chainsaw's 'connect to, log4j2chainsawappender', and a new tab appeared and correctly formatted your log events, parsing 'Start' as your logger, correct severity levels etc.

How configure SLF4J underlying configuration file location [duplicate]

I have the followed imports:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and the following instantiation:
private static Logger logger = LoggerFactory.getLogger(Test.class);
and the following in my Main method:
logger.info("SOME MESSAGE: ");
However, I'm not able to find the output anywhere. All I see is that in my console there is:
21:21:24.235 [main] INFO some_folder.Test - SOME MESSAGE:
How do I locate the log file?
Note that the following are on my build path:
slf4j-api-1.7.5.jar
slf4j-log4j12-1.6.4.jar
I read the answer to similar questions but nobody actually says how to fix the problem.
slf4j is only an API. You should have a concrete implementation (for example log4j). This concrete implementation has a config file which tells you where to store the logs.
When slf4j catches a log messages with a logger, it is given to an appender which decides what to do with the message. By default, the ConsoleAppender displays the message in the console.
The default configuration file is :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<!-- By default => console -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
If you put a configuration file available in the classpath, then your concrete implementation (in your case, log4j) will find and use it. See Log4J documentation.
Example of file appender :
<Appenders>
<File name="File" fileName="${filename}">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
...
</Appenders>
Complete example with a file appender :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="File" fileName="${filename}">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
As already mentioned its just a facade and it helps to switch between different logger implementation easily. For example if you want to use log4j implementation.
A sample code would looks like below.
If you use maven get the dependencies
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
Have the below in log4j.properties in location src/main/resources/log4j.properties
log4j.rootLogger=DEBUG, STDOUT, file
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
Hello world code below would prints in console and to a log file as per above configuration.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}
It does not write to a file by default. You would need to configure something like the RollingFileAppender and have the root logger write to it (possibly in addition to the default ConsoleAppender).
The log file is not visible because the slf4j configuration file location needs to passed to the java run command using the following arguments .(e.g.)
-Dlogging.config={file_location}\log4j2.xml
or this:
-Dlog4j.configurationFile={file_location}\log4j2.xml

log4j2 configuration with disruptor

I am trying to use log4j2 with disruptor in a java application. I have the following jar files in my classpath:
log4j-api-2.0-rc2.jar
log4j-core-2.0-rc2.jar
disruptor-3.2.0.jar
In my Java class, I a doing the following to test:
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class LoggerTest {
private static final Logger Logger = LogManager.getLogger(LoggerTest.class.getName());
public static void main(String[] args) {
Logger.info("testing log4j2 with disruptor");
}
My log4j2.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Don't forget to set system property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous. -->
<configuration status="INFO">
<appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<FastFile name="RandomAccessFile" fileName="logs/test.log" immediateFlush="false" append="false">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m %ex%n</pattern>
</PatternLayout>
</FastFile>
</appenders>
<loggers>
<root level="info" includeLocation="true">
<appender-ref ref="RandomAccessFile"/>
</root>
</loggers>
</configuration>
When I run the application, I get the following error (with no log output):
2014-07-10 14:45:32,930 ERROR Error processing element FastFile: CLASS_NOT_FOUND
2014-07-10 14:45:32,973 ERROR Unable to locate appender RandomAccessFile for logger
In beta9, the <FastFile> appender was renamed to <RandomAccessFile>. If you rename this element in your configuration it should work.

Log4j2 not logging to console

I cannot get Log4j 2 to log to the console. Nothing is showing up when running with gradle.
log4j2.xml in the projects root directory:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Usage in my classes:
public class ABCHandler {
private final Logger logger = LogManager.getLogger();
public ABC(String serialPortName) {
logger.info("Opening serial port {}", serialPortName);
}
}
Loading your file and configurations on my machine works.
This was the class I used:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test
{
private final Logger logger = LogManager.getLogger(Test.class);
public Test(String serialPortName) {
System.out.println(logger.isInfoEnabled());
logger.entry();
logger.info("info! {}", serialPortName);
logger.error("error! {}", serialPortName);
logger.debug("debug! {}", serialPortName);
}
public static void main(String args[])
{
Test h1 = new Test("1001");
}
}
This is the log4j2.xml:
<ThresholdFilter level="all"/>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p method: [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
and finally, this is the output:
true
2014-05-29 12:19:15,266 TRACE method: [main] Test (Test.java:10) - entry
2014-05-29 12:19:15,268 INFO method: [main] Test (Test.java:11) - info! 1001
2014-05-29 12:19:15,269 ERROR method: [main] Test (Test.java:12) - error! 1001
2014-05-29 12:19:15,269 DEBUG method: [main] Test (Test.java:13) - debug! 1001
One common error when using Log4j2 is placing the log4j2.xml in a file that is not in the classpath.
To diagnose if that is the problem, change the line
logger.info("Opening serial port {}", serialPortName);
to
logger.error("Opening serial port {}", serialPortName);
If you see any output it is because log4j can't load your file. This is because the default log level when the file is not found is ERROR, not DEBUG.
The location of the log4j2.xml on my project (Maven) is in src/main/resources, which I know it is in my classpath.
My problems were:
I was using a log4j.properties file. Once renamed to log4j2.properties the main and test classpaths picked it up
I had to add logging to standard output to build.gradle
test {
testLogging.showStandardStreams = true
}
note: when I added sourcesets according to here it still didn't find the log4j.properties file only renaming it worked!

Categories

Resources