officescripts-logging-framework
    Preparing search index...

    Class ConsoleAppender

    Singleton appender that logs messages to the console. It is used as default appender by this class and by LoggerImpl, if no other appender is defined. The content of the message event sent can be customized via any Layout implementation, but by default it uses the LayoutImpl. Usage:

    • Call ConsoleAppender.getInstance() to get the appender
    • Automatically used if no other appender is defined Warning: The console appender is a singleton, so it should not be instantiated multiple times.
    const appender = ConsoleAppender.getInstance() // Get the singleton instance
    appender.log("This is an info message", LOG_EVENT.INFO) // Log an info message
    // Better use:
    Logger.info("This is an info message") // uses the ConsoleAppender by default.
    • LOG_EVENT for the log event types used to determine the color of the message.
    • LogEvent for the structure of log events.
    • Appender for the interface definition.
    • AbstractAppender for the base class documentation.
    • Layout for the layout used to format log events before sending them to the console.

    Hierarchy (View Summary)

    Implements

    Index

    Properties

    defaultLogEventFactoryFun: LogEventFactory

    Default factory function to create LogEvent instances. Used in case it was not set before by the user.

    Methods

    • To ensure when invoking clearInstance method in a sub-class it also clears (null) the last log event sent to the appender.

      Returns void

      Mainly intended for testing purposes. The state of the singleton will be lost. This method is available only in src folder, it is not deployed in dist folder (production).

    • Log a LogEvent or build a log event using a message string with event type, and if present, with extra fields. It implements both the log methods from the Appender interface, since in Typescript doesn't allow you to overload methods with the same name but with different signatures.

      Parameters

      • arg1: string | LogEvent

        LogEvent or message string.

      • Optionalarg2: LOG_EVENT

        LOG_EVENT type. Only required if arg1 is a string.

      • Optionalarg3: LogEventExtraFields

        extraFields, only used if arg1 is a string.

      Returns void

      ScriptError if:

      • arg1 is a string but arg2 is not provided or is not a valid LOG_EVENT.
      • if the arg1 is not a string and arg1 is not a valid LogEvent.
      • If arg1 is a string, it creates a new LogEvent using the provided message and event type, and if presents extraFields.
      • If arg1 is already a LogEvent, it validates the event and sends it directly.
      • The method uses the static layout to format the log event before sending it to the appenders.
      • The arg3 parameter is optional and can be used to provide extra fields to create a log event, if arg1 is a string.
      • The Template pattern is used, i.e. it implements common logic for logging, but delegates the actual sending of the log event to the sendEvent abstract method. Therefore subclasses must implement the sendEvent method to define how the log event is sent.
      // Example: Log an error message with a custom event type and extra fields.
      const appender = new ConsoleAppender() // Assuming ConsoleAppender extends AbstractAppender
      appender.log("An error occurred", LOG_EVENT.ERROR, { user: "dlealv", id: 42 })
      // Example: Log a warning event directly.
      const warningEvent = new LogEventImpl("This is a warning", LOG_EVENT.WARNING)
      appender.log(warningEvent) // Directly log a LogEvent instance
    • Send the log event to the console. At this point is where the event is formatted before sending it to the console.

      Parameters

      • event: LogEvent

        The log event to output.

      • Optionalcontext: string

        (Optional) A string to provide additional context in case of an error.

      Returns void

      ScriptError if

      • The event is not a valid LogEvent.
      • The instance is not available (not instantiated).
    • Set the last log event sent to the appender.

      Parameters

      • event: LogEvent

        The log event to be sent.

      Returns void

      ScriptError if:

      • The event is not a valid LogEvent.

      Subclasses must call setLastLogEvent(event) after successfully sending the event, otherwise getLastLogEvent() will not reflect the most recent log event.

    • Override toString method. It uses the parent toString method to get the base information, such as the layout and last log event sent to the appender.

      Returns string

      A string representation of the appender, including the class name, since it doesn't have additional properties.

      ScriptError If the singleton was not instantiated

    • Sets to null the singleton instance, useful for running different scenarios. It also sets to null the parent property lastLogEvent, so the last log event is cleared.

      Returns void

      you reset it.

      appender:ConsoleAppender = ConsoleAppender.getInstance()
      appender = ConsoleAppender.log("info event", LOG_EVENT.INFO)
      appender.getLastLogEvent().message // Output: info event"
      ConsoleAppender.clearInstance() // clear the singleton
      appender = ConsoleAppender.getInstance() // restart the singleton
      appender.getLastLogEvent().message // Output: ""

      Mainly intended for testing purposes. The state of the singleton will be lost. This method is available only in src folder, it is not deployed in dist folder (production).

    • Sets to null the static layout, useful for running different scenarios.

      Returns void

      Mainly intended for testing purposes. The state of the singleton will be lost. This method is available only in src folder, it is not deployed in dist folder (production).

    • Sets to null the log event factory, useful for running different scenarios.

      Returns void

      Mainly intended for testing purposes. The state of the singleton will be lost. This method is available only in src folder, it is not deployed in dist folder (production).

    • Returns Layout

      The layout associated to all events. Used to format the log event before sending it to the appenders. If the layout was not set, it returns a default layout (lazy initialization). The layout is shared by all events and all appenders, so it is static.

      Static, shared by all log events. Singleton.

    • Sets the layout associated to all events, the layout is assigned only if it was not set before.

      Parameters

      • layout: Layout

        The layout to set.

      Returns void

      ScriptError if the layout is not a valid Layout implementation.

    • Sets the log event factory function used to create LogEvent instances if it was not set before.

      Parameters

      • logEventFactory: LogEventFactory

        A factory function to create LogEvent instances. Must have the signature (message: string, eventType: LOG_EVENT) => LogEvent, i.e. LogEventFactory type. If not provided, a default factory function is used.

      Returns void

      ScriptError if the log event factory is not a valid function with the expected signature.

      // Example: Custom LogEvent to be used to specify the environment where the log event was created.
      let prodLogEventFactory: LogEventFactory
      = function prodLogEventFactoryFun(message: string, eventType: LOG_EVENT) {
      return new LogEventImpl("PROD-" + message, eventType) // add environment prefix
      }
      AbstractAppender.setLogEventFactory(prodLogEventFactory) // Now all appenders will use ProdLogEvent