thunder-log
Version:
The logger used for Shared Services Logging.
75 lines (69 loc) • 2.87 kB
JavaScript
const { format, createLogger, transports } = require("winston");
// formatting for all logs to the console
const consoleFormat = format.printf(({ CorrelationId, SequenceNumber, level, timestamp, LoggerID, UserId, Summary, Action, ActionTarget, ActionResult, ExceptionDetails, StackInfo, AdditionalDetails }) => {
let log = `[${timestamp}] ${level} -VMG- `;
(CorrelationId) ? log += `${CorrelationId} ` : '';
(SequenceNumber) ? log += `${SequenceNumber} ` : '';
(LoggerID) ? log += `${LoggerID} ` : '';
(UserId) ? log += `${UserId} ` : '';
(ExceptionDetails) ? log += `(${ExceptionDetails}) ` : '';
log += `${Summary} `;
(Action) ? log += `/${Action} ` : '';
(ActionTarget) ? log += `${ActionTarget} ` : '';
(ActionResult) ? log += `(${ActionResult}) ` : '';
(StackInfo) ? log += `${StackInfo} ` : '';
(AdditionalDetails) ? log += `: ${AdditionalDetails}` : '';
return log;
});
// formatting for all logs to file in json
const jsonFormat = format.printf(({ CorrelationId, SequenceNumber, level, timestamp, LoggerID, UserId, Summary, Action, ActionTarget, ActionResult, ExceptionDetails, StackInfo, AdditionalDetails }) => {
let log = `"ApplicationId":"VMG", "Severity":"${level}", "Timestamp":"${timestamp}", "Summary":"${Summary}"`;
(CorrelationId) ? log += `"CorrelationId":"${CorrelationId}", ` : '';
(SequenceNumber) ? log += `"SequenceNumber":"${SequenceNumber}", ` : '';
(LoggerID) ? log += `"LoggerId":"${LoggerID}", ` : '';
(UserId) ? log += `"UserId":"${UserId}", ` : '';
(ExceptionDetails) ? log += `"ExceptionDetails":"${ExceptionDetails}", ` : '';
(Action) ? log += `${Action} ` : '';
(ActionTarget) ? log += `"ActionTarget":"${ActionTarget}", ` : '';
(ActionResult) ? log += `"ActionResult":"${ActionResult}", ` : '';
(StackInfo) ? log += `StackInfo":"${StackInfo}", ` : '';
(AdditionalDetails) ? log += `"AdditionalDetails":"${AdditionalDetails}"` : '';
return `{ ${log} }`;
})
// development logger with transports to console and file (log.txt)
const devLogger = () => {
return createLogger({
level: 'debug',
transports: [
new transports.Console({
format: format.combine(
format.timestamp({ format: 'YYYY-DD-MM HH:mm:ss.SSS' }),
format.colorize(),
format.prettyPrint(),
consoleFormat
)
}),
new transports.File({
filename:'log.txt',
format: format.combine(
format.timestamp({ format: 'YYYY-DD-MM HH:mm:ss.SSS' }),
jsonFormat
)
})],
});
};
// production logger with transport to console
const awsLogger = () => {
return createLogger({
level: 'info',
transports: [new transports.Console()],
format: format.combine(
format.timestamp({ format: 'YYYY-DD-MM HH:mm:ss.SSS' }),
jsonFormat,
)
});
};
module.exports = {
devLogger,
awsLogger
};