Log4j Pattern Layout: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 19: Line 19:
There is no explicit separator between literal text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character.
There is no explicit separator between literal text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character.


===Literal Text===
==Literal Text==


Literal text may include special characters (\t, \n \r \f). '\\' can be used to insert a backslash in the output.
Literal text may include special characters (\t, \n \r \f). '\\' can be used to insert a backslash in the output.


===Conversion Specifier===
==Conversion Specifier==


Each ''conversion specifier'' starts with a percent sign ('%') and it is followed by an optional [[#Format_Modifier|format modifier]] and a mandatory ''[[#Conversion_Character|conversion character]]'':
Each ''conversion specifier'' starts with a percent sign ('%') and it is followed by an optional [[#Format_Modifier|format modifier]] and a mandatory ''[[#Conversion_Character|conversion character]]'':
Line 29: Line 29:
  %-5c
  %-5c


====Format Modifier====
===Format Modifier===


A ''format modifier'' modifies how the data generated by the [[#Conversion_Specifier|conversion specifier]] is displayed: field with, padding and justification.
A ''format modifier'' modifies how the data generated by the [[#Conversion_Specifier|conversion specifier]] is displayed: field with, padding and justification.


====Conversion Character====
===Conversion Character===


The ''conversion character'' specifies the type of data:
The ''conversion character'' specifies the type of data:
Line 47: Line 47:
* 't': thread name
* 't': thread name


==Throwable Handling==
=Throwable Handling=


If the pattern string does not contain a specifier to handle a Throwable being logged, parsing of the pattern will act as if the "%xEx" specifier had been added to the end of the string. To suppress formatting of the Throwable completely, simply add "%ex{0}" as a specifier in the pattern string.
If the pattern string does not contain a specifier to handle a Throwable being logged, parsing of the pattern will act as if the "%xEx" specifier had been added to the end of the string. To suppress formatting of the Throwable completely, simply add "%ex{0}" as a specifier in the pattern string.

Revision as of 20:00, 15 November 2017

External

Internal

Overview

The pattern layout is defined by a conversion pattern string.

Conversion Pattern

A conversion pattern string , where various elements of a log event can be referred and the way they are intended to be displayed in a log file specified. The conversion pattern is closely related to the conversion pattern of the printf function in C. The conversion pattern is composed of:

There is no explicit separator between literal text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character.

Literal Text

Literal text may include special characters (\t, \n \r \f). '\\' can be used to insert a backslash in the output.

Conversion Specifier

Each conversion specifier starts with a percent sign ('%') and it is followed by an optional format modifier and a mandatory conversion character:

%-5c

Format Modifier

A format modifier modifies how the data generated by the conversion specifier is displayed: field with, padding and justification.

Conversion Character

The conversion character specifies the type of data:

  • 'c': category (or logger).
c{precision}
  • 'd': date.
  • 'm': application supplied message associated with this log event.
  • 'n': platform dependent line separator.
  • 'p': the level of the logging event
  • 't': thread name

Throwable Handling

If the pattern string does not contain a specifier to handle a Throwable being logged, parsing of the pattern will act as if the "%xEx" specifier had been added to the end of the string. To suppress formatting of the Throwable completely, simply add "%ex{0}" as a specifier in the pattern string.