bare-logger
Low-level logger for Bare
v2.0.6bare-logger — Low-level logger for Bare. It is a native addon.
npm i bare-loggerUsage
const Log = require('bare-logger')
const log = new Log()
log.info('Hello %s', 'world!')API
Log
new Log(options?: LogOptions)
Create a logger. debug() and info() write to standard output, and warn(), error(), and fatal() to standard error, one line per call. colors defaults to whether both standard output and standard error are TTYs. Fixed in v2.0.5: if something in the process had made standard output or standard error non-blocking, as libuv does when it opens a stream on one, lines written while the pipe was full went missing or were cut short.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
options? | LogOptions | — | Logger options; colors forces ANSI color styling on or off, defaulting to whether both standard output and standard error are TTYs. |
Log.clear(): void
Clears the log output. A no-op on the base Log.
Log.colors: boolean
Whether this logger applies ANSI color styling to formatted output. Read-only.
Log.debug(...data: unknown[]): void
Logs data at debug level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
Log.error(...data: unknown[]): void
Logs data at error level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
Log.fatal(...data: unknown[]): void
Logs data at fatal level, then ends the process at once with exit code 1, so exit handlers don't run.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
Log.format(...data: unknown[]): string
Formats data the same way debug, info, warn, error, and fatal do, and returns the resulting string without logging it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
Log.info(...data: unknown[]): void
Logs data at info level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
Log.warn(...data: unknown[]): void
Logs data at warn level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
CompositeLog
new CompositeLog(logs: Log[])
Create a logger that forwards every call to each logger in logs, so a single log call can write to, for example, both the console and a file.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
logs | Log[] | — | The loggers to forward every call to, in order. |
CompositeLog.clear(): void
Clears the log output. A no-op on the base Log.
CompositeLog.colors: boolean
Whether this logger applies ANSI color styling to formatted output. Read-only.
CompositeLog.debug(...data: unknown[]): void
Logs data at debug level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
CompositeLog.error(...data: unknown[]): void
Logs data at error level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
CompositeLog.fatal(...data: unknown[]): void
Logs data at fatal level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
CompositeLog.format(...data: unknown[]): string
Formats data the same way debug, info, warn, error, and fatal do, and returns the resulting string without logging it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
CompositeLog.info(...data: unknown[]): void
Logs data at info level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
CompositeLog.warn(...data: unknown[]): void
Logs data at warn level.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown[] | — | Values to format and log; the first may be a printf-style format string (for example %s, %d, %o) with the remaining values as substitutions. |
Types
LogOptions
interface LogOptions {
colors?: boolean
}Options for Log. colors forces ANSI color styling on or off in formatted output.
Threat model
bare-logger is one of the addons Bare compiles into its binary, so every Bare process has it, sealed or not, and it inherits Bare's threat model. The module's own threat model, added in v2.0.5, says where it sits in that one. It writes to the standard output and standard error the process was given, which is how console writes in a default build, and opens nothing else. Whatever sealed code logs goes wherever those two go, so treat them as a way out of the process: sealed code can put any bytes into a file, pipe, or device log at the far end, and escape sequences into a terminal. That sealed code can write to them is not a bug. A memory bug in the C that formats strings and converts them to UTF-8, or any way to reach another file descriptor, is.
Multiple loggers
CompositeLog can be used to output to multiple loggers. Such as console and file loggers.
const { Log, CompositeLog } = require('bare-logger')
const log1 = new Log()
const log2 = new Log()
const log = new CompositeLog([log1, log2])
log.info('Hello %s', 'world!')See also
- Builds on
bare-format. - Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.
Last updated on