Skip to content

Commit

Permalink
added varargs support in loggers functions and custom log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
mahajanankur committed Oct 5, 2020
1 parent 0aedfe0 commit 2564710
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const logger = Logger.getLogger(__filename);

//Server port configuration.
server.listen(port, () => {
logger.info(`Motifer node server is running on port: ${port}`);
logger.info(`Motifer node server is running on port:`, port);
});

//Register the controllers as routers.
Expand All @@ -119,7 +119,7 @@ const router = express.Router();
//Resources
router.get("/status", async (req, res, next) => {
logger.info("Service status request.");
logger.debug("Service is up, sample debug log.");
logger.debug("Service is up, sample debug log.", req.query);
logger.warn("Warning the parameter is null, sample warn log.");
logger.error("Exception is thrown, sample error log.");
return res.json({message: "Service is running!!"});
Expand Down
5 changes: 3 additions & 2 deletions examples/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const getEmployees = employees => {
employees.forEach(element => {
logger.info(`The first name is ${element.first}`);
otherFunction();
logger.debug(`The function is successfull`);
logger.debug(`The function is successfull`, element);
});
throwException(new Error("Sample Error"));
}

const otherFunction = () => {
logger.debug(`In the other function`);
const obj = { key1: "value1", key2: ["some", "array", "values"] };
logger.debug(`In the other function`, obj);
}

const throwException = (err) => {
Expand Down
32 changes: 20 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { winstonLoggerClient } = require('./winstonClient');
const httpContext = require('express-http-context');
const uuid = require('uuid');
const morgan = require('morgan');
const util = require('util');
// const morganFormat = "combined";
const apiLogLevel = "INFO";
const IS_EXPRESS = true;
Expand Down Expand Up @@ -33,28 +34,35 @@ let LoggerObject = function (level, message, args, filename, isExpress) {
* @param {string} filename filename with path.
*/
const LoggerBuilder = function (filename, isExpress) {
let level;
let message;
let args;
return {
info: function (msg) {
info: function (...args) {
this.level = "info";
this.message = msg;
this.message = util.format(...args);
return this.build();
},
debug: function (msg) {
debug: function (...args) {
this.level = "debug";
this.message = msg;
this.message = util.format(...args);
return this.build();
},
error: function (msg) {
error: function (...args) {
this.level = "error";
this.message = msg;
this.message = util.format(...args);
return this.build();
},
warn: function (msg) {
warn: function (...args) {
this.level = "warn";
this.message = msg;
this.message = util.format(...args);
return this.build();
},
crawlError: function (...args) {
this.level = "crawlError";
this.message = util.format(...args);
return this.build();
},
crawlInfo: function (...args) {
this.level = "crawlInfo";
this.message = util.format(...args);
return this.build();
},
// @depricated
Expand Down Expand Up @@ -179,7 +187,7 @@ morgan.token('id', function getId(req) {

const loggerStream = {
write: (message) => {
message = message.substring(0,message.lastIndexOf('\n'));
message = message.substring(0, message.lastIndexOf('\n'));
let dto = { message, api: true };
logger.info(dto);
}
Expand Down
212 changes: 211 additions & 1 deletion package-lock.json

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

Loading

0 comments on commit 2564710

Please sign in to comment.