# Custom Log4j2 Configuration Guide
# Overview
This guide helps you configure a custom log4j2.xml file to control logging behavior in the deployed Java application. With this configuration, you can customize log levels, output formats, rotation policies, and define multiple logging destinations (console, file, etc.).
This document includes:
Instructions to enable custom logging.
Two sample
log4j2.xmlexamples:- Console-based logging
- File-based logging with rolling policy
# How to Use Custom Log4j2 Configuration
Create your custom
log4j2.xmlconfiguration file using one of the provided samples below.Pass the file location to the JVM using the system property in the yCrash launch script file:
-Dlog4j.configurationFile=<absolute path to log4j2.xml>1For example:
-Dlog4j.configurationFile=/opt/myapp/config/log4j2.xml1
# Sample 1: Console-Based Logging
This configuration sends log output to the console (stdout). It's ideal for development or containerized environments.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="logPattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<!-- Application console output -->
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<!-- Usage stats console output -->
<Console name="ConsoleUsageStatsAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d - %X{userId} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<!-- Application-specific logger -->
<AsyncLogger name="com.tier1app.framework.servlet.UsageStatsLogger" level="info" additivity="false">
<AppenderRef ref="ConsoleUsageStatsAppender"/>
</AsyncLogger>
<!-- Root logger -->
<Root level="error">
<AppenderRef ref="ConsoleAppender"/>
</Root>
</Loggers>
</Configuration>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Console Logging Properties Summary
| Property | Description |
|---|---|
status="WARN" | Internal logging level for Log4j itself (helps diagnose config issues). |
monitorInterval="30" | Log4j2 will check for file changes every 30 seconds. |
${logPattern} | Defines the log message format: timestamp, thread, level, logger, and message |
<Console name="ConsoleAppender"> | Sends all logs to standard output (console). |
<Console name="ConsoleUsageStatsAppender"> | Sends usage-specific logs to console with a different pattern. |
<AsyncLogger> | Asynchronous logger for better performance. |
<Root level="error"> | Sets global logging level to ERROR. |
# Sample 2: Rolling File-Based Logging
This configuration writes logs to a rolling log file. It rotates the log daily or when the file size exceeds 10MB, whichever comes first. Old logs are archived in compressed format, and up to 30 log files are retained.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="logDir">logs</Property>
<Property name="logFileName">ycrash.log</Property>
<Property name="logPattern">%d; %p; %c{1.}; %X{userId}; %X{sessionId}; %X{txnId}; %t; %m%n</Property>
</Properties>
<Appenders>
<!-- Main rolling file appender -->
<RollingFile name="RollingFileAppender"
fileName="${logDir}/${logFileName}"
filePattern="${logDir}/logs/${date:yyyy-MM}/ycrash-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingFile>
<!-- Rolling usage stats log -->
<RollingFile name="RollingUsageStatsFileAppender"
fileName="${sys:logDir}/logs/usage-stats.log"
filePattern="${sys:logDir}/logs/${date:yyyy-MM}/usage-stats-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d - %X{userId} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="900 MB"/>
</Policies>
</RollingFile>
<!-- JSON format usage stats log -->
<RollingFile name="RollingUsageStatsJSONFileAppender"
fileName="${sys:logDir}/logs/usage-stats-json.log"
filePattern="${sys:logDir}/logs/${date:yyyy-MM}/usage-stats-%d{MM-dd-yyyy}-%i-json.log.gz">
<PatternLayout>
<pattern>{"timeMillis":"%d","thread":"%t","level":"%p","loggerName":"%c","user":"%X{userId}","message":"%m"}%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="900 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- Application-specific logger -->
<AsyncLogger name="com.tier1app.framework.servlet.UsageStatsLogger" level="info" additivity="false">
<AppenderRef ref="RollingUsageStatsFileAppender"/>
<AppenderRef ref="RollingUsageStatsJSONFileAppender"/>
</AsyncLogger>
<!-- Root logger -->
<Root level="error">
<AppenderRef ref="RollingFileAppender"/>
</Root>
</Loggers>
</Configuration>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File Logging Properties Summary
| Property | Description |
|---|---|
logDir, logFileName | Define the base location and file name for logs. |
filePattern | Defines the archive pattern using timestamp and index (e.g., daily rotation). |
<TimeBasedTriggeringPolicy interval="1"/> | Rotates logs daily. |
<SizeBasedTriggeringPolicy size="10MB"/> | Triggers rollover when log size exceeds 10MB |
<DefaultRolloverStrategy max="30"/> | Retains a maximum of 30 archived log files |
<RollingUsageStatsFileAppender> | Logs usage data in plain text format. |
<RollingUsageStatsJSONFileAppender> | Logs usage data in structured JSON format. |
<PatternLayout> | Defines the layout and format for log entries. |
<AsyncLogger> | Improves performance by logging asynchronously. |