Log

source

The standard logger for all CartaX services.

Constructors

new Log(): Log

Properties

staticdebugEnabled: booleansource

staticformat: LogFormattersource

statictagInjectors: TagInjector[]source

Methods

Static methods

staticaddTagInjector(injector: TagInjector): voidsource

staticdebug(message: string, tags: Tags): voidsource

Log a debug message. Use this for messages that are only useful during development and would not be useful when diagnosing a production issue. If in doubt, use info instead.

The message will only be printed if debug-level logging has been enabled by calling setDebugEnabled.

staticenquoteMaybe(value: string | Object): stringsource

If text contains spaces, returns the value of text surrounded by double-quote characters, with double-quotes already in the string escaped using backslashes. If there are no spaces, returns the original string unchanged.

staticerror(message: string, tags: Tags): voidsource

Log an error message.

staticfatal(message: string, tags: Tags): voidsource

Log a fatal error message. Use this when the application cannot proceed and must shut down.

staticformatCaller(): stringsource

Returns the file and line number of a caller of this function, looking up through the stack to find the first frame that doesn't look like it's part of the logging infrastructure.

staticformatInjectedTags(): string[]source

staticformatTagsMaybe(tags: Tags): string[]source

Format a collection of message tags (key/value pairs) for printing. K/V pairs are formatting in logfmt style (key=value). Values are quoted if they contain spaces; if quoted, quotes within the string are escaped with a backslash character.

If a trace context is active, the trace and span ID will also be included in the returned string.

staticinfo(message: string, tags: Tags): voidsource

Log an informational message.

staticprint(level: string, message: string, tags: Tags): voidsource

Format and print a message of a given log level, with an optional collection of message tags.

staticproxy<In extends any[], Out extends any[]>(loggerFn: Function, transformerFn: Function): Functionsource

Creates a proxy function that receives a set of arguments and returns them in a form that can be passed to a standard logging method. Use this to wrap a standard logger function without causing the wrapper to appear as the caller in the printed message.

The transformer function can take any arguments but must return a tuple that corresponds to the arguments of the logger function being proxied.

For example:

const transform = (...message: string[]) => [message.join(" ")] as const;
const error = Log.proxy(Log.error, transform);
error("a message", "that will", "be joined together");

staticrender(level: string, message: string, tags: Tags): stringsource

Format a message of a given log level, with an optional collection of message tags.

staticsetDebugEnabled(enabled: boolean = true): voidsource

Configure whether or not debug messages are emitted.

staticsetFormatter(formatter: LogFormatter): voidsource

Configure the formatter.

staticwarn(message: string, tags: Tags): voidsource

Log a warning message.