This is a package to add logging to request/response packages inside backend services and web applications.
Using NPM:
npm install @ismaelalves/logger
There are 4 ways to use this package, for now. This package can be:
- A simple logger that uses bunyan as logging package.
- A middleware for Express services, used to log all requests received by the service
- An interceptor for axios request package, logging all requests that axios executes
- An interceptor for request package, logging all requests that request executes
- A redact package to remove secret information from the logs
The logger configuration (type LoggerConfig
) has these properties:
Name | Description | Type | Required |
---|---|---|---|
PROJECT_NAME | The name of the project using the logger | String | true |
LOG_LEVEL | The level to start logging the messages | Log Level | false |
OMIT_ROUTES | Routes that the express middleware will not log | String[] | false |
STREAMS | Configuration for the location of the output of the log level | Stream | false |
const { Logger } = require('@ismaelalves/logger').init({
PROJECT_NAME: 'project-name',
});
Logger.info(JSON.stringify({
message: 'This is a cool message',
origin: 'Some Place',
status: 200,
}));
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":245,"level":30,"msg":"{\"message\":\"This is a cool message\",\"origin\":\"Some Place\",\"status\":200}","time":"2019-09-10T00:42:46.361Z","v":0}
*/
const { ExpressLogger } = require('@ismaelalves/logger').init({
PROJECT_NAME: 'project-name',
});
/** Request/Response Logger */
app.use(ExpressLogger.onSuccess.bind(ExpressLogger));
app.use(ExpressLogger.onError.bind(ExpressLogger));
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":298,"level":30,"msg":"{\"origin\":\"Express\",\"requestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"}}","time":"2019-09-10T00:49:04.394Z","v":0}
*/
const { AxiosLogger } = require('@ismaelalves/logger').init({
PROJECT_NAME: 'project-name',
});
const axiosInstance = require('axios').default.create({ ...config });
AxiosLogger.attachInterceptor.bind(AxiosLogger)(axiosInstance);
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Axios\",\"requestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/
const { RequestLogger } = require('@ismaelalves/logger').init({
PROJECT_NAME: 'project-name',
});
const request = require('request');
RequestLogger.attachDebug.bind(RequestLogger)(request);
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Request\",\"requestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/
import { init } from '@ismaelalves/logger';
const {
Logger,
} = init({
PROJECT_NAME: 'project-name',
});
Logger.info(JSON.stringify({
message: 'This is a cool message',
origin: 'Some Place',
status: 200,
}));
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":245,"level":30,"msg":"{\"message\":\"This is a cool message\",\"origin\":\"Some Place\",\"status\":200}","time":"2019-09-10T00:42:46.361Z","v":0}
*/
import { init } from '@ismaelalves/logger';
const {
ExpressLogger,
} = init({
PROJECT_NAME: 'project-name',
});
/** Request/Response Logger */
app.use(ExpressLogger.onSuccess.bind(ExpressLogger));
app.use(ExpressLogger.onError.bind(ExpressLogger));
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":298,"level":30,"msg":"{\"origin\":\"Express\",\"requestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"}}","time":"2019-09-10T00:49:04.394Z","v":0}
*/
import { init } from '@ismaelalves/logger';
import axios from 'axios';
const {
AxiosLogger,
} = init({
PROJECT_NAME: 'project-name',
});
const axiosInstance = axios.default.create({ ...config });
AxiosLogger.attachInterceptor.bind(AxiosLogger)(axiosInstance);
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Axios\",\"requestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/
import { init } from '@ismaelalves/logger';
import request from 'request';
const {
RequestLogger,
} = init({
PROJECT_NAME: 'project-name',
});
RequestLogger.attachDebug.bind(RequestLogger)(request);
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Request\",\"requestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/
import { init } from '@ismaelalves/logger';
const {
RequestLogger,
Redact,
} = init({
PROJECT_NAME: 'project-name',
});
RequestLogger.info(Redact.map({
'password': 'secret',
}));
RequestLogger.info(Redact.map({
'code': 'secret',
}));
Redact.addKey(/code/i);
RequestLogger.info(Redact.map({
'code': 'secret',
}));
import { init } from '@ismaelalves/logger';
const {
ExpressLogger,
} = init({
PROJECT_NAME: 'project-name',
});
/** Request/Response Logger */
app.use(ExpressLogger.onSuccess.bind(ExpressLogger));
app.use(ExpressLogger.onError.bind(ExpressLogger));
/** Change requestId */
app.use((req, res, next)=>{
req.headers["requestId"] = "new value";
next();
});
/*
This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":298,"level":30,"msg":"{\"origin\":\"Express\",\"requestId\":\"new value\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"}}","time":"2019-09-10T00:49:04.394Z","v":0}
*/