Constructs a new LayoutImpl
with an optional custom formatter.
Optional
formatter: LayoutFormatter(Optional) A function that formats a LogEvent
as a string.
If omitted, uses the default formatter: [timestamp] [type] message
.
The formatter must accept a single LogEvent
parameter, and return a string.
ScriptError
if the formatter is invalid or the instance is misconfigured.ScriptError if:
null
or undefined
.null
or undefined
(e.g., due to subclassing or mutation).// Default formatter:
const layout = new LayoutImpl()
// Custom JSON formatter:
const jsonLayout = new LayoutImpl(event => JSON.stringify(event))
// XML formatter:
const xmlLayout = new LayoutImpl(event =>
`<log><type>${event.type}</type><message>${event.message}</message></log>`
)
// Short format: [type] message
const shortLayout = new LayoutImpl(e => `[${LOG_EVENT[e.type]}] ${e.message}`)
// Named function for better diagnostics in toString():
const namedShortLayout = new LayoutImpl(
function shortLayoutFun(e: LogEvent): string { return `[${LOG_EVENT[e.type]}] ${e.message}` }
)
Static
defaultConvenience static property to define a long formatter used as default, e.g. [timestamp] [type] message
.
This is the default formatter used if no custom formatter is provided.
Static
shortConvenience static property to define a short formatter, e.g. [type] message
.
Formats the given log event as a string.
The log event to format.
A string representation of the log event.
A string representation of the layout. If the formatter is a function, it returns the name of the function.
Static
validateAsserts that the provided object implements the Layout
interface.
Checks for the public format
method (should be a function taking one argument).
It used by LayoutImpl
and appenders to ensure that the layout is valid before using it.
The object to validate as a Layout
implementation.
Optional
context: string(Optional) Additional context for error messages.
Default implementation of the
Layout
interface. Formats log events into a string using a provided or default formatting function.Remarks
LogEvent
interface before formatting.ScriptError
if the event does not conform to the expectedLogEvent
interface.See