gpt4 book ai didi

java - Log4j2 HTMLLayout 配置和输出

转载 作者:行者123 更新时间:2023-12-02 11:43:35 26 4
gpt4 key购买 nike

我试图将 Log4J 包含在我的 Swing 应用程序中,当我每次尝试搜索而不是 Log4J2 时都有 Log4J 1.x 教程时,事情会变得困惑,我相信可能不会有太大改变,但在这一切之间我是没有像我预期的那样通过下面的设置获得 HTML 日志文件作为输出。

我按照 here 的以下说明进行操作:

属性文件:

# log4j2.properties
# Define the root logger with file appender
log4j.rootLogger = DEBUG, HTML

log4j.appender.HTML=org.apache.log4j.FileAppender
log4j.appender.HTML.File=app_${current.date.time}.log

# Define the html layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=App Log
log4j.appender.HTML.layout.LocationInfo=true
log4j.appender.HTML.Threshold=DEBUG

初始化:

// Static block to create new System Property that help us creating new Log time every run
static{

SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyyyhhmmss");
System.setProperty("current.date.time", dateFormat.format(new Date()));
}
// Initiate Logger instance
private static final Logger LOGGER = LogManager.getLogger(MainWindow.class);

写作日志:

public static void main(String args[]) {

// Log
LOGGER.debug("Application Initializing");
//...Other code follows for JFrame and components initialization

我的印象是,即使只进行一行日志记录,我也应该生成一个日志文件,但事实并非如此。我在这里做错了什么?

我尝试在 Apache 的 website 上搜索 2.10 版本中的 .properties 文件更改但没有消除任何疑虑。

如果需要了解的话,我正在使用 NetBeans 8.2。

  • 阿西夫

最佳答案

看起来您在示例中使用的是旧的 (1.2.7) log4j.properites 文件。

使用此链接 https://howtodoinjava.com/log4j2/log4j2-htmllayout-configuration-example/

它还包括一个 log4j2.properies 文件(从博客复制到下面)

status = error
name = PropertiesConfig

#Make sure to change log file path as per your need
property.filename = C:\\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 = Howtodoinjava Info Logs
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 = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

关于java - Log4j2 HTMLLayout 配置和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370459/

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