Logger.error() method
Log an error message, if the log level is error or higher.
Signature
error(message?: unknown, ...args: unknown[]): void;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
message |
unknown |
(Optional) Message to log, as accepted by |
|
args |
unknown[] |
Optional variable arguments. |
Returns
void
Remarks
The message is prefixed with error: and passed via console.error(). If the logger was created with the timestamp option, it starts with a timestamp, and the prefix is replaced by [error], also for an Error that comes first (see below).
There is a special case when the first argument is an Error object: the prefix is omitted, since the first line of the expanded error already starts with its name (for example Error: boom). This is deliberate, and only error() does it. The error is expanded with the full stack trace and the cause chain, on several lines, and passed via console.error().
An Error passed after other arguments is also expanded, but the prefix is kept: log.error('failed:', err) outputs error: failed: Error: boom, followed by the stack trace.
To show only the message of an error, on a single line with the prefix, pass its message instead of the object (see **Logging errors** in the description of the class):
try {
// ...
} catch (err) {
// `Error: boom`, followed by the stack trace.
log.error(err)
// `error: boom`, on a single line.
log.error(err instanceof Error ? err.message : String(err))
}
Example
log.error('Not good...')
Generated via tsdoc2docusaurus 1.3.2 by API Extractor/Documenter 7.60.1.