@communitylife/cl-logger
v1.0.6
Published
Logging middleware to provide request tracking and logging functions
Downloads
79
Keywords
Readme
cl-logger
Logging middleware to provide request tracking and logging functions
Prerequisites
- Node.JS (>=v.8.15.0)
Installation
npm install @communitylife/cl-logger
Usage
const express = require('express');
const app = express();
const logger = require('@communitylife/cl-logger');
// once imported, all unhandled rejections, as well as uncaught exceptions are automatically been tracked
//this will provide an endpoint to get information about the system and the running process
app.get('/health', logger.HealthMiddleware);
// this will track every request
app.use(logger.RequestMiddleware);
// you can also track only specific requests
app.get('/user', logger.RequestMiddleware, getUser);
/**
* You can also manually log messages with optional details
* available methods: debug, info, warn, error
*/
logger.Logger.info(new logger.Log('Some info message', {optional: {info: 'details'}}));
// You can also write to custom levels
logger.Logger.log(new logger.Log('A special level'), 'special');
All the logs are stored inside logs/logs.log
.
If the log file exceeds a limit, it will create a new log file, while the old one will be copied inside logs folder under a different name.
The logs have a predefined format:
{"level":"INFO","message":"Some info","timestamp":"2019-07-05T13:15:14.226Z","details":"{\"foo\":[{\"key\":\"bar\"}]}"}
Default log (details optional, custom format)
{"level":"REQUEST","message":"POST /","timestamp":"2019-07-05T13:44:54.727Z","details":"{\"req\":{\"hostname\":\"localhost\",\"body\":{\"id\":\"id\"},\"params\":{},\"query\":{},\"started\":1562334294719},\"res\":{\"time\":0.008,\"statusCode\":200}}"}
Request log from middleware (details are automatically attached and extracted from Request and Response