@fengcart/logger v1.17.0
@fengcart/logger
New Shared logging library for CTG
Installation
yarn add @fengcart/logger
Usage
The default logger will cover almost all use cases.
import log from '@fengcart/logger';
function hello() {
log.info({ data: 'Some data' }, 'A log message');
}
Output
2018-12-20T17:05:25.330Z INFO A log message
{
"level": 30,
"message": "Some A log message",
"data": "Some data"
}
Lambda Functions
For usage in a Lambda function, you can capture request information which will append the request and tracing IDs to all log statements. This functionality is provided by pino-lamdba.
See the default tags that are tracked in Lambda functions.
The usage of withRequest
is global, that is, you do not need to pass instances of logger around
to other classes or functions. Calling withRequest
once will track tags in ALL logger instances
across the application, regardless of the method you use to import or create the logger.
import log from '@fengcart/logger';
export async function handler(event, context) {
log.withRequest(event, context);
log.info({ data: 'Some data' }, 'A log message');
}
Output
2018-12-20T17:05:25.330Z 6fccb00e-0479-11e9-af91-d7ab5c8fe19e INFO A log message
{
"awsRequestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e",
"x-correlation-id": "238da608-0542-11e9-8eb2-f2801f1b9fd1",
"x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1",
"level": 30,
"message": "Some A log message",
"data": "Some data"
}
Default Tags
By default, the following tags are tracked in all log messages
Property | Value | Info |
---|---|---|
awsRequestId | context.awsRequestId | The unique request id for this request |
x-correlation-id | event.headers'x-correlation-id' | The upstream request id for tracing |
x-correlation-trace-debug | event.headers'x-correlation-debug' | The upstream service wants debug logs enabled for this request |
x-correlation-trace-id | process.env.X_AMZN_TRACE_ID | The AWS tracing id |
x-correlation-* | event.headers.startsWith('x-correlation-') | Any header that starts with x-correlation- will be automatically added |
name | process.env.AWS_LAMBDA_FUNCTION_NAME | The lambda function name |
stage | process.env.STAGE or process.env.NODE_ENV | Application Stage |
brand | process.env.BRAND | RBI Brand |
region | process.env.REGION | RBI Region/Country Code |
These tags are compatible with what Datadog expects and AWS specific metrics will be ignored in non-aws environments.
Configuration
Log level output is controlled by environment variables set to fatal, error, warn, info (default), debug, trace, or silent
LOG_LEVEL=debug
Log level can also be controlled by upstream requests. If an incoming request contains a header of
x-correlation-debug
set to true
, the logger will be set to debug
level for that request.
In rare cases where the logger needs to be customized further, we will support all options that the underlying
logging provider itself supports for configuration (by default pino
).
Local Development
Local development provides a more readable output using pino-pretty
by setting the LOG_PRETTY
environment variable to true
;
Child Loggers
You can create named child loggers which will output to add an additional tags to all messages.
import logger from '@fengcart/logger';
const childLogger = logger.child({
module: 'Foo',
// any additional tags
});
childLogger.info({ data: 'Some data' }, 'A log message');
Output
2018-12-20T17:05:25.330Z 6fccb00e-0479-11e9-af91-d7ab5c8fe19e INFO A log message
{
"awsRequestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e",
"x-correlation-id": "238da608-0542-11e9-8eb2-f2801f1b9fd1",
"x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1",
"level": 30,
"message": "Some A log message",
"data": "Some data",
"module": "Foo"
}
Custom Loggers
// logger.ts
import { create } from '@fengcart/logger';
export default create({
// any pino options
});
// handler.ts
import log from './logger';
export async function handler(event, context) {
log.withRequest(event, context);
log.info({ data: 'Some data' }, 'A log message');
}
NestJS
The ApiLogger
class implements a simple NestJS LoggerService
that logs with the RBI logger instance.
import { ApiLogger } from '@fengcart/logger';
const app = await NestFactory.create(AppModule, { logger: new ApiLogger() });