gpt4 book ai didi

java - 如何使用 apache log4j2 功能和 log4j2 配置文件写入 CSV 文件?

转载 作者:行者123 更新时间:2023-11-30 07:12:01 25 4
gpt4 key购买 nike

我一直在努力使用 Log4j 中的 CsvParameterLayout 生成 CSV 文件。我对 log4j2 配置文件进行了编码,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="csvLog.fileName">C:/ql/logs/csvLog</Property>
</Properties>
<Appenders>
<RollingFile name="csvFile" fileName="${csvLog.fileName}.tmp"
filePattern="${csvLog.filename}-%d{MM-dd-yyyy}-%i.csv" header="${file-header}">
<CsvParameterLayout delimeter=","/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="csvFile" level="debug"/>
</Root>
</Loggers>
</Configuration>

但是,我无法生成所需的 CSV 文件。有人可以帮我解决这个问题吗?另外,在将来,我想在打印的所有内容之前添加一个唯一的 ID,我将使用线程上下文生成该 ID。

请帮忙。

最佳答案

遵循 CSV Layout 的 Log4j 2 文档这取决于Apache Commons CSV .

查找以下工作片段。

pom.xml

...
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
...

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="csvLog.fileName">csvLog</Property>
<Property name="file-header">column1,column2,column3</Property>
</Properties>
<Appenders>
<RollingFile name="csvFile"
fileName="${csvLog.fileName}.tmp"
filePattern="${csvLog.filename}-%d{MM-dd-yyyy}-%i.csv" >
<CsvParameterLayout delimiter="," header="${file-header}\n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="200" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="csvFile" level="debug"/>
</Root>
</Loggers>
</Configuration>

Java 代码片段

Logger logger = LogManager.getLogger(...);
logger.info("sample output", "foo", "bar");

输出csvLog.tmp

column1,column2,column3
foo,bar

编辑

log4j2.properties

appenders = csvFile

appender.csvFile.type = RollingFile
appender.csvFile.name = CSVFILE
appender.csvFile.fileName = csvLog.tmp
appender.csvFile.filePattern= csvLog-%d{MM-dd-yyyy}-%i.csv
appender.csvFile.layout.type = CsvParameterLayout
appender.csvFile.layout.delimiter = ,
appender.csvFile.layout.header = column1,column2,column3\n
appender.csvFile.policies.type = Policies
appender.csvFile.policies.time.type = TimeBasedTriggeringPolicy
appender.csvFile.policies.time.interval = 1
appender.csvFile.policies.time.modulate = true
appender.csvFile.policies.size.type = SizeBasedTriggeringPolicy
appender.csvFile.policies.size.size=10MB
appender.csvFile.strategy.type = DefaultRolloverStrategy
appender.csvFile.strategy.max = 200

rootLogger.level = debug
rootLogger.appenderRefs = csvFile
rootLogger.appenderRef.csvFile.ref = CSVFILE

关于java - 如何使用 apache log4j2 功能和 log4j2 配置文件写入 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39078327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com