1

我以两种方式生成日志:使用 HTML 和文件(.log 文件)。该文件工作正常,因为所有日志都附加在日志文件中。在同一日志文件中的每一行上都有时间戳。HTML 报告已关闭,因为它仅在日志文件上记录上次运行,并将其他文件作为备份文件附加。有什么方法可以让它在同一个 HTML 日志文件中记录所有内容?

这是文件日志

# Set root logger level to DEBUG and it's only appender to A1.
log4j.rootLogger=DEBUG, A1, FILE, HTML

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

status = warn
name= RollingFileLogConfig

# Log files location
property.basePath = logs

# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}/app.log
appender.rolling.filePattern= ${basePath}/app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.policies.type = Policies

# RollingFileAppender rotation policy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 10MB
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 10
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified

# Delete all files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d

# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = fileLogger

在日志文件中,它记录了这很好

[INFO ] 2022-03-02 10:48:57.781 [main] MyLogger - Actual font properties   src/test/resources/ui/Volgende.png--Inter--false--false--false--false--false--false--
[INFO ] 2022-03-02 10:49:00.056 [main] MyLogger - Actual font properties   src/test/resources/ui/TKeepItSimple.png--Ubuntu--true--false--false--false--false--false--
[INFO ] 2022-03-02 10:49:01.213 [main] MyLogger - Actual font properties   src/test/resources/ui/TKeepItSimpleText.png--Inter--false--false--false--false--false--false--
[INFO ] 2022-03-02 10:49:03.502 [main] MyLogger - Actual font properties   src/test/resources/ui/TKeepItSafe.png--Ubuntu--true--false--false--false--false--false--
[INFO ] 2022-03-02 10:49:04.862 [main] MyLogger - Actual font properties   src/test/resources/ui/TKeepItSafeText.png--Inter--false--false--false--false--false--false--
[INFO ] 2022-03-02 10:49:07.094 [main] MyLogger - Actual font properties   src/test/resources/ui/TKeepItYours.png--Ubuntu--true--false--false--false--false--false--
[INFO ] 2022-03-02 10:49:08.262 [main] MyLogger - Actual font properties   src/test/resources/ui/TKeepItYoursText.png--Inter--false--false--false--false--false--false--

当我运行相同的代码并想要生成 HTML 报告时,我使用了这段代码

log4j.rootLogger=DEBUG, A1, FILE, HTML
log4j.appender.A1=org.apache.log4j.ConsoleAppender

status = warn
name = PropertiesConfig

#Make sure to change log file path as per your need
property.filename = logs/app-info.html

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = rolling

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.html.gz
appender.rolling.layout.type = HTMLLayout
appender.rolling.layout.charset = UTF-8
appender.rolling.layout.title = Datakeeper Visual Testing Report
appender.rolling.layout.locationInfo = true
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20

loggers = rolling

#Make sure to change the package structure as per your application
logger.rolling.name = nl.rabobank
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


# Log files location
property.basePath = logs

# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

# RollingFileAppender rotation policy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 10
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified

# Delete all files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d

# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = fileLogger

我得到了这些备份日志文件。所有这些都应该在同一个 HTML 文件中。我不知道为什么它将它们创建为单独的日志文件。 在此处输入图像描述

这在 HTML 日志文件中

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8"/>
<title>Datakeeper Visual Testing Report</title>
<style type="text/css">
<!--
body, table {font-family:arial,sans-serif; font-size: medium
;}th {background: #336699; color: #FFFFFF; text-align: left;}
-->
</style>
</head>
<body bgcolor="#FFFFFF" topmargin="6" leftmargin="6">
<hr size="1" noshade="noshade">
Log session start time Wed Mar 02 11:52:43 CET 2022<br>
<br>
<table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%">
<tr>
<th>Time</th>
<th>Thread</th>
<th>Level</th>
<th>Logger</th>
<th>File:Line</th>
<th>Message</th>
</tr>

<tr>
<td>75154</td>
<td title="main thread">main</td>
<td title="Level">INFO</td>
<td title="nl.rabobank.utils.log.MyLogger logger">nl.rabobank.utils.log.MyLogger</td>
<td>MyLogger.java:9</td>
<td title="Message">Actual font properties   src/test/resources/ui/Acties.png--Ubuntu--true--false--false--false--false--false--</td>
</tr>
</table>
<br>
</body></html>
4

0 回答 0