infront-logger
v1.1.11
Published
The logger supports both writhing to file and to console.
Downloads
273
Readme
Infront Logger
Logging done right
The logger supports both writhing to file and to console.
npm i @infront/logger
Usage
BaseLogger
const {BaseLogger} = require('infront-logger');
let options = {};
let logger = new BaseLogger("component1", options);
logger.log("this is a test message");
HttpLogger (Express Logger)
const {HttpLogger} = require('infront-logger');
const responseInterceptor = (req, res, next) => {
// Save the original response method
const originalSend = res.send;
const originalJSON = res.json;
const originalSendStatus = res.sendStatus;
let responseSent = false;
let startTime = Date.now();
// Override the response method
res.sendStatus = function(status){
try{
const httpLogger = new HttpLogger();
if (!responseSent) {
res.statusCode = status;
httpLogger.request(req);
httpLogger.response(res);
if (status < 400) {
httpLogger.success();
} else {
httpLogger.error("req.sendStatus with error status");
}
responseSent = true;
}
} catch (err){
console.error(err);
}
return originalSendStatus.call(this, status);
};
res.send = function (body){
try{
const httpLogger = new HttpLogger();
if (!responseSent) {
httpLogger.request(req);
httpLogger.response(res);
httpLogger.body(body);
if (res.statusCode < 400) {
httpLogger.success();
} else {
httpLogger.error(body.message);
}
responseSent = true;
}
} catch (err){
console.error(err);
}
// Call the original response method
return originalSend.call(this, body);
};
res.json = function (body){
try{
const httpLogger = new HttpLogger();
if (!responseSent) {
httpLogger.request(req).response(res).body(body);
if (res.statusCode < 400) {
httpLogger.success();
} else {
httpLogger.error(body.message);
}
responseSent = true;
}
} catch (err){
console.error(err);
}
// Call the original response method
return originalJSON.call(this, body);
};
// Continue processing the request
next();
}
app.use(responseInterceptor);
DatadogLogger
import {DatadogLogger} from 'infront-logger/browser';
let loggerOptions = {
clientToken: 'YOUR_DATADOG_CLIENT_TOKEN',
service : "SERVICE_NAME"
}
let logger = new DatadogLogger(loggerOptions);
logger.log("this is a test message", {prop1: "val1"});
FastifyLogger
MongooseLoggerPlugin
Make sure to use before defining schemas;
const {MongooseLoggerPlugin} = require('infront-logger');
const mongoose = require('mongoose');
let plugin = MongooseLoggerPlugin({console : false, filename : 'db.log'});
mongoose.plugin(plugin);
Loggers
BaseLogger
Methods
- session(id)
- log(msg)
- info(msg)
- error(err)
- profile(action, options)
- profileMem(options)
HttpLogger (extends BaseLogger)
Methods
- request(req)
- response(res)
- body(data)
- success(req, res, body)
- error(err)
FastifyLogger (extends BaseLogger)
Methods
- request(req)
- response(res)
- body(data)
- success(req, res, body)
- error(err)
options
- dirname - The directory in which the logs will be created (default: "logs")
- levels - An object of "level name : hierarchical value"
- level - default level (default: info)
- dateFormat : default "YYYY-MM-DD HH:mm:ss:ms"
- file - boolean - should log to file (default : true)
- maxFileSize: The maximum file size for a rotating file strategy (default: "30m")
- maxFiles: The maximum number of files for a rotating file strategy (default: 2)
- filename - name of the default log file (default: "logs.log")
- errorFilename - name of the default error log file (default: "error.log")
- console - boolean - should log to console (default: true)
- consoleJSON - boolean - print full json to console (default : false)