officescripts-logging-framework
    Preparing search index...

    Class AbstractAppenderAbstract

    Abstract base class for all log appenders. Provides a common interface and shared functionality for concrete appender implementations.

    Features:

    • Allows specifying the formatting of log events via a shared Layout (see Layout).
    • Uses a static LogEventFactory function type to create log events, enabling flexible and customizable event creation.
    • Implements the core logic for logging, delegating the actual delivery of log events to subclasses via the sendEvent method.

    Usage:

    • Extend this class to implement specific appenders (e.g., ConsoleAppender, ExcelAppender).
    • Subclasses must implement the sendEvent(event, context) method to define how log events are delivered.
    • The static layout and log event factory are shared across all appenders.
    • Designed for extensibility and consistency across different output targets.
    • LOG_EVENT for the enumeration of log event types.
    • LogEvent for the structure of log events.
    • Layout for the layout used to format log events before sending them to appenders.
    • LayoutImpl for the default layout implementation.
    • Appender for the interface definition.
    • LogEventFactory for the factory function type used to create log events.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    • Constructs a new AbstractAppender instance. Nothing is initialized, because the class only has static properties that are lazy initialized or set by the user.

      Returns AbstractAppender

    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 appropriate destination. This method must be implemented by subclasses to define how the log event is sent. It is the final responsible for sending the log event to the appropriate destination (e.g., console, file, etc.).

      Parameters

      • event: LogEvent

        The log event to be sent.

      • Optionalcontext: string

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

      Returns void

      ScriptError if - The log event is not a valid LogEvent.

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

    • Returns a string representation of the appender. It includes the class name, layout, log event factory, so far this class doesn't have additional properties to show. Intended to be used by subclasses to provide information of the base class.

      Returns string

      A string representation of the appender.

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

    • Gets the log event factory function used to create LogEvent instances. If it was not set before, it returns the default factory function. The logEventFactory is shared by all events and all appenders, so it is static.

      Returns LogEventFactory

      The log event factory function.

    • 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