Skip to content

Commit

Permalink
Merge pull request #5 from mahajanankur/crawl-log-format
Browse files Browse the repository at this point in the history
different log formats for crawlInfo and crawlError & logging level bug fix
  • Loading branch information
mahajanankur authored Sep 22, 2021
2 parents 3bf90e0 + 5fdb7bc commit 27c24d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ const LoggerBuilder = function (filename, isExpress) {
return this.build();
},
crawlError: function (...args) {
this.level = "crawlError";
this.level = "crawlerror";
this.message = util.format(...args);
return this.build();
},
crawlInfo: function (...args) {
this.level = "crawlInfo";
this.level = "crawlinfo";
this.message = util.format(...args);
return this.build();
},
Expand Down Expand Up @@ -222,4 +222,4 @@ module.exports = {
LoggerFactory,
ExpressLoggerFactory,
Logger
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "motifer",
"version": "1.2.1",
"description": "Generic logs pattern manager for express, build on top of winston.",
"version": "1.2.2",
"description": "Generic logs pattern manager for express, built on top of winston.",
"main": "index.js",
"scripts": {
"test": "node examples/service.js"
Expand Down
23 changes: 17 additions & 6 deletions winstonClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const REQUEST_ID = "requestId";
const DailyRotateFile = require('winston-daily-rotate-file');

const loggingLevels = {
crawlError: -2,
crawlInfo: -1,
crawlerror: -2,
crawlinfo: -1,
error: 0,
warn: 1,
info: 2,
Expand All @@ -19,15 +19,26 @@ const loggingLevels = {
silly: 6
};

const maskedLevels = {
crawlinfo: 'INFO',
crawlerror: 'ERROR'
};

const customFormat = printf(info => {
let requestId = httpContext.get(REQUEST_ID);
if (info.api) {
return `${info.message}`;
} else {
if (info.isExpress) {
// [${info.args ? JSON.stringify(info.args) : null}]
if(['crawlerror', 'crawlinfo'].includes(info.level)){
return `${info.timestamp} [${maskedLevels[info.level]}] ${info.message}`;
}
return `${info.timestamp} [service] [${requestId ? requestId : null}] [${info.label}] [${info.level.toUpperCase()}] [${info.filename}] ${info.message}`;
} else {
if(['crawlerror', 'crawlinfo'].includes(info.level)){
return `${info.timestamp} [${maskedLevels[info.level]}] ${info.message}`;
}
return `${info.timestamp} [${info.filename}] [${info.label}] [${info.level.toUpperCase()}] [${info.filename}] ${info.message}`;
}
}
Expand Down Expand Up @@ -64,7 +75,7 @@ const buildTransports = (level, options) => {
let transporters = [];
let logLevel = verifyLogLevel(level);
// default console transports
transporters.push(new transports.Console({ logLevel }));
transporters.push(new transports.Console({ level: logLevel }));

if (options && options instanceof Array) {
options.forEach(element => {
Expand Down Expand Up @@ -94,8 +105,8 @@ const buildTransports = (level, options) => {
const verifyLogLevel = (level) => {
let logLevel = defaultLevel;
if (level) {
// logLevel = supportedLevels.includes(level.toLocaleLowerCase()) ? level.toLocaleLowerCase() : defaultLevel;
logLevel = loggingLevels[level] ? level : defaultLevel;
level = level.toLowerCase();
logLevel = loggingLevels[level] || level == 'error' ? level : defaultLevel;
}
return logLevel;
}
}

0 comments on commit 27c24d9

Please sign in to comment.