module:logger
- Description:
Logging utility
Default mode is to use util.inspect and output to stdout
Syslog mode can log to local or remote syslog with customizable facility and level
JSON mode will output an object with predefined properties first then the rest will be put inside the data array
{"level":"LOG","date":"2026-05-10T18:22:16.066Z","now":1778437336066,"role":"node","pid":71942, "data": ["shutdownServer:", "api", "node", "server", "closed"] }AsyncLocalStorage is available as
logger.alsand can be used to output contextslogger.als.run({ reqId: "123" }, (req, res) => { ... getUser(req.params.id, (err) => { if (err) logger.error("failed", req.params.id, err); }) })
- Source:
Example
logger.setLevel("info")
logger.log("log") // visible
logger.info("info") // visible
logger.debug("debug") // not visible
logger.error("error") // visible
logger.setLevel("debug")
logger.debug("debug") // visible
logger.setLevel("info")
logger.setDebugFilter("custom")
logger.debug("debug") // still not visible
logger.debug("custom") // visible
Members
(static) als :AsyncLocalStorage
- Description:
if set the current store will be added to the log arguments
- Source:
(static) date :string
- Description:
default date format is $BKJS_LOG_DATE or local or iso or utc or none
- Source:
- Default Value:
- >iso
(static) filters :object
(static) inspectArgs :object
(static) json :boolean
- Description:
if true the outout will be a JSON object
- Source:
- Default Value:
- >false
(static) level :int
- Description:
current logging level, can be set via $BKJS_LOG_LEVEL
- Source:
- Default Value:
- >0(WARN)
(static) levels :object
- Description:
levels sorted by priorities: ERROR, WARN, LOG, INFO, DEBUG, DEV
- Source:
(static) oneline :boolean
- Description:
if true newlines will be replaced with spaces,
- Source:
- Default Value:
- >false
(static) pid :string
- Description:
pid is $BKJS_LOG_PID or the current process pid
- Source:
- Default Value:
- >process.pid
(static) role :string
(static) separator :string
- Description:
concat argumenrs with this separator @default
- Source:
- Default Value:
- >space
(static) tag :string
Methods
(static) debug(…args)
- Description:
Log with DEBUG level, enabled debug filter will be logged if matched
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
(static) dev(…args)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
(static) dump()
(static) error(…args)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
(static) info(…args)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
(static) inspect(level, args) → {string}
- Description:
Called by logger methods to perform input formatting and cleaning, calls default or installed inspector
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
level |
string | current logging level |
args |
Array.<any> | items to output |
Returns:
| Type | Description |
|---|---|
| string |
|
(static) log(…args)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
(static) logger(level, …args)
- Description:
A generic logger method, safe, first arg is supposed to be a logging level, if not valid the error level is used
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
level |
string | logging level, can be one of the default levels or custom one registered with {registerLevel} |
|
args |
any |
<repeatable> |
arguments to log |
(static) none(…args)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
(static) registerLevel(level, callback, optionsopt)
- Description:
Register a custom level handler, must be invoked via
logger.loggeronly, if no handler registered for given level the whole message will be logger as an error. The custom handler is called in the context of the module which means the options are available inside the handler.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
level |
string | custom log level |
|
callback |
function | call it for this level |
|
options |
Object |
<optional> |
(static) setDebugFilter(label, {function)
- Description:
Enable debugging level for this label, if used with the same debugging level it will be printed regardless of the global level, a label is first argument to the
logger.debugmethods, it is used as is, usually the fist argument is the current function name with comma, likelogger.debug("select:", name, args)
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
label |
string | label(s) to debug, can be a comma separated list |
{function |
handler - can be a function to be used instead of regular logging, this is for rerouting some output to a custom console or for
dumping the actual Javascript data without preformatting, most useful to use |
(static) setFile(file)
Parameters:
| Name | Type | Description |
|---|---|---|
file |
string |
Example
logger.setFile('var/log/error.log')
(static) setInspect(inspect, inspectArgsopt)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
inspect |
function | function to output |
|
inspectArgs |
object |
<optional> |
(static) setLevel(level)
- Description:
Set the output level, it can be a number or one of the supported level names, on invalid level then
WARNis set
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
level |
stream |
(static) setOptions(options)
- Description:
Special options for logger to override defaults, syslog options must start with
syslog-For tag, date, pid values may refer to other env variables if start with $, like BKJS_LOG_TAG='$BKJS_TAG'
- Source:
Parameters:
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
Properties
|
(static) setSyslog(facility)
Parameters:
| Name | Type | Description |
|---|---|---|
facility |
int | string | syslog facility as a number or remote syslog server:, nullish value will disable syslog |
Example
logger.setSyslog('unix://')
logger.setSyslog('udp://host?tag=app')
logger.setSyslog('tcp://host:514?facility=LOG_LOCAL0')
// Back to console output
logger.setSyslog()
(static) trace()
(static) warn(…args)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |