Static
Readonly
DEFAULT_Default colors for log events, used if no custom colors are provided.
The colors can be customized by passing a map of LOG_EVENT
types to hex color strings
when calling getInstance()
. Default colors are:
ERROR
: 9c0006
redWARN
: ed7d31
orangeINFO
: 548235
greenTRACE
: 7f7f7f
grayStatic
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.
Returns the map of event types to font colors used by this appender.
A defensive copy of the event fonts map.
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
sendSets the value of the cell, with the event message, using the font defined for the event type, if not font was defined it doesn't change the font of the cell.
a value from enum LOG_EVENT
.
Optional
context: string(Optional) A string to provide additional context in case of an error. If not provided, it uses the class name and method name as context.
Protected
setSet the last log event sent to the appender.
The log event to be sent.
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.
const activeSheet = workbook.getActiveWorksheet() // workbook is input argument of main
const msgCellRng = activeSheet.getRange("C2")
appender = ExcelAppender.getInstance(msgCellRng) // with default log event colors
appender.log("info event", LOG_EVENT.INFO) // Output: In cell C2 with green color: "info event"
// Now we want to test how getInstance() can throw a ScriptError,
// but we can't because the instance was already created and it is a singleton we need clearInstance
appender.clearInstance()
appender = ExcelAppender.getInstance(null) // throws a ScriptError
Static
clearStatic
clearStatic
getReturns the singleton ExcelAppender
instance, creating it if it doesn't exist.
On first call, requires a valid single cell Excel range to display log messages and optional
color customizations for different log events (LOG_EVENT
). Subsequent calls ignore parameters
and return the existing instance.
ExcelScript.Range where log messages will be written. Must be a single cell range and
not null
or undefined
.
Optional. A map of LOG_EVENT
types to hex color codes for the font colors.
If not provided, defaults to the predefined colors in DEFAULT_EVENT_FONTS
.
The user can provide just the colors they want to customize,
the rest will use the default colors. These colors can be defined with or without #
prefix.
The singleton ExcelAppender
instance.
ScriptError if:
msgCellRng
was not defined or if the range covers multiple cellsconst range = workbook.getWorksheet("Log").getRange("A1")
let excelAppender = ExcelAppender.getInstance(range)
excelAppender.log("info event", LOG_EVENT.INFO)
// Example using custom colors for errors events for the rest it uses the default colors:
const customFonts: Record<LOG_EVENT, string> = {
[LOG_EVENT.ERROR]: "0000FF", // blue
}
excelAppender.clearInstance() // Clear the singleton instance
excelAppender = ExcelAppender.getInstance(range, customFonts)
excelAppender.log("custom colored error event", LOG_EVENT.ERROR) // Output: In cell A1 with blue color: "custom colored error event"
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 a specified Excel cell. Logs messages in color based on the
LOG_EVENT
enum:ERROR
: red,WARN
: orange,INFO
: green,TRACE
: grayUsage:
ExcelAppender.getInstance(ExcelScript.Range)
once with a valid single cell range.Example
Remarks
LOG_EVENT
types to hex color strings.ExcelScript.Range
API to write messages, so it is designed to work in Office Scripts.getInstance
method.See
Appender
interface that logs messages to the console.