Provide the basic logging mechanism. It can be easy to inject the storage logic for those logging result.
Quick Start • Definition • Example • Use Case • License
Install via npm:
npm install system-logger --save
const logger = require('system-logger');
level
: Logging level representing priorities (error
,warn
,info
,verbose
,debug
,silly
)message
: Major informationoptions
: optional information
const systemlogger = require('system-logger');
const { Logger } = systemlogger;
const logConfig = { level: systemlogger.level.silly };
const logger = new Logger(logConfig);
logger.log('error', `Fail Log Message`, { error: 'err message' });
logger.log('error', `Fail Log Message`, new Error('Timeout'));
logger.log('error', `Fail Log Message`, [1, '1234']);
logger.log('warn', `Warn Log Message`, { warn: 'Should not happening' });
logger.log('info', `Information Log Message`, 'test message');
logger.log('info', `Information Log Message`, {
cId: '34a343a3-7cd0-4d88-a8ed-733ba36d3a3c',
action: { id: 879 },
});
logger.log('verbose', `Verbose Log Message`, {
event: { type: 'open', message: 'test' },
});
logger.log('debug', `Debug Log Message`, {
action: { id: 123, name: 'tester' },
});
logger.log('silly', `Silly Log Message`);
const systemlogger = require('system-logger');
const { Logger } = systemlogger;
const logConfig = { level: systemlogger.level.info };
const fileConfig = { saveToFileName: './track.log' };
const logger = new Logger(logConfig, fileConfig);
logger.log('info', `Information Log Message`, {
cId: '34a343a3-7cd0-4d88-a8ed-733ba36d3a3c',
action: { id: 879 },
});
const systemlogger = require('system-logger');
const { Logger } = systemlogger;
const logConfig = { level: systemlogger.level.info };
logConfig.externalDisplayFormat = (info) => {
if (info.optional === null || typeof info.optional === 'undefined') {
return `${info.timestamp} ${info.level}: ${info.message}`;
} else {
return `${info.timestamp} ${info.level}: ${info.message} [Detail: { cId: ${info.optional.cId}, actionId: ${info.optional.action.id} }]`;
}
};
const logger = new Logger(logConfig);
logger.log('info', `Information Log Message`, {
cId: '34a343a3-7cd0-4d88-a8ed-733ba36d3a3c',
action: { id: 879 },
});
Need to disable the entire logging during testing
const systemlogger = require('system-logger');
const { Logger } = systemlogger;
const logConfig = { level: systemlogger.level.info, silent: true };
const fileConfig = { saveToFileName: './track.log' };
const logger = new Logger(logConfig, fileConfig);
// The following code will execute, but nothing will be logged.
logger.log('info', `Information Log Message`, {
cId: '34a343a3-7cd0-4d88-a8ed-733ba36d3a3c',
action: { id: 879 },
});
logger.log('error', `Fail Log Message`, { error: 'err message' });
Need to rotate log file
const systemlogger = require('system-logger');
const { Logger } = systemlogger;
const logConfig = { level: systemlogger.level.info };
const fileConfig = {
saveToFileName: './track.log',
isFileRotate: true,
fileRotateType: systemlogger.fileRotateType.daily,
};
const logger = new Logger(logConfig, fileConfig);
logger.log('info', `Information Log Tests`, { Detail: 'test' });
Need to persist to DB
const systemlogger = require('system-logger');
const { Logger } = systemlogger;
const logExternalCallBack = async function (
connector,
type,
message,
detail,
cId,
) {
const sql = require('mssql');
const result = await connector
.request()
.input('type_parameter', sql.TinyInt, type)
.input('cId_parameter', sql.NVarChar, cId)
.input('message_parameter', sql.NVarChar, message)
.input('detail_parameter', sql.NVarChar, detail)
.query(`INSERT INTO Process_Log
([LogType], [CId], [Message], [Detail])
VALUES
(@type_parameter,
@cId_parameter,
@message_parameter,
@detail_parameter)`);
return result;
};
const logConfig = { level: systemlogger.level.info };
const mssql = require('mssql');
const localhost = 'localhost';
const database = 'MonitorDB';
const username = 'UserMonitor';
const password = 'Test!23AbcPassword';
const pool = await mssql.connect(
`mssql://${username}:${password}@${localhost}/${database}`,
);
const sourceConfig = {
levels: [
systemlogger.level.error,
systemlogger.level.warn,
systemlogger.level.info,
],
connector: pool,
callback: logExternalCallBack,
};
const logger = new Logger(logConfig, null, sourceConfig);
await logger.log('info', `Simple Log Test`, {
Detail: 'test',
cid: '9c4f5aba-6cb5-4b06-aa50-d6718a41f350',
});
pool.close();
MIT