README
CI Logger
CI Logger is a very simple logger designed for CI environments - no colors, no timestamps - just data. Log entries can be formatted to indicate the results of a previous message, and the process can be terminated of an error is logged.
Usage
const logger = require('ci-logger');
logger.log({message: 'Retrieving data from somewhere...'});
// Retrieving data from somewhere...
logger.log({message: 'Error retrieving data', isResult: true, level: logger.levels.warn});
// ⮡ Error retrieving data
logger.log({message: 'Error retrieving data', isResult: true, level: logger.levels.error, exitOnError: true, errorCode: 2});
// ⮡ Error retrieving data
// Fatal error - exiting (2)
The log
function must be passed an object with the following possible properties:
- The
message
property contains the message to be logged and is the only required property. It can be any value exceptundefined
ornull
. - The
level
property must be one of 'info', 'warn', or 'error' (one of the values of thelevels
enumeration exposed by the module). The default value islevels.info
. - The
isResult
property is a boolean value intended to indicate the result of an operation, primarily intended to simplify reading busy CI console logs. IfisResult
is true, the logged message is indented and prefixed with theresultPrefix
string value to indicate it is the result of the preceeding message. If false, the message is not altered. The default value ofisResult
isfalse
. The default value forresultPrefix
is '\u2BA1' ('⮡ '). - The
exitOnError
property is a boolean value indicating whether the process should exit if an error is logged. If true, the error will be logged, a fatal error message will be logged, andprocess.exit
will be called with theerrorCode
value (an integer). The default value ofexitOnError
is true, and the default value oferrorCode
is 1.
The getLogEntry
function can be used to retrieve a complete log entry as it would be logged, with default values populating any unspecified values.
const logger = require('ci-logger');
const logEntry = logger.getLogEntry({message: 'Retrieving data from somewhere...'});
// logEntry = {
// message: 'Retrieving data from somewhere...',
// isResult: false,
// level: 'info',
// exitOnError: true,
// errorCode: 1,
// resultPrefix: '\u2BA1'
// }
Default Log Entry Values
The default values for errorCode
, exitOnError
, isResult
, level
, and resultPrefix
, as detailed above, are used when they are not specified for a specific log entry. If desired, those defaults can be changed using the setLogEntryDefaults
function. This function accepts an object with any or all of these values.
const logger = require('ci-logger');
logger.setLogEntryDefaults({ errorCode: 2, level: logger.levels.warn });
The default values can be set back to the original values via the resetLogEntryDefaults
function.
const logger = require('ci-logger');
logger.resetLogEntryDefaults();