Skip to content

Commit

Permalink
Merge pull request #3 from mratanusarkar/feature/log-rotate
Browse files Browse the repository at this point in the history
add: day wise log split and log files management
  • Loading branch information
mratanusarkar committed Feb 28, 2023
2 parents 2f8d158 + 8ae6f80 commit c714ec3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -6,6 +6,9 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Project Implementation Logs
logFiles

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

Expand Down
25 changes: 19 additions & 6 deletions logger/logger.js
@@ -1,4 +1,5 @@
const winston = require('winston');
require('winston-daily-rotate-file');

// decide on log level based on environment [dev / qa / staging / prod]
const environment = 'dev'; // to be added from process.env
Expand All @@ -12,9 +13,8 @@ let location = 'local runtime'

// output file
let logDir = './logFiles/';
let date = new Date().toISOString().split('T')[0];
let errorFilePath = logDir + date + '-' + 'error.log';
let comboFilePath = logDir + date + '-' + 'combined.log';
let errorFileName = 'error.log';
let comboFileName = 'combined.log';


/*
Expand All @@ -37,9 +37,22 @@ const log = winston.createLogger({
),
defaultMeta: { project: project, repo: repo, type: type, location: location },
transports: [
new winston.transports.File({ filename: errorFilePath, level: 'error' }),
new winston.transports.File({ filename: comboFilePath }),
// new winston.transports.Console({ format: winston.format.simple() })
new winston.transports.DailyRotateFile({
level: 'error',
filename: `${logDir}%DATE%-${errorFileName}`,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: null,
maxFiles: '30d'
}),
new winston.transports.DailyRotateFile({
filename: `${logDir}%DATE%-${comboFileName}`,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: null,
maxFiles: '30d'
}),
// new winston.transports.Console({ format: winston.format.simple() }),
new winston.transports.Console({ format: winston.format.printf(info => `[${info.timestamp}] (${info.level}): ${info.repo} > ${info.category} > ${info.subcategory} - ${info.message} ${info.stack ? '\n' + info.stack : ''}`) })
]
});
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/mratanusarkar/NodeJS-Logging-Mechanism#readme",
"dependencies": {
"winston": "^3.8.2"
"winston": "^3.8.2",
"winston-daily-rotate-file": "^4.7.1"
}
}

0 comments on commit c714ec3

Please sign in to comment.