我创建了一个servlet,以在运行时更改日志级别,下面是进行重新初始化的代码段。
String propertyFile = request.getParameter("log4jproperties");
Properties log4jProperties = new Properties();
log4jProperties.load(new StringReader(propertyFile));
LogManager.resetConfiguration();
PropertyConfigurator.configure(log4jProperties);
logger.info("Hello at info");
logger.error("Hello at error");
System.out.println("Configured Succesfully ");
我已将调试标志设置为true。
-Dlog4j.debug
当我更改日志级别时,我看到使用在servlet中传递的新属性运行的log4j的重新初始化。我还看到与属性中传递的日志级别相对应的servlet中的“ Hello”日志消息。
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4jProperties.size()18
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Parsing for [root] with value=[ERROR,Portal,console].
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Level token is [ERROR].
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Category root set to ERROR
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Parsing appender named "Portal".
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Parsing layout options for "Portal".
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Setting property [conversionPattern] to [[%d{MM/dd/yy HH:mm:ss.SSS }] %t [%p]%m%n].
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: End of parsing for "Portal".
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Setting property [maxFileSize] to [100MB].
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Setting property [maxBackupIndex] to [20].
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Setting property [file] to [/applogs/MemberPortal/MemberPortal.log].
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: setFile called: /applogs/MemberPortal/MemberPortal.log, true
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: setFile ended
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Parsed "Portal" options.
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Parsing appender named "console".
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Parsing layout options for "console".
[10/11/18 15:14:14:487 EDT] 000001ab SystemOut O log4j: Setting property [conversionPattern] to [[%d{MM/dd/yy HH:mm:ss.SSS }] %t %c %r [%p]%m%n].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: End of parsing for "console".
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Parsed "console" options.
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Parsing for [com.BCBSTN.OpenToken] with value=[ERROR,OpenToken,console].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Level token is [ERROR].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Category com.BCBSTN.OpenToken set to ERROR
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Parsing appender named "OpenToken".
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Parsing layout options for "OpenToken".
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Setting property [conversionPattern] to [[%d{MM/dd/yy HH:mm:ss.SSS }] %t [%p]%m%n].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: End of parsing for "OpenToken".
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Setting property [maxFileSize] to [100MB].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Setting property [file] to [/applogs/OpenToken/OpenToken.log].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Setting property [maxBackupIndex] to [20].
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: setFile called: /applogs/OpenToken/OpenToken.log, true
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: setFile ended
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Parsed "OpenToken" options.
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Parsing appender named "console".
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Appender "console" was already parsed.
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Handling log4j.additivity.com.BCBSTN.OpenToken=[false]
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Setting additivity for "com.BCBSTN.OpenToken" to false
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O log4j: Finished configuring.
[10/11/18 15:14:14:488 EDT] 000001ab SystemOut O [10/11/18 15:14:14.488 ] WebContainer : 2 com.bcbst.mp.updateotherinsurance.controller.ResetLog4jSettings 500736 [ERROR]Hello at error
[10/11/18 15:14:14:489 EDT] 000001ab SystemOut O Configured Succesfully
这是我作为参数传递给servlet的日志文件。
# Console Configuration
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{MM/dd/yy HH:mm:ss.SSS }] %t %c %r [%p]%m%n
#Member Portal Rolling File Configuration
log4j.appender.Portal=org.apache.log4j.RollingFileAppender
log4j.appender.Portal.File=/applogs/MemberPortal/MemberPortal.log
log4j.appender.Portal.MaxFileSize=100MB
log4j.appender.Portal.MaxBackupIndex=20
log4j.appender.Portal.layout=org.apache.log4j.PatternLayout
log4j.appender.Portal.layout.ConversionPattern = [%d{MM/dd/yy HH:mm:ss.SSS }] %t [%p]%m%n
#Open Token Rolling File Configuration
log4j.appender.OpenToken=org.apache.log4j.RollingFileAppender
log4j.appender.OpenToken.File=/applogs/OpenToken/OpenToken.log
log4j.appender.OpenToken.MaxFileSize=100MB
log4j.appender.OpenToken.MaxBackupIndex=20
log4j.appender.OpenToken.layout=org.apache.log4j.PatternLayout
log4j.appender.OpenToken.layout.ConversionPattern = [%d{MM/dd/yy HH:mm:ss.SSS }] %t [%p]%m%n
# Root Configuration - All packages
# Always keep this configuration
log4j.rootLogger=DEBUG,Portal,console
# OPEN TOKEN is a shared jar , needs restart of JVM to effect changes in log configuration #
log4j.logger.com.BCBSTN.OpenToken=DEBUG,OpenToken,console
log4j.additivity.com.BCBSTN.OpenToken=false
供应商jar中的日志处于DEBUG级别。 我不明白的是为什么jar不能动态选择日志级别的变化。
我必须更改文件“ customlogFile”,然后重新启动服务器以使更改生效。
-Dlog4j.configuration=file:/customlogFile.properties
该servlet作为war捆绑在一起,供应商jar和log4j部署在服务器上。 (因此,我相信在jar和servlet中加载类的类加载器可能有所不同)。
有人可以帮我弄清楚为什么Jar中的记录器不接受日志级别更改的原因。我想念什么。