0.1.14 • Published 4 years ago
@lcdev/logger v0.1.14
Launchcode Logger
A consistent, structured logger for services. This package is a thin wrapper over winston.
The logger that's used and created is literally just winston. We just have a couple utilities here for the common use cases.
Quick Start
yarn add @lcdev/logger@0.1
import * as Koa from 'koa';
import {
getLogger,
createLogger,
createLoggerMiddleware,
} from '@lcdev/logger';
// the lifecycle of the logger is simple. create it once, use anywhere with `getLogger`.
createLogger({
// optional - default to NODE_ENV = 'development'
debug: true,
// optional - default to NODE_ENV = 'development'
outputStackTraces: true,
// optional - can spawn different loggers with different settings
name: 'non-default',
// optional - default true
stdout: true,
// optional - insert logs into a file
logfile: './logs.txt',
});
const createApp = () => {
const app = new Koa();
const logger = createLogger();
// this will log all request/responses on your service
app.use(createLoggerMiddleware(logger));
// use the logger anywhere you'd like, specifying the level (again, this is just winston)
logger.info('created app!');
return app;
};
Note that you'll need to initialize the global logger before calling getLogger
, which
will return undefined. If using typescript, getLogger()!.info('hello')
is not an
antipattern, provided that your program guarantees it's initialization beforehand.
Output
Output will be pretty printed in development (debug) and plain JSON otherwise.
Most important is that we track timestamps, duration, status, and other metadata. It should be easy to parse these later from production logs especially, since it's all raw JSON.