本文介绍了在运行时更改log4j属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时更改log4j属性(rootLogger,MaxFileSize等).我该怎么办?

I need to change my log4j properties (rootLogger, MaxFileSize, etc.) at runtime.How can I do this?

推荐答案

https://github.com/apache/jena/blob/master/jena-tdb/log4j.properties 有一个log4j属性文件.

https://github.com/apache/jena/blob/master/jena-tdb/log4j.propertieshas a log4j properties file.

基于此,我正在使用如下所示的configureLog4j帮助函数:

Based on it I am using the configureLog4j helper function shown below like this:

在运行时设置耶拿日志记录级别

String level=org.apache.log4j.Level.OFF.toString();
if (debug)
  level=org.apache.log4j.Level.INFO.toString();
configureLog4j(level);

configureLog4J功能

  /**
   * 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);
  }

这篇关于在运行时更改log4j属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:07