This Log4j2 example will help you in configuring log4j2.xml
file with HTMLLayout
. The HTMLLayout
generates an HTML page and adds each log statement to a row in a table.
1. HTMLLayout Configuration Options
You can use following attributes while configuring HTMLLayout:
locationInfo
– If “true”, location information will be included. The default is false.title
– The title to include in the file header. If none is specified the default title will be used.contentType
– The content type. Defaults to “text/html”.charset
– The character set to use. If not specified, the default will be used.fontSize
– The font size of the text.font
– The font to use for the text.
Let’s see different configurations options for generating HTML format-based log files.
1.1. Log4j2.xml Configuration
<?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>
1.2. Log4j2.properties Configuration
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
1.3. Log4j2.json Configuration
{ "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" } } } } }
2. Log Statements
Now let’s use dome log statements to generate log files.
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 !!"); } }
3. Log4j2 HTMLLayout Output Example
Running the above log files will generate the following HTML file.
Drop me your questions in the comments section.
Reference:
http://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
I want to show only time and message section in tabel how to do that using xml configuration
Hi there, can you tell me what properties needs to be changes in Log4j2.properties file to have multiple Classes writing in same Log file without overwriting?
I am creating private static final LOGGER object in every class with there class name.
I am looking for the same implementation
What is the problem you are facing? Log4j supports creating multiple loggers writing to the same appender. Please share your log4j2 config and the issue you are facing.
For example, in this post we have multiple loggers to log into console appender. You can configure another file appender for all loggers.