-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mratanusarkar/feature/basic-impl
add: basic logger implementation
- Loading branch information
Showing
4 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const logger = require('./logger/logger'); | ||
|
||
function todo() { | ||
try { | ||
|
||
logger.log.silly("This is a silly log.", { category: 'index.js', subcategory: 'todo()' }); | ||
logger.log.debug("This is a debug log.", { category: 'index.js', subcategory: 'todo()' }); | ||
logger.log.verbose("This is a verbose log.", { category: 'index.js', subcategory: 'todo()' }); | ||
logger.log.http("This is a http log.", { category: 'index.js', subcategory: 'todo()' }); | ||
logger.log.info("This is a info log.", { category: 'index.js', subcategory: 'todo()' }); | ||
logger.log.warn("This is a warn log.", { category: 'index.js', subcategory: 'todo()' }); | ||
logger.log.error("This is a error log.", { category: 'index.js', subcategory: 'todo()' }); | ||
|
||
let a = mssql.a(); | ||
console.log("print a:", a); // this line will never work due to error above | ||
|
||
} catch (error) { | ||
logger.log.error(error, { category: 'index.js', subcategory: 'todo()' }); | ||
} | ||
} | ||
|
||
todo(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const winston = require('winston'); | ||
|
||
// decide on log level based on environment [dev / qa / staging / prod] | ||
const environment = 'dev'; // to be added from process.env | ||
let level = environment === 'prod' ? 'http' : 'silly' | ||
|
||
// default meta data | ||
let project = 'Project Logging' | ||
let repo = 'NodeJS-Logging-Mechanism' | ||
let type = 'script' | ||
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'; | ||
|
||
|
||
/* | ||
* Note: Log Levels used as per RFC5424 are: | ||
* 0: error | ||
* 1: warn | ||
* 2: info | ||
* 3: http | ||
* 4: verbose | ||
* 5: debug | ||
* 6: silly | ||
*/ | ||
const log = winston.createLogger({ | ||
level: level, | ||
format: winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.errors({ stack: true }), | ||
winston.format.splat(), | ||
winston.format.json() | ||
), | ||
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.Console({ format: winston.format.printf(info => `[${info.timestamp}] (${info.level}): ${info.repo} > ${info.category} > ${info.subcategory} - ${info.message} ${info.stack ? '\n' + info.stack : ''}`) }) | ||
] | ||
}); | ||
|
||
|
||
module.exports.log = log; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "nodejs-logging-mechanism", | ||
"version": "1.0.0", | ||
"description": "a logging framework for node js applications using winston", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mratanusarkar/NodeJS-Logging-Mechanism.git" | ||
}, | ||
"keywords": [ | ||
"log", | ||
"logging", | ||
"winston", | ||
"nodejs" | ||
], | ||
"author": "Atanu Sarkar <mratanusarkar@gmail.com> (https://github.com/mratanusarkar)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mratanusarkar/NodeJS-Logging-Mechanism/issues" | ||
}, | ||
"homepage": "https://github.com/mratanusarkar/NodeJS-Logging-Mechanism#readme", | ||
"dependencies": { | ||
"winston": "^3.8.2" | ||
} | ||
} |