nest-custom-logger
Provides logger and request middleware for NestJS and express application.
Output examples
Styled Example
Logs
Http logs
Json Example
Logs
Http logs
{"type":"http","context":"Request","message":"::ffff:192.168.88.107 - POST /auth/signin - 400 [Bad Request] (0b sent in 21 ms)","requestData":{"clientIp":"::ffff:192.168.88.107","method":"POST","originalUrl":"/auth/signin","statusCode":400,"statusMessage":"Bad Request","contentSize":0,"responseTime":21}}
Usage
Simple usage
import { Logger } from '@opengeekslab_llc/nest-custom-logger';
const logger = new (Logger(!process.env.NODE_ENV))('Hello');
logger.log('World')
Usage in classes
function CustomLogger(context: string) {
return new (Logger(!process.env.NODE_ENV))(context);
}
class Test {
logger = CustomLogger('Test');
run() {
this.logger.log('log');
this.logger.error('error');
}
}
Usage for not ts projects
const { Logger } = require('@opengeekslab_llc/nest-custom-logger');
const logger = new (Logger(!process.env.NODE_ENV))('Hello');
logger.log('World')
Usage of middleware for express
const express = require('express');
const { requestLoggerMiddleware } = require('@opengeekslab_llc/nest-custom-logger');
const app = express();
const requestWriteLogs = CustomLogger('Request');
const requestLogger = requestLoggerMiddleware({
writeLog: requestWriteLogs.http,
});
app.use(requestLogger);
Real usage
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CustomLogger, requestLogger } from '@opengeekslab_llc/nest-custom-logger';
const port = 8000;
async function bootstrap() {
const logger = CustomLogger('NEST');
const app = await NestFactory.create(AppModule, {
logger,
});
app.use(requestLogger);
await app.listen(port);
logger.log(`app is listening on port ${port}`);
}
bootstrap();