Constructor
new Logger(name, …args)
Constructs a
Logger
instance with the given name and optional additional prefixes.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | The main name or component for the logger prefix. | |
args |
string | boolean | null | undefined |
<repeatable> |
Additional prefix arguments. |
Properties:
Name | Type | Description |
---|---|---|
logPrefix |
string | The formatted prefix for all log messages from this logger. |
prefixes |
Array.<string> | The list of prefix strings used to build the logPrefix. |
Members
debug
console.debug()
with the logger's prefix.
debugWithTraceObjs
Logs
msg
at debug level but only appends args
if isDebugMode
.
deep
Calls
Logger.debug
to log but only if isDeepDebug
.
info
console.info()
with the logger's prefix.
log
console.log()
with the logger's prefix.
trace
Calls
Logger.debug
to log but only if isDebugMode
.
warn
Call
console.warn()
with the logger's prefix. Checks for Error
objs in
args
in the same way as Logger.error
.
warnWithoutTrace
Logs a warning message with a warning colored prefix (not a real warning level).
Methods
error(msg, …args) → {string}
Logs an error message or
Error
object to the console with the logger's prefix.
Checks whether any element of args
is an instance of Error
for
special handling.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
msg |
string | Error | The error message or Error object. | |
args |
unknown |
<repeatable> |
Additional arguments to log. |
Returns:
The error message string.
- Type
- string
line(msg) → {string}
Concatenates
this.logPrefix
and the given msg
.
Parameters:
Name | Type | Description |
---|---|---|
msg |
string | undefined | The message to prefix. |
Returns:
The prefixed log line.
- Type
- string
logAndThrowError(msg, …args)
Logs an error message and throws an
Error
with the stringified arguments and message.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
msg |
string | The error message. | |
args |
unknown |
<repeatable> |
Additional arguments to include in the error. |
Throws:
-
A new Error with the formatted message, optionally including the first Error argument.
- Type
- Error
logArrayReduction(before, after, objType, reasonopt)
Logs the reduction in size of an array (e.g. after filtering or deduplication).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
before |
Array.<T> | The array before reduction. | |
after |
Array.<T> | The array after reduction. | |
objType |
string | The type of object in the array. | |
reason |
string |
<optional> |
Optional reason for reduction. |
logSortedDict(msg, dict)
Logs a sorted dictionary of string-number pairs.
Parameters:
Name | Type | Description |
---|---|---|
msg |
string | The message to log before the dictionary. |
dict |
StringNumberDict | The dictionary to log. |
logStringifiedProps(msg, obj)
Log a message with stringified properties from an object.
Parameters:
Name | Type | Description |
---|---|---|
msg |
string | |
obj |
Record.<string, (Date|OptionalString|boolean|number)> |
Example
logWithStringifiedProps('End', {a: 'a', count: 5}) // Logs: 'End: a="a", count=5'
logTelemetry(msg, startedAtopt, …args)
Logs a message with the elapsed time since
startedAt
, optionally with additional labels/args.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
msg |
string | The message to log. | |
startedAt |
Date |
<optional> |
The start time to compute elapsed time. |
args |
unknown |
<repeatable> |
Additional arguments or labels. |
tagWithRandomString()
Adds a random string to the logger's prefix (useful for distinguishing logs in concurrent contexts).
tempLogger(arg1, …args) → {module:helpers/logger~Logger}
Returns a new
Logger
with additional prefix arguments appended to this.prefixes
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arg1 |
string | The additional prefix. | |
args |
LoggerArg |
<repeatable> |
More prefix arguments. |
Returns:
A new Logger instance with the extended prefix.
- Type
- module:helpers/logger~Logger
(static) buildEnumLoggers(strEnum) → {Record.<E, module:helpers/logger~Logger>}
Builds a dictionary of
Logger
instances keyed by the values of a string enum.
Parameters:
Name | Type | Description |
---|---|---|
strEnum |
T | The enum that will key the loggers. |
Returns:
Dict of Logger instances keyed by the enum values.
- Type
- Record.<E, module:helpers/logger~Logger>
(static) logBuilder(name, …prefixes) → {function}
Returns a function that builds
Logger
objects with the starting prefixes.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | The main name for the logger. | |
prefixes |
LoggerArg |
<repeatable> |
Additional prefixes. |
Returns:
A fxn that creates
Logger
instances.
- Type
- function
(static) withParenthesizedName(name, parenthesized, …args) → {module:helpers/logger~Logger}
Alternate constructor; makes the first two arguments into a parenthesized bracketed string.
e.g. the prefix will be [name (parenthesized)].
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | The main name for the logger. | |
parenthesized |
string | The value to parenthesize in the prefix. | |
args |
string |
<repeatable> |
Additional prefix arguments. |
Returns:
A new Logger instance with the custom prefix.
- Type
- module:helpers/logger~Logger
Example
Logger.withParenthesizedName('a', 'b', 'c').log('Hello'); // Logs: [a (b)] {c} Hello