officescripts-logging-framework
    Preparing search index...

    Class LayoutImpl

    Default implementation of the Layout interface. Formats log events into a string using a provided or default formatting function.

    • Uses the Strategy Pattern for extensibility: you can pass a custom formatting function (strategy) to the constructor.
    • All events are validated to conform to the LogEvent interface before formatting.
    • Throws ScriptError if the event does not conform to the expected LogEvent interface.
    • Layout for the interface definition.
    • LogEvent for the structure of log events.

    Implements

    Index

    Constructors

    • Constructs a new LayoutImpl with an optional custom formatter.

      Parameters

      • Optionalformatter: 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.

      Returns LayoutImpl

      • Implements the Strategy Pattern: pass a custom formatter to change output format at runtime.
      • Formatter must be a function of arity 1; see LayoutFormatter.
      • Throws ScriptError if the formatter is invalid or the instance is misconfigured.

      ScriptError if:

      • The formatter is not a function or does not accept exactly one argument.
      • The formatter is null or undefined.
      • The instance is 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}` }
      )

    Properties

    defaultFormatterFun: LayoutFormatter

    Convenience 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.

    shortFormatterFun: LayoutFormatter

    Convenience static property to define a short formatter, e.g. [type] message.

    Methods

    • Formats the given log event as a string.

      Parameters

      • event: LogEvent

        The log event to format.

      Returns string

      A string representation of the log event.

      ScriptError if the event does not conform to the LogEvent interface.

    • Returns string

      A string representation of the layout. If the formatter is a function, it returns the name of the function.

    • Asserts 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.

      Parameters

      • layout: Layout

        The object to validate as a Layout implementation.

      • Optionalcontext: string

        (Optional) Additional context for error messages.

      Returns void

      ScriptError if:

      • layout is null or undefined.
      • format is not a function or doesn't have arity 1.