💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Log4j2 `HTMLLayout`配置示例 > 原文: [https://howtodoinjava.com/log4j2/log4j2-htmllayout-configuration-example/](https://howtodoinjava.com/log4j2/log4j2-htmllayout-configuration-example/) 此 [Log4j2](//howtodoinjava.com/log4j2/) 示例将帮助您使用`HTMLLayout`配置`log4j2.xml`文件。 `HTMLLayout`生成 HTML 页面,并将每个 log 语句添加到表中的一行。 ## `HTMLLayout`配置 在配置`HTMLLayout`时,可以使用以下属性: 1. **`locationInfo`** – 如果为`true`,则将包括位置信息。 默认为`false`。 2. **`title`** – 要包含在文件头中的标题。 如果未指定,则使用默认标题。 3. **`contentType`** – 内容类型。 默认为“`text/html`”。 4. **`charset`** – 要使用的字符集。 如果未指定,将使用默认值。 5. **`fontSize`** – 文本的字体大小。 6. **`font`** – 用于文本的字体。 我们来看看用于生成基于 HTML 格式的日志文件的不同配置选项。 #### `log4j2.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn"> <Properties> <Property name="basePath">C:\\logs</Property> </Properties> <Appenders> <RollingFile name="fileLogger" fileName="${basePath}/app-info.html" filePattern="${basePath}/app-info-%d{yyyy-MM-dd}.html"> <HTMLLayout charset="UTF-8" title="Howtodoinjava Info Logs" locationInfo="true" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> </RollingFile> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </Console> </Appenders> <Loggers> <Logger name="com.howtodoinjava" level="debug" additivity="false"> <appender-ref ref="fileLogger" level="debug" /> </Logger> <Root level="debug" additivity="false"> <appender-ref ref="console" /> </Root> </Loggers> </Configuration> ``` #### `log4j2.properties` ```java 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 ``` #### `log4j2.json` ```java { "configuration": { "name": "Default", "appenders": { "RollingFile": { "name":"File", "fileName":"C:/logs/howtodoinjava.html", "filePattern":"C:/logs/howtodoinjava-backup-%d{MM-dd-yy-HH-mm-ss}-%i.html.gz", "HTMLLayout": { "charset":"UTF-8", "title":"Howtodoinjava Info Logs", "locationInfo":"true" }, "Policies": { "SizeBasedTriggeringPolicy": { "size":"10 MB" } }, "DefaultRolloverStrategy": { "max":"10" } } }, "loggers": { "root": { "level":"debug", "appender-ref": { "ref":"File" } } } } } ``` ## 示例日志语句 现在,我们使用圆顶日志语句生成日志文件。 ```java package com.howtodoinjava.log4j2.examples; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4j2HelloWorldExample { private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName()); public static void main(String[] args) { LOGGER.debug("Debug Message Logged !!"); LOGGER.info("Info Message Logged !!"); LOGGER.debug("Another Debug Message !!"); } } ``` ## Log4j2 `HTMLLayout`输出 在上面的日志文件中运行将生成以下 HTMl 文件。 ![Log4j2 HTMLLayout Output](https://img.kancloud.cn/5b/5a/5b5a9c2cf01aed60714c0705308c21d2_1024x286.png) Log4j2 `HTMLLayout`输出 将我的问题放在评论部分。 参考: [http://logging.apache.org/log4j/2.x/manual/layouts.html#HTMLLayout](https://logging.apache.org/log4j/2.x/manual/layouts.html#HTMLLayout) [https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/layout/HtmlLayout.html](https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/layout/HtmlLayout.html)