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>