
- log4j - Home
- log4j - Overview
- log4j - Installation
- log4j - Architecture
- log4j - Configuration
- log4j - Sample Program
- log4j - Logging Methods
- log4j - Logging Levels
- log4j - ConsoleAppender
- log4j - Logging in Database
Log4j - Formatting Layouts
- log4j - Log Formatting
- log4j - CSV Parameter Layout
- log4j - CSV Log Event Layout
- log4j - HTML Layout
- log4j - Pattern Layout
- log4j - Json Template Layout
Log4j - File Appenders
- log4j - Logging in Files
- log4j - FileAppender
- log4j - Separate Folder per Month
- log4j - Daily log File Generation
log4j Resources
log4j - CSV Log Event Layout
If you want to encode your complete logging information in an CSV-formatted file, then you can use org.apache.logging.log4j.CSVLogEventLayout to format your logging information.
The CSVLogEventLayout class encodes all details of the message of log event.
For example, consider the below log statements −
LOGGER.info("Record 1 {} {}", "arg1", "arg2"); LOGGER.error("Record 2 {} {} {}", "arg3", "arg4", "arg5", throwable);
The resulted logs will contain following entries in CSV format −
0,1757242265192,DEBUG,1,main,5,Record 1 arg1 arg2,org.apache.logging.log4j.spi.AbstractLogger,com.tutorialspoint.Log4jDemo,,,com.tutorialspoint.Log4jDemo.main(Log4jDemo.java:12),{},[] 0,1757242265199,INFO,1,main,5,Record 2 arg1 arg2 arg3,org.apache.logging.log4j.spi.AbstractLogger,com.tutorialspoint.Log4jDemo,,,com.tutorialspoint.Log4jDemo.main(Log4jDemo.java:13),{},[]
CSVLogEventLayout Fields
CSVLogEventLayout generate records with following fields of a log event:
Time (in nanoseconds)
Time (in milliseconds)
Level
Thread ID
Thread name
Thread priority
Message (formatted, hence including parameters)
Logger FQCN
Logger name
Marker
Throwable
Source
Thread context map
Thread context stack
CSVLogEventLayout Configuration
CSVLogEventLayout can be configured with following parameters:
Parameter | Type | Description |
---|---|---|
format | String | A predefined format name (Default, Excel, MySQL, RFC4180, TDF, etc.) accepted by CSVFormat |
delimiter | Character | The field delimiter character |
escape | Character | The escape character |
quote | Character | The quote character |
quoteMode | String | A quote mode name (ALL, ALL_NONE_NULL, MINIMAL, NON_NUMERIC, NONE, etc.) accepted by QuoteMode |
nullString | String | The string to denote null values |
recordSeparator | String | The record separator string |
charset | Charset | The character encoding |
header | String | The header to include when the stream is opened |
footer | String | The footer to include when the stream is closed |
We need following jars in the classpath −
Example - Usage of CSVLogEventLayout
Following is a simple configuration file for CSVLogEventLayout:
log4j2.properties
# Define the appender appender.0.type = Console appender.0.name = CONSOLE appender.0.layout.type = CSVLogEventLayout # Define the root logger with appender Console rootLogger.level = DEBUG rootLogger.appenderRef.0.ref = CONSOLE
Now consider the following Java Example which would generate logging information:
Log4jDemo.java
package com.tutorialspoint; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4jDemo{ /* Get actual class name to be printed on */ private static final Logger LOGGER = LogManager.getLogger(); public static void main(String[] args) { LOGGER.debug("Record 1 {} {}", "arg1", "arg2"); LOGGER.info("Record 2 {} {} {}", "arg1", "arg2", "arg3"); } }
Output
Compile and run the above program. Console will print the following log information:
0,1757242265192,DEBUG,1,main,5,Record 1 arg1 arg2,org.apache.logging.log4j.spi.AbstractLogger,com.tutorialspoint.Log4jDemo,,,com.tutorialspoint.Log4jDemo.main(Log4jDemo.java:12),{},[] 0,1757242265199,INFO,1,main,5,Record 2 arg1 arg2 arg3,org.apache.logging.log4j.spi.AbstractLogger,com.tutorialspoint.Log4jDemo,,,com.tutorialspoint.Log4jDemo.main(Log4jDemo.java:13),{},[]