CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-logging-log4j--log4j-core

A versatile, industrial-grade, and reference implementation of the Log4j API with rich components for various logging use cases.

Pending
Overview
Eval results
Files

layouts.mddocs/

Layouts

Layouts are responsible for formatting log events into the desired output format. Log4j Core provides numerous built-in layouts for various output formats including pattern-based text, JSON, XML, HTML, CSV, and protocol-specific formats.

Capabilities

Core Layout Interface

Base interface that all layouts must implement.

/**
 * Core layout interface for formatting log events
 * @param <T> The type that the layout returns
 */
public interface Layout<T extends Serializable> extends Encodable {
    /**
     * Get header bytes for the layout output
     * @return Header bytes or null if no header
     */
    byte[] getHeader();
    
    /**
     * Get footer bytes for the layout output  
     * @return Footer bytes or null if no footer
     */
    byte[] getFooter();
    
    /**
     * Format log event to byte array
     * @param event LogEvent to format
     * @return Formatted event as byte array
     */
    byte[] toByteArray(LogEvent event);
    
    /**
     * Format log event to serializable object
     * @param event LogEvent to format
     * @return Formatted event as serializable object
     */
    T toSerializable(LogEvent event);
    
    /**
     * Get content type for this layout
     * @return Content type string
     */
    String getContentType();
    
    /**
     * Get content format descriptors
     * @return Map of format properties
     */
    Map<String, String> getContentFormat();
}

Pattern Layout

Flexible pattern-based text formatting with customizable conversion patterns.

/**
 * Create PatternLayout with builder pattern
 * @return PatternLayout.Builder instance
 */
public static PatternLayout.Builder newBuilder();

/**
 * Create default PatternLayout with standard pattern
 * @return PatternLayout with default pattern
 */
public static PatternLayout createDefaultLayout();

/**
 * Create PatternLayout with custom pattern
 * @param pattern Pattern string to use
 * @return PatternLayout with specified pattern
 */
public static PatternLayout createLayout(String pattern);

/**
 * PatternLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<PatternLayout> {
    public Builder withPattern(String pattern);
    public Builder withCharset(Charset charset);
    public Builder withAlwaysWriteExceptions(boolean alwaysWriteExceptions);
    public Builder withNoConsoleNoAnsi(boolean noConsoleNoAnsi);
    public Builder withHeader(String header);
    public Builder withFooter(String footer);
    public PatternLayout build();
}

Usage Examples:

import org.apache.logging.log4j.core.layout.PatternLayout;
import java.nio.charset.StandardCharsets;

// Create default pattern layout
PatternLayout defaultLayout = PatternLayout.createDefaultLayout();

// Create custom pattern layout
PatternLayout customLayout = PatternLayout.newBuilder()
    .withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n")
    .withCharset(StandardCharsets.UTF_8)
    .withAlwaysWriteExceptions(true)
    .build();

// Create layout with header and footer
PatternLayout headerFooterLayout = PatternLayout.newBuilder()
    .withPattern("%d{ISO8601} [%level] %logger - %msg%n")
    .withHeader("=== Application Start ===%n")
    .withFooter("=== Application End ===%n")
    .build();

// Common pattern examples
String timeStampPattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n";
String shortPattern = "%d{HH:mm:ss.SSS} [%level] %logger{20} - %msg%n";
String verbosePattern = "%d{ISO8601} [%thread] %level %logger{36}:%line - %msg%n";

JSON Layout

Formats log events as JSON objects with configurable field inclusion.

/**
 * Create JsonLayout with builder pattern
 * @return JsonLayout.Builder instance
 */
public static JsonLayout.Builder newBuilder();

/**
 * JsonLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<JsonLayout> {
    public Builder setCharset(Charset charset);
    public Builder setLocationInfo(boolean locationInfo);
    public Builder setProperties(boolean properties);
    public Builder setPropertiesAsList(boolean propertiesAsList);
    public Builder setComplete(boolean complete);
    public Builder setCompact(boolean compact);
    public Builder setEventEol(boolean eventEol);
    public Builder setHeader(String header);
    public Builder setFooter(String footer);
    public JsonLayout build();
}

Usage Examples:

import org.apache.logging.log4j.core.layout.JsonLayout;

// Create basic JSON layout
JsonLayout jsonLayout = JsonLayout.newBuilder()
    .setLocationInfo(false)
    .setProperties(true)
    .setComplete(false)
    .setCompact(true)
    .build();

// Create verbose JSON layout with location info
JsonLayout verboseJsonLayout = JsonLayout.newBuilder()
    .setLocationInfo(true)
    .setProperties(true)
    .setPropertiesAsList(false)
    .setComplete(false)
    .setCompact(false)
    .setEventEol(true)
    .build();

XML Layout

Formats log events as XML elements with configurable structure.

/**
 * Create XmlLayout with builder pattern
 * @return XmlLayout.Builder instance
 */
public static XmlLayout.Builder newBuilder();

/**
 * XmlLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<XmlLayout> {
    public Builder setCharset(Charset charset);
    public Builder setLocationInfo(boolean locationInfo);
    public Builder setProperties(boolean properties);
    public Builder setComplete(boolean complete);
    public Builder setCompact(boolean compact);
    public Builder setHeader(String header);
    public Builder setFooter(String footer);
    public XmlLayout build();
}

HTML Layout

Formats log events as HTML table rows with CSS styling support.

/**
 * Create HtmlLayout with builder pattern
 * @return HtmlLayout.Builder instance
 */
public static HtmlLayout.Builder newBuilder();

/**
 * HtmlLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<HtmlLayout> {
    public Builder setCharset(Charset charset);
    public Builder setLocationInfo(boolean locationInfo);
    public Builder setTitle(String title);
    public Builder setContentType(String contentType);
    public HtmlLayout build();
}

Usage Examples:

import org.apache.logging.log4j.core.layout.HtmlLayout;

// Create HTML layout for web display
HtmlLayout htmlLayout = HtmlLayout.newBuilder()
    .setTitle("Application Log")
    .setLocationInfo(true)
    .setContentType("text/html; charset=UTF-8")
    .build();

CSV Layout

Formats log events as comma-separated values with configurable field selection.

/**
 * Create CsvLayout with builder pattern  
 * @return CsvLayout.Builder instance
 */
public static CsvLayout.Builder newBuilder();

/**
 * CsvLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<CsvLayout> {
    public Builder setCharset(Charset charset);
    public Builder setDelimiter(char delimiter);
    public Builder setEscapeChar(char escapeChar);
    public Builder setFormat(CSVFormat format);
    public Builder setHeader(String header);
    public Builder setFooter(String footer);
    public CsvLayout build();
}

Usage Examples:

import org.apache.logging.log4j.core.layout.CsvLayout;
import org.apache.commons.csv.CSVFormat;

// Create CSV layout with custom delimiter
CsvLayout csvLayout = CsvLayout.newBuilder()
    .setFormat(CSVFormat.DEFAULT.withHeader("Time", "Level", "Logger", "Message"))
    .setDelimiter('|')
    .build();

GELF Layout

Formats log events in Graylog Extended Log Format for centralized logging.

/**
 * Create GelfLayout with builder pattern
 * @return GelfLayout.Builder instance  
 */
public static GelfLayout.Builder newBuilder();

/**
 * GelfLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<GelfLayout> {
    public Builder setHost(String host);
    public Builder setCompressionType(CompressionType compressionType);
    public Builder setCompressionThreshold(int compressionThreshold);
    public Builder setIncludeStackTrace(boolean includeStackTrace);
    public Builder setIncludeThreadContext(boolean includeThreadContext);
    public Builder setIncludeNullDelimiter(boolean includeNullDelimiter);
    public GelfLayout build();
}

YAML Layout

Formats log events in YAML format with readable structure.

/**
 * Create YamlLayout with builder pattern
 * @return YamlLayout.Builder instance
 */
public static YamlLayout.Builder newBuilder();

/**
 * YamlLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<YamlLayout> {
    public Builder setCharset(Charset charset);
    public Builder setLocationInfo(boolean locationInfo);
    public Builder setProperties(boolean properties);
    public Builder setComplete(boolean complete);
    public Builder setHeader(String header);
    public Builder setFooter(String footer);
    public YamlLayout build();
}

Syslog Layout

Formats log events according to syslog protocol standards.

/**
 * Create SyslogLayout with builder pattern
 * @return SyslogLayout.Builder instance
 */
public static SyslogLayout.Builder newBuilder();

/**
 * SyslogLayout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<SyslogLayout> {
    public Builder setCharset(Charset charset);
    public Builder setFacility(Facility facility);
    public Builder setIncludeMdc(boolean includeMdc);
    public Builder setMdcId(String mdcId);
    public Builder setMdcPrefix(String mdcPrefix);
    public Builder setEventPrefix(String eventPrefix);
    public Builder setNewLine(boolean newLine);
    public SyslogLayout build();
}

/**
 * Syslog facility enumeration
 */
public enum Facility {
    KERN(0), USER(1), MAIL(2), DAEMON(3), AUTH(4), SYSLOG(5), 
    LPR(6), NEWS(7), UUCP(8), CRON(9), AUTHPRIV(10), FTP(11),
    LOCAL0(16), LOCAL1(17), LOCAL2(18), LOCAL3(19), 
    LOCAL4(20), LOCAL5(21), LOCAL6(22), LOCAL7(23);
}

RFC5424 Layout

Formats log events according to RFC 5424 syslog standard.

/**
 * Create Rfc5424Layout with builder pattern
 * @return Rfc5424Layout.Builder instance
 */
public static Rfc5424Layout.Builder newBuilder();

/**
 * Rfc5424Layout configuration builder
 */
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<Rfc5424Layout> {
    public Builder setCharset(Charset charset);
    public Builder setFacility(Facility facility);
    public Builder setDefaultId(String defaultId);
    public Builder setEnterpriseNumber(int enterpriseNumber);
    public Builder setIncludeMdc(boolean includeMdc);
    public Builder setMdcId(String mdcId);
    public Builder setMdcPrefix(String mdcPrefix);
    public Builder setEventPrefix(String eventPrefix);
    public Builder setNewLine(boolean newLine);
    public Builder setAppName(String appName);
    public Builder setMessageId(String messageId);
    public Rfc5424Layout build();
}

Pattern Layout Conversion Patterns

Common conversion patterns for PatternLayout:

Date/Time Patterns

  • %d{yyyy-MM-dd HH:mm:ss.SSS} - Full timestamp with milliseconds
  • %d{ISO8601} - ISO 8601 format
  • %d{HH:mm:ss} - Time only
  • %d{ABSOLUTE} - Absolute time format

Logger and Level Patterns

  • %logger{36} - Logger name truncated to 36 characters
  • %c{1} - Short logger name (last component only)
  • %level - Log level (INFO, DEBUG, etc.)
  • %-5level - Left-aligned level with fixed width

Message and Exception Patterns

  • %msg or %m - Log message
  • %ex - Exception stack trace
  • %ex{short} - Shortened exception
  • %rEx - Root cause exception

Thread and Location Patterns

  • %thread or %t - Thread name
  • %location or %l - Full location info
  • %C{1} - Simple class name
  • %M - Method name
  • %L - Line number
  • %F - File name

Formatting Patterns

  • %n - Platform-specific line separator
  • %highlight{pattern} - Highlight based on level
  • %style{pattern}{color} - Apply ANSI color
  • %-20logger - Left-aligned logger with padding

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-logging-log4j--log4j-core

docs

appenders.md

async-logging.md

configuration.md

core-context.md

filters.md

index.md

layouts.md

lookups.md

plugins.md

tile.json