⚠️ This library is deprecated please use DotCom logger instead ⚠️
Logger useful for AWS lambda applications, particularly those which are aggregated in Splunk. Logs in JSON format using pino.
This was created to provide a simple logger, compatible with lambda, which outputs in a JSON format (n-logger) was previously used but didn't handle nested JSON fields or provide a JSON option).
This does make process.stdout.write
a blocking function (process.stdout._handle.setBlocking(true);
), as AWS Lambda previously streamed to an output which was synchronous, but has since changed to asynchronous behaviour, leading to lost logs.
const logger = require('@financial-times/lambda-logger');
logger.info({ importantField: 'some-field' }, 'Logging a thing');
This module exports both
- a commonjs build (the
main
field inpackage.json
) - an ESM (ecmascript module) build (the
module
field inpackage.json
)
If you're using commonjs and webpack, say with serverless-webpack it will try to load the ESM build out of the box. This exports a default
export, and as such won't work if using commonjs.
The solutions to this problem are:
- Use
import/export
syntax locally and ensure your local tooling uses the ESM build, e.g. by using the esm module. - Setup a webpack alias to the commonjs build:
// webpack.config.js
module.exports = {
...
resolve: {
alias: {
// use commonjs export of lambda-logger to avoid having to use import/export syntax locally
'@financial-times/lambda-logger':
'@financial-times/lambda-logger/dist/lambda-logger.js',
},
},
};
The logger's API is identical to that of pino with the following exceptions:
- The property
sourcetype: _json
is added to logs in production for Splunk compatibility. - Lambda related environment variables are added by default:
-
AWS_REGION
AWS_EXECUTION_ENV
,AWS_LAMBDA_FUNCTION_NAME
,AWS_LAMBDA_FUNCTION_MEMORY_SIZE
,AWS_LAMBDA_FUNCTION_VERSION
- Defaults to ISO timestamp logging for splunk compatiblity. At the time of writing this incurs a 25% pino performance penalty.
Pino adds the following properties to logs by default:
level
- the log level in string form. This is translated from thepino
default of logging an integer representation.v
- the pino logger API version.hostname
- the hostname the process is running on.pid
- the process PID.
NODE_ENV
- pretty printing is enabled when value ofNODE_ENV
is not same asp
,prod
orproduction
.CONSOLE_LOG_LEVEL
- determines the level to log at (pintolevel
option). Defaults toinfo
.SYSTEM_CODE
- adds thesystemCode
property to every log.ENVIRONMENT|STAGE
- adds theenvironment
property to every log.STAGE
is used as a fallback due to it's default definition in the serverless framework.