我们使用log4j1到log4j2桥,
log4j-1.2-api-2.17.1.jar
我们的代码使用PropertyConfigu
System.getProperty( "appserver.Name" );
System.setProperty( "appserver.Name", "/usr/local/logs/server3" );
l4jprops.put( "appserver.Name", "/usr/local/logs/server3" );
PropertyConfigurator.configure( l4jprops );
logger = Logger.getLogger(PfsSystemPropertiesServlet.class.getName());
下面是一个示例log4j设置。
log4j.appender.AuthAppender.File=${appserver.Name}/log4j_api_auth.log
log4j.appender.AuthAppender.DatePattern='.'yyyy-MM-dd
这看起来并不像我们想要的那样编写日志,我们如何才能让这段代码与桥一起工作。那个班有空位。
发布于 2022-01-25 11:56:18
在Log4j 2.17.1之前,PropertyConfigurator
一直是个无名小卒.这将在即将发布的版本(cf )中发生变化。源代码):您的代码应该在没有任何更改的情况下运行()。
为了测试新版本,添加快照存储库
<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Snapshot Repository</name>
<url>https://repository.apache.org/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
并将log4j-1.2-api
的版本设置为2.17.2-SNAPSHOT
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.17.2-SNAPSHOT</version>
</dependency>
编辑:如果您不能使用快照或等待下一个版本的发布,PropertyConfigurator
的行为可以模拟如下:
import org.apache.log4j.config.PropertiesConfiguration;
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.Configurator;
import org.apache.logging.log4j.core.config.NullConfiguration;
// PropertiesConfiguration only accepts an InputStream in 2.17.1
final ByteArrayOutputStream os = new ByteArrayOutputStream();
l4jprops.save(os, null);
final InputStream is = new ByteArrayInputStream(os.toByteArray());
// Initialize to prevent automatic configuration.
Configurator.initialize(new NullConfiguration());
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = new PropertiesConfiguration(ctx, new ConfigurationSource(is), 0);
Configurator.reconfigure(config);
https://stackoverflow.com/questions/70854599
复制