API Connect Toolkit logger
Basic usage
The logger supports the following levels:
- debug
- info
- warning
- error (or severe)
- fatal
They are exposed as methods on the logger object and can be used as logger.info(...)
etc.
Default Logging Streams
By default, logs are streamed to the console and to $APIC_CONFIG_PATH/apic.log
. If $APIC_CONFIG_PATH
is not set, it is logged to ~/.apiconnect/apic.log
.
The console logging stream is prettified by the formatter stream in lib/formatter.js
. To change the logging level of the console stream, you can set APIC_LOG_CONSOLE_LEVEL
.
The file logging stream consists of raw JSON logs. To see it in a readable format, you can run cat ~/.apiconnect/apic.log | bunyan
. To change the logging level of the file stream, you can set APIC_LOG_FILE_LEVEL
.
Configuring the logger
The logger can be customized with either the default logger configuration file (~/.apiconnect/logger-options.json
), or a custom file defined by APIC_LOG_CONFIG_FILE
. Your configuration file can be JS or JSON.
All configuration files are merged together; however, the user configuration file takes priority over the default configuration file. Both take priority over the default embedded configuration. Streams are always merged together, never overridden. You cannot remove the default file and console streams.
Default Logger Configuration File ($APIC_CONFIG_PATH/logger-options.json
)
Sample logger-options.json
file:
{
"bunyan": {
"name": "custom logger name",
"src": false
}
}
Note: If you have both logger-options.json
and logger-options.js
, the JSON file takes priority.
User Logger Configuration File ($APIC_LOG_CONFIG_FILE
)
$ export APIC_LOG_CONFIG_FILE="path_to/custom_opts.js"
custom_opts.js:
var osenv = require('osenv');
module.exports =
{
"bunyan": {
"name": "custom bunyan name",
streams: [ {
level: 'debug',
type: 'rotating-file',
period: '1d',
count: 10,
path: path.resolve(osenv.home(), 'custom_file.log'),
}]
}
}
See the bunyan README for information about the available configuration options. These can be specified in the bunyan
element of the config file.
Console Output
The logger provides 2 methods to write to console logger.writeln
and logger.write
.
Both these methods will write to stdout and log output to the log file.
Flushing logs
The logger provides logger.flush()
and logger.exit(exitCode)
methods which should be called before the CLI exits in
order to ensure that all logs are written to disk. Both methods return a Promise object.
Environment variables
APIC_CONFIG_PATH
specifies the directory path containing the system logging configuration file logger-config.json
.APIC_LOG_CONFIG_FILE
specifies the path of the user-level logging configuration.APIC_LOG_CONSOLE_LEVEL
specifies the lowest level of messages logged to the console stream.APIC_LOG_FILE_LEVEL
specifies the lowest level of messages logged to the apic.log
log file.