EWL (express-winston-logger) provides common defaults for logging in an express application using winston. It is pre-configured with request tracing enabled and extends some of the features provided by express-winston, e.g. body blacklisting.
yarn add ewl
import { Ewl } from 'ewl';
const ewl = new Ewl();
ewl.debug('This logs to console by default.');
index.ts
:
import { Application } from 'express';
import { ewl, initEwl } from './logger';
const app = express() as Application;
initEwl(app);
const port = 3000;
app.listen(port, () => {
ewl.debug(`App: Listening on port ${port}!`);
});
logger/index.ts
:
import { Application } from 'express';
import { Ewl, LogLevel } from 'ewl';
export let ewl: Ewl;
export function initEwl(app: Application): void {
ewl = new Ewl({
environment: process.env.ENVIRONMENT || 'development',
label: 'app',
logLevel: (process.env.LOG_LEVEL as LogLevel) || 'error',
useLogstashFormat: false,
version: process.env.VERSION || 'local',
});
// Use the context middleware for request id injection
app.use(ewl.contextMiddleware);
// Use express-winston for logging request information
app.use(
ewl.createHandler({
bodyBlacklist: ['accessToken', 'password', 'refreshToken'],
colorize: true,
expressFormat: false,
headerBlacklist: ['cookie', 'token'],
ignoreRoute: () => false,
meta: true,
metaField: 'express',
msg: 'HTTP {{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}',
requestWhitelist: [
'headers',
'method',
'httpVersion',
'originalUrl',
'query',
'params',
'url',
],
responseWhitelist: ['headers', 'statusCode'],
statusLevels: true,
}),
);
}
/middleware/logger.middleware.ts
:
import { NextFunction, Request, Response } from 'express';
import { ewl } from '../logger';
export function loggerMiddleware(req: Request, _: Response, next: NextFunction): void {
ewl.debug(`${req.method} ${req.path}`);
next();
}
index.ts
/ main.ts
:
import { NestFactory } from '@nestjs/core';
import { Ewl, LogLevel } from 'ewl';
import { AppModule } from './app.module';
async function bootstrap() {
const ewl = new Ewl({
environment: process.env.ENVIRONMENT || 'development',
label: 'app',
logLevel: (process.env.LOG_LEVEL as LogLevel) || 'error',
useLogstashFormat: false,
version: process.env.VERSION || 'local',
});
// Set the default NestJS logger, allowing EWL to be the proxy.
const app = await NestFactory.create(AppModule, { logger: ewl });
// Use the context middleware for request id injection
app.use(ewl.contextMiddleware);
// Use express-winston for logging request information
app.use(
ewl.createHandler({
bodyBlacklist: ['accessToken', 'password', 'refreshToken'],
colorize: true,
expressFormat: false,
headerBlacklist: ['cookie', 'token'],
ignoreRoute: () => false,
meta: true,
metaField: 'express',
msg: 'HTTP {{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}',
requestWhitelist: [
'headers',
'method',
'httpVersion',
'originalUrl',
'query',
'params',
'url',
],
responseWhitelist: ['headers', 'statusCode'],
statusLevels: true,
}),
);
ewl.debug('Starting application on localhost:3000');
await app.listen(3000, 'localhost');
}
bootstrap();
MIT License
Contributions are encouraged, please see further details below:
Here are some basic rules to follow to ensure timely addition of your request:
- Match coding style (braces, spacing, etc.).
- If it is a feature, bugfix, or anything please only change the minimum amount of code required to satisfy the change.
- Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge.
- Pull requests must be made against the
main
branch. Any other branch (unless specified by the maintainers) will get rejected. - Check for existing issues first, before filing a new issue.