Log4j2 HTMLLayout配置和输出

时间:2018-01-21 18:50:22

标签: java swing logging log4j log4j2

我正在尝试将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 个答案:

答案 0 :(得分:1)

您的示例中似乎使用的是旧的(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