Static
defaultDefault factory function to create LogEvent
instances. Used in case it was not set before by the user.
Protected
clearTo ensure when invoking clearInstance
method in a sub-class it also clears (null
) the last log event
sent to the appender.
The last log event sent by the appender.
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.
LogEvent
or message string.
Optional
arg2: LOG_EVENTLOG_EVENT
type. Only required if arg1
is a string.
Optional
arg3: LogEventExtraFieldsextraFields
, only used if arg1
is a string.
ScriptError if:
arg1
is a string but arg2
is not provided or is not a valid LOG_EVENT
.arg1
is not a string and arg1
is not a valid LogEvent
.arg1
is a string, it creates a new LogEvent
using the provided message and event type, and if presents extraFields
.arg1
is already a LogEvent
, it validates the event and sends it directly.arg3
parameter is optional and can be used to provide extra fields to create a log event, if arg1
is a string.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
Protected
sendSend the log event to the console. At this point is where the event is formatted before sending it to the console.
The log event to output.
Optional
context: string(Optional) A string to provide additional context in case of an error.
Protected
setSet the last log event sent to the appender.
The log event to be sent.
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.
A string representation of the appender, including the class name, since it doesn't have additional properties.
Static
clearSets 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.
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: ""
Static
clearStatic
clearStatic
getGets the singleton instance of the class.
The singleton instance of the class. If it was not instantiated, it creates a new instance.
Static
getThe 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
getGets 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.
The log event factory function.
Static
setSets the layout associated to all events, the layout is assigned only if it was not set before.
The layout to set.
Static
setSets the log event factory function used to create LogEvent
instances if it was not set before.
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.
// 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
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 anyLayout
implementation, but by default it uses theLayoutImpl
. Usage:ConsoleAppender.getInstance()
to get the appenderExample
See