Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: day wise log split and log files management #3

Merged
merged 4 commits into from Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
}