自上周以来,我收到了这个奇怪的错误。没有对我的代码进行任何修改,如下所示。
我在SO Answers中遇到了同样的错误,但大多数都提到了xml解析问题,在我的情况下它根本没有。
另一个奇怪的事情是它没有给我堆栈跟踪,请在代码中读出注释以便清晰
private void initializeLoggerContext(Properties properties) throws IOException {
System.out.println("initializeLoggerContext : Properties -> " + properties.toString());
ByteArrayOutputStream output = new ByteArrayOutputStream();
properties.store(output, null);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
Configuration conf = null;
try {
ConfigurationSource c = new ConfigurationSource(input);
//conf = PropertiesConfigurationFactory.getInstance().getConfiguration(new ConfigurationSource(input));
ConfigurationFactory conffact = PropertiesConfigurationFactory.getInstance();
System.out.println("ConfigurationFactory conffact = " + conffact.toString());
//ERROR : below line prints message on the console as "[Fatal Error] :1:1: Content is not allowed in prolog." with no stack trace
conf = conffact.getConfiguration(c);
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.start(conf);
LOGGER.info("Logging configuration is : {}", ctx);
} catch(Exception e) {//Note getting called when getting "[Fatal Error] :1:1: Content is not allowed in prolog." on the console
System.out.println("initializeLoggerContext : Exception is -> ");
e.printStackTrace();
}
}
我已经使用原始输入属性在下面创建了一个示例应用,但它仍然无法使用log4j 2.5。当我尝试使用带有一个键值对的simple属性时,结果也是相同的。请检查以下代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EcommLoggingIssueByDeven {
private static final Logger LOGGER = LoggerFactory.getLogger(EcommLoggingIssueByDeven.class);
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
String str = "appender.jdbc.policies.type=Policies"
+ ", logger.xmanager.appenderRefs=xmanager"
+ ", appender.remedy.fileName=${remedyfilename}"
+ ", logger.xmanager.additivity=false"
+ ", appender.eventFramework.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", appender.spring.layout.type=PatternLayout"
+ ", appender.application.fileName=${appfilename}"
+ ", appender.scm.policies.size.type=SizeBasedTriggeringPolicy"
+ ", appender.scm.layout.type=PatternLayout"
+ ", appender.remedy.filePattern=${sys:ECOMM_HOME}/logs/filename-%i.log.gz"
+ ", logger.jdbc.additivity=false"
+ ", appender.application.type=RollingFile"
+ ", rootLogger.appenderRefs=application"
+ ", appender.eventFramework.policies.size.size=1MB"
+ ", appender.application.strategy.max=10"
+ ", appender.spring.type=RollingFile"
+ ", appender.remedy.policies.size.type=SizeBasedTriggeringPolicy"
+ ", appender.eventFramework.policies.type=Policies"
+ ", status=info"
+ ", property.eventFrameworkfilename=${sys:ECOMM_HOME}/logs/filename.log"
+ ", logger.application.level=DEBUG"
+ ", appender.remedy.name=remedy"
+ ", appender.application.policies.type=Policies"
+ ", logger.remedy.name=com.ecommerce.fulfillment.external.remedy"
+ ", logger.jdbc.appenderRef.jdbc.ref=jdbc"
+ ", logger.remedy.level=INFO"
+ ", logger.remedy.appenderRefs=remedy"
+ ", appender.application.filePattern=/log/ecomm-logging/app-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz"
+ ", appender.spring.fileName=${springfilename}"
+ ", logger.eventFramework.appenderRef.eventFramework.ref=eventFramework"
+ ", appender.scm.policies.size.size=1MB"
+ ", logger.eventFramework.name=com.ecommerce.fulfillment.common.eventframework"
+ ", property.appfilename=/log/ecomm-logging/app.log"
+ ", appender.console.layout.pattern=%m%n"
+ ", appender.eventFramework.layout.type=PatternLayout"
+ ", appender.spring.policies.type=Policies"
+ ", appender.config.layout.type=PatternLayout"
+ ", logger.scm.level=INFO, rootLogger.level=INFO"
+ ", appender.spring.strategy.max=5"
+ ", appender.remedy.policies.size.size=1MB"
+ ", logger.application.appenderRefs=application"
+ ", appender.application.name=application"
+ ", appender.xmanager.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", appender.spring.name=spring"
+ ", appender.jdbc.type=RollingFile"
+ ", appender.jdbc.layout.type=PatternLayout"
+ ", logger.spring.name=org.springframework"
+ ", logger.jdbc.appenderRefs=jdbc"
+ ", logger.scm.name=com.platform"
+ ", appender.scm.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", property.scmfilename=${sys:ECOMM_HOME}/logs/filename.log, logger.spring.appenderRef.spring.ref=spring"
+ ", logger.xmanager.level=INFO"
+ ", appender.xmanager.layout.type=PatternLayout"
+ ", appender.spring.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", appender.scm.filePattern=${sys:ECOMM_HOME}/logs/filename-%i.log.gz"
+ ", appender.jdbc.policies.size.type=SizeBasedTriggeringPolicy"
+ ", logger.config.level=INFO"
+ ", logger.eventFramework.level=INFO"
+ ", appender.config.strategy.max=5"
+ ", appender.config.policies.size.type=SizeBasedTriggeringPolicy"
+ ", appender.spring.policies.size.type=SizeBasedTriggeringPolicy"
+ ", logger.remedy.additivity=false, logger.jdbc.level=INFO"
+ ", appender.scm.type=RollingFile"
+ ", name=PropertiesConfig"
+ ", appender.eventFramework.strategy.max=5"
+ ", appender.scm.strategy.type=DefaultRolloverStrategy"
+ ", logger.application.name=com.ecommerce.fulfillment"
+ ", appender.xmanager.fileName=${xmanagerfilename}"
+ ", appender.scm.fileName=${scmfilename}"
+ ", rootLogger.appenderRef.application.ref=application"
+ ", logger.application.additivity=false"
+ ", appender.spring.filePattern=${sys:ECOMM_HOME}/logs/filename-%i.log.gz"
+ ", appender.xmanager.type=RollingFile"
+ ", appender.xmanager.policies.size.type=SizeBasedTriggeringPolicy"
+ ", appender.jdbc.strategy.type=DefaultRolloverStrategy"
+ ", appender.eventFramework.fileName=${eventFrameworkfilename}"
+ ", logger.spring.appenderRefs=spring"
+ ", appender.config.policies.type=Policies"
+ ", appender.jdbc.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", appender.jdbc.name=jdbc, appender.xmanager.policies.type=Policies"
+ ", appender.eventFramework.filePattern=${sys:ECOMM_HOME}/logs/filename-%i.log.gz"
+ ", appender.jdbc.policies.size.size=3MB"
+ ", property.configfilename=/log/ecomm-logging/config1.log"
+ ", appender.console.layout.type=PatternLayout"
+ ", appender.config.policies.size.size=1MB"
+ ", appender.spring.policies.size.size=3MB"
+ ", loggers=application,config,eventFramework,jdbc,remedy,scm,spring,xmanager"
+ ", logger.scm.appenderRef.scm.ref=scm"
+ ", appender.config.filePattern=/log/ecomm-logging/config-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz"
+ ", logger.config.appenderRef.config.ref=config"
+ ", appender.eventFramework.strategy.type=DefaultRolloverStrategy"
+ ", appender.scm.name=scm"
+ ", logger.config.appenderRefs=config"
+ ", appender.xmanager.policies.size.size=5MB"
+ ", appender.config.fileName=${configfilename}"
+ ", appender.jdbc.filePattern=${sys:ECOMM_HOME}/logs/filename-%i.log.gz"
+ ", appenders=application,config,eventFramework,jdbc,remedy,scm,spring,xmanager"
+ ", appender.remedy.policies.type=Policies"
+ ", appender.xmanager.name=xmanager"
+ ", property.xmanagerfilename=${sys:ECOMM_HOME}/logs/filename.log"
+ ", logger.spring.level=INFO"
+ ", appender.application.policies.size.type=SizeBasedTriggeringPolicy"
+ ", logger.xmanager.appenderRef.xmanager.ref=xmanager"
+ ", property.filename=/log/ecomm-logging/test.log"
+ ", logger.eventFramework.appenderRefs=eventFramework"
+ ", appender.application.strategy.type=DefaultRolloverStrategy"
+ ", appender.spring.strategy.type=DefaultRolloverStrategy"
+ ", logger.spring.additivity=false"
+ ", logger.application.appenderRef.application.ref=application"
+ ", appender.xmanager.filePattern=${sys:ECOMM_HOME}/logs/filename-%i.log.gz"
+ ", logger.scm.appenderRefs=scm"
+ ", appender.scm.strategy.max=5"
+ ", appender.console.type=Console"
+ ", logger.eventFramework.additivity=false"
+ ", appender.application.policies.size.size=5MB"
+ ", appender.jdbc.strategy.max=5"
+ ", logger.remedy.appenderRef.remedy.ref=remedy"
+ ", appender.config.type=RollingFile"
+ ", appender.eventFramework.type=RollingFile"
+ ", appender.jdbc.fileName=${jdbcfilename}"
+ ", logger.scm.additivity=false"
+ ", property.remedyfilename=${sys:ECOMM_HOME}/logs/filename.log"
+ ", appender.application.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", logger.config.name=com.ecommerce.fulfillment.common.configuration"
+ ", logger.xmanager.name=com.ecommerce.fulfillment.common.xmanagerframework"
+ ", appender.config.strategy.type=DefaultRolloverStrategy"
+ ", appender.remedy.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", appender.application.layout.type=PatternLayout"
+ ", appender.remedy.layout.type=PatternLayout"
+ ", appender.xmanager.strategy.type=DefaultRolloverStrategy"
+ ", appender.config.layout.pattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}{GMT+0} %p %env %appDetail [%t] %l %customInfo%msgInfo %n"
+ ", logger.config.additivity=false"
+ ", appender.console.name=STDOUT"
+ ", property.jdbcfilename=${sys:ECOMM_HOME}/logs/filename.log"
+ ", appender.config.name=config"
+ ", appender.eventFramework.name=eventFramework"
+ ", appender.xmanager.strategy.max=5"
+ ", packages=com.ecommerce.logging.plugins"
+ ", logger.jdbc.name=org.springframework.jdbc.core"
+ ", appender.remedy.strategy.type=DefaultRolloverStrategy"
+ ", appender.remedy.strategy.max=5"
+ ", appender.remedy.type=RollingFile"
+ ", appender.scm.policies.type=Policies"
+ ", appender.eventFramework.policies.size.type=SizeBasedTriggeringPolicy"
+ ", property.springfilename=${sys:ECOMM_HOME}/logs/filename.log";
properties.load(new StringBufferInputStream(str));
System.out.println("Properties -> " + properties.toString());
ByteArrayOutputStream output = new ByteArrayOutputStream();
properties.store(output, null);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
Configuration conf = null;
conf = PropertiesConfigurationFactory.getInstance().getConfiguration(new ConfigurationSource(input));
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.start(conf);
LOGGER.info("Logging configuration is : {}", ctx);
}
}
答案 0 :(得分:2)
你试过这个:
PropertiesConfiguration config = new PropertiesConfigurationBuilder().setConfigurationSource(source)
.setRootProperties(properties).build();
Configurator.initialize(config);
这实际上是PropertiesConfigurationFactory的作用: http://logging.apache.org/log4j/2.x/log4j-core/xref/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationFactory.html
考虑到这一点,问题可能是对LoggerContext.start(Configuration)
的呼唤。相反,你需要做Configurator.initialize(config)
。