1.1.14 • Published 3 months ago
infront-logger v1.1.14
Infront Logger
Logging done right
The logger supports both writing 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");
StaticLogger
const {staticLogger} = require('infront-logger');
// Simple logging
staticLogger.info("User logged in");
// Logging with context
staticLogger.info("User action", { userId: 123, action: "login" });
// Different log levels
staticLogger.debug("Debug information");
staticLogger.info("Information message");
staticLogger.warn("Warning message");
staticLogger.error("Error occurred", { error: "Details here" });
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)
- warn(msg)
- error(err)
- debug(msg)
- profile(action, options)
- profileMem(options)
- exclude(pattern) - Exclude messages matching string or regex pattern
StaticLogger (extends BaseLogger)
Methods
- log(msg, context?) - Logs at info level
- info(msg, context?)
- warn(msg, context?)
- error(msg, context?)
- debug(msg, context?)
All methods support both string messages and context objects. Context objects are automatically merged into the log context.
HttpLogger (extends BaseLogger)
Methods
- request(req)
- response(res)
- body(data)
- success(req, res, body)
- error(err)
Options
- maxBodyLength - -1 = unlimited, default: 128
FastifyLogger (extends BaseLogger)
Methods
- request(req)
- response(res)
- body(data)
- success(req, res, body)
- error(err)
Options
- maxBodyLength - -1 = unlimited, default: 128
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)
1.1.12
5 months ago
1.1.14
3 months ago
1.1.13
4 months ago
1.1.9
9 months ago
1.1.8
9 months ago
1.1.7
9 months ago
1.1.11
9 months ago
1.1.10
9 months ago
1.1.6
10 months ago
1.1.5
1 year ago
1.1.4
1 year ago
1.1.3
1 year ago
1.1.2
1 year ago
1.1.1
1 year ago
1.1.0
1 year ago
1.0.9
1 year ago
1.0.8
1 year ago
1.0.7
1 year ago
1.0.6
1 year ago
1.0.5
1 year ago
1.0.4
1 year ago
1.0.3
1 year ago
1.0.1
1 year ago
1.0.0
1 year ago