Skip to content

Commit

Permalink
fix: correctly set the level tag
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot committed Jan 3, 2021
1 parent 069fcc2 commit 29826c9
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from 'aws-lambda';
import pino, { BaseLogger, DestinationStream, LoggerOptions } from 'pino';
import pino, { BaseLogger, DestinationStream, LoggerOptions, LevelMapping } from 'pino';
import { GlobalContextStorageProvider, ContextStorageProvider, ContextMap } from './context';

export interface ExtendedPinoOptions extends LoggerOptions {
Expand All @@ -13,9 +13,11 @@ interface LamdbaEvent {
[key: string]: any;
}

export type PinoLambdaLogger = BaseLogger & {
withRequest: (event: LamdbaEvent, context: Context) => void;
};
interface PinoLambdaExtensionOptions {
levels: LevelMapping;
options: ExtendedPinoOptions;
storageProvider: ContextStorageProvider;
}

const AMAZON_TRACE_ID = '_X_AMZN_TRACE_ID';
const CORRELATION_HEADER = 'x-correlation-';
Expand All @@ -24,16 +26,25 @@ const CORRELATION_TRACE_ID = `${CORRELATION_HEADER}trace-id`;
const CORRELATION_DEBUG = `${CORRELATION_HEADER}debug`;

const isLamdbaExecution = (): boolean => !!process.env.AWS_EXECUTION_ENV;
const formatLevel = (level: string | number, levels: LevelMapping): string => {
if (typeof level === 'string') {
return level.toLocaleUpperCase();
} else if (typeof level === 'number') {
return levels.labels[level]?.toLocaleUpperCase();
}
return level;
};

/**
* Custom destination stream for Pino
* @param options Pino options
* @param storageProvider Global storage provider for request values
*/
const pinolambda = (
options: ExtendedPinoOptions,
storageProvider: ContextStorageProvider,
): DestinationStream => ({
const pinolambda = ({
levels,
options,
storageProvider,
}: PinoLambdaExtensionOptions): DestinationStream => ({
write(buffer: string) {
if (options.prettyPrint) {
// prettyPrint buffer is not ndjson formatted
Expand All @@ -47,7 +58,8 @@ const pinolambda = (
const { level, msg } = JSON.parse(buffer);
const { awsRequestId } = storageProvider.getContext() || {};
const time = new Date().toISOString();
let line = `${time}\t${awsRequestId}\t${level.toUpperCase()}\t${msg}\t${buffer}`;
const levelTag = formatLevel(level, levels);
let line = `${time}\t${awsRequestId}\t${levelTag}\t${msg}\t${buffer}`;
line = line.replace(/\n/, '\r');
process.stdout.write(line + '\n');
}
Expand All @@ -56,6 +68,10 @@ const pinolambda = (
},
});

export type PinoLambdaLogger = BaseLogger & {
withRequest: (event: LamdbaEvent, context: Context) => void;
};

/**
* Exports a default constructor with an extended instance of Pino
* that provides convinience methods for use with AWS Lambda
Expand Down Expand Up @@ -85,7 +101,7 @@ export default (extendedPinoOptions?: ExtendedPinoOptions): PinoLambdaLogger =>
// construct a pino logger and set its destination
const logger = (pino(
pinoOptions,
pinolambda(pinoOptions, storageProvider),
pinolambda({ options: pinoOptions, storageProvider, levels: pino.levels }),
) as unknown) as PinoLambdaLogger;

// keep a reference to the original logger level
Expand Down

0 comments on commit 29826c9

Please sign in to comment.