Reputation: 1537
I want to set the log level and the filename and the filepath of the logfile. I found a way to do this and it all works but log4j create an additionla logfile. I got the following log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<Appenders>
<File name="logFile" filename="${sys:logDir}${sys:logFilename}" >
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="logFile" />
</Root>
</Loggers>
</Configuration>
In my main function I set ${sys:logDir} and ${sys:logFilename}:
import config.ReadParameterFile;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import java.io.IOException;
public class main {
private final static Logger log = LogManager.getLogger(main.class);
public static void main(String [] args) throws IOException {
ReadParameterFile config = new ReadParameterFile();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config1 = ctx.getConfiguration();
LoggerConfig loggerConfig = config1.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
switch (config.getProtokollProtokollTiefe()){
case "Default":loggerConfig.setLevel(Level.DEBUG);
break;
case "Produktion":loggerConfig.setLevel(Level.ERROR);
break;
case "Test":loggerConfig.setLevel(Level.FATAL);
break;
default:System.out.print("falsche Logtief");
}
System.setProperty("logDir",config.getProtokollDirProtokoll());
System.setProperty("logFilename", config.getProtokollDateinameProtokoll());
ctx.reconfigure();
ctx.updateLoggers();
}
}
It's working like I want but log4j create a file called "${sys:logDir}${sys:logFilename}" in my main project path. Why is log4j2 doing this? I guess it initialize the logfile before I can set the name. How can I change it?
Kindly Regards!
I could solve the prob with the following main:
public class main {
public static void main(String [] args) throws IOException {
ReadParameterFile config = new ReadParameterFile();
System.setProperty("logDir",config.getProtokollDirProtokoll());
System.setProperty("logFilename", config.getProtokollDateinameProtokoll());
switch (config.getProtokollProtokollTiefe()){
case "Default":System.setProperty("logLvl","DEBUG");
break;
case "Produktion":System.setProperty("logLvl","ERROR");
break;
case "Test": System.setProperty("logLvl","ALL");
break;
default:System.out.print("falsche Logtief");
}
}
}
Upvotes: 2
Views: 911
Reputation: 9141
You would need to do:
private static ReadParameterFile config;
private static Logger log;
static {
config = new ReadParameterFile();
System.setProperty("logDir",config.getProtokollDirProtokoll());
System.setProperty("logFilename", config.getProtokollDateinameProtokoll());
log = LogManager.getLogger(main.class);
}
Upvotes: 2
Reputation: 36754
The logger is declared as a static field. This is initialized when the class is loaded, before the main
method is called.
The first call to LogManager
should come after these system properties are set.
You can either initialize the Logger after setting the properties, or set the properties on the command line with the -D
option.
Upvotes: 2