I need to change my log4j properties (rootLogger, MaxFileSize, etc.) at runtime. How can I do this?
Default log4j properties. The presence of only the ConsoleAppender means that the standard output is directed to the console, not to a log file. Logging to a file. You can change the default log4j.properties configuration so that messages are logged only to a file or to both the console and a file.
Log4j allows multiple configuration files to be used by specifying them as a list of comma separated file paths on log4j.configurationFile or, when using urls, by adding secondary configuration locations as query
The presence of only the ConsoleAppender means that the standard output is directed to the console, not to a log file. You can change the default log4j.properties configuration so that messages are logged only to a file or to both the console and a file. For example, you would change the above configuration to a configuration similar to this:
To accomplish this, Log4j uses variations of Apache Commons Lang 's StrSubstitutor and StrLookup classes. In a manner similar to Ant or Maven, this allows variables declared as $ {name} to be resolved using properties declared in the configuration itself.
Use LogManager.resetConfiguration();
to clear the current config and configure it again.
Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact.
For this to work, I usually add the first appender from code (and not with a config file). That allows me to save a reference which makes it more simple to remove it later.
https://github.com/apache/jena/blob/master/jena-tdb/log4j.properties has a log4j properties file.
Based on it I am using the configureLog4j helper function shown below like this:
set jena logging level at runtime
String level=org.apache.log4j.Level.OFF.toString();
if (debug)
level=org.apache.log4j.Level.INFO.toString();
configureLog4j(level);
configureLog4J function
/**
* configure Log4J
* @param level -the level to use e.g. "INFO", "DEBUG", "OFF"
* see org.apache.log4j.Level
*/
private void configureLog4j(String level) {
Properties props = new Properties();
props.put("log4j.rootLogger", level+", stdlog");
props.put("log4j.appender.stdlog", "org.apache.log4j.ConsoleAppender");
props.put("log4j.appender.stdlog.target", "System.out");
props.put("log4j.appender.stdlog.layout", "org.apache.log4j.PatternLayout");
props.put("log4j.appender.stdlog.layout.ConversionPattern",
"%d{HH:mm:ss} %-5p %-25c{1} :: %m%n");
// Execution logging
props.put("log4j.logger.com.hp.hpl.jena.arq.info", level);
props.put("log4j.logger.com.hp.hpl.jena.arq.exec", level);
// TDB loader
props.put("log4j.logger.org.apache.jena.tdb.loader", level);
// Everything else in Jena
props.put("log4j.logger.com.hp.hpl.jena", level);
props.put("log4j.logger.org.apache.jena.riot", level);
// TDB
// TDB syslog.
props.put("log4j.logger.TDB", level);
props.put("log4j.logger.com.hp.hpl.jena.tdb", level);
props.put("log4j.logger.com.hp.hpl.jena.tdb.transaction", level);
props.put("log4j.logger.com.hp.hpl.jena.tdb.transaction.NodeTableTrans",
level);
props.put("log4j.logger.com.hp.hpl.jena.tdb.transaction.TransactionManager",level);
props.put("log4j.logger.com.hp.hpl.jena.tdb.transaction.TestTransSystem",level);
// Joseki server
props.put("log4j.logger.org.joseki", level);
LogManager.resetConfiguration();
PropertyConfigurator.configure(props);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With