Skip to content

Commit

Permalink
Merge pull request #1 from mratanusarkar/feature/basic-impl
Browse files Browse the repository at this point in the history
add: basic logger implementation
  • Loading branch information
mratanusarkar committed Feb 26, 2023
2 parents 0d1ba6e + a8ff9c1 commit 2f8d158
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 0 deletions.
22 changes: 22 additions & 0 deletions index.js
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();
48 changes: 48 additions & 0 deletions logger/logger.js
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;
218 changes: 218 additions & 0 deletions package-lock.json

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

28 changes: 28 additions & 0 deletions package.json
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"
}
}

0 comments on commit 2f8d158

Please sign in to comment.