Light weight logging module for nodejs
$ npm install log-err
var logerr = require('log-err');
After the module has been loaded, next step is to set the path
of the logger file that will contain api
and module
info for logging.
logerr.setPath(path);
Open your logger file after it has been created. This file will export an object which has the following structure.
module.exports = {
moduleInfo : {
apiInfo : {
trace : 0,
error : 0,
request : 0,
response : 0,
query : 0
}
}
};
moduleInfo
is the file/module where your api function is residing.
apiInfo
is the name of the api function.
If you have an api to send a post request to any server.
Lets say we have an end point : /send_post_request
So, we create a file/module
: externalRequests.js
Now this file has our api function as below :
exports.sendPostRequest = sendPostRequest
function sendPostRequest{
}
Here, moduleInfo
= externalRequests
and apiInfo
= sendPostRequest
First of all, you need to create an object in each api function and pass it to the logger functions.
Lets say the we declare an object named loggingInfo
.
exports.sendPostRequest = sendPostRequest
function sendPostRequest{
var loggingInfo = {
moduleInfo : "externalRequests",
apiInfo : "sendPostRequest"
}
}
Accordingly, your logger file will have following object details,
module.exports = {
externalRequests : {
sendPostRequest : {
trace : 0,
error : 0,
request : 0,
response : 0,
query : 0
}
}
};
To start logging, call any of the following functions depending upon their trace level,
logger.trace(loggingInfo, object, object, object);
logger.error(loggingInfo, object, object, object);
logger.request(loggingInfo, object, object, object);
logger.response(loggingInfo, object, object, object);
logger.query(loggingInfo, object, object, object);
Note
: The number of arguments that you are passing to the logging functions may vary.
To enable or disable logging just toggle
the logging level flag according to your need in the logger file for any module
and api
info you like.
Released under MIT.