Skip to content

Commit 4bb04d2

Browse files
committed
Guard when running in non-lambda environments
1 parent 057faac commit 4bb04d2

File tree

4 files changed

+2053
-24
lines changed

4 files changed

+2053
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
coverage
4+
.nyc_output

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lint": "eslint --ext .ts ./src",
1616
"prebuild": "yarn clean",
1717
"pretest:e2e": "yarn clean && yarn build",
18-
"test:unit": "jest",
18+
"test:unit": "tap",
1919
"test:e2e": "node ./dist/test.js"
2020
},
2121
"keywords": [],
@@ -28,6 +28,7 @@
2828
"@types/aws-lambda": "^8.10.68",
2929
"@types/node": "12",
3030
"@types/pino": "^6.0.0",
31+
"@types/tap": "^14.10.1",
3132
"@typescript-eslint/eslint-plugin": "^4.11.0",
3233
"@typescript-eslint/parser": "^4.11.0",
3334
"eslint": "^7.16.0",
@@ -38,6 +39,7 @@
3839
"pino-pretty": "^4.0.0",
3940
"prettier": "^2.2.1",
4041
"rimraf": "^3.0.2",
42+
"tap": "^14.11.0",
4143
"typescript": "^4.1.3"
4244
},
4345
"peerDependencies": {

src/index.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export interface ExtendedPinoOptions extends LoggerOptions {
88

99
export interface LamdbaEvent {
1010
headers?: {
11-
[key: string]: string
12-
}
11+
[key: string]: string;
12+
};
1313
}
1414

1515
export type PinoLambdaLogger = BaseLogger & {
@@ -22,6 +22,8 @@ const CORRELATION_ID = `${CORRELATION_HEADER}id`;
2222
const CORRELATION_TRACE_ID = `${CORRELATION_HEADER}trace-id`;
2323
const CORRELATION_DEBUG = `${CORRELATION_HEADER}debug`;
2424

25+
const isLamdbaExecution = (): boolean => !!process.env.AWS_EXECUTION_ENV;
26+
2527
/**
2628
* Custom destination stream for Pino
2729
* @param options Pino options
@@ -32,6 +34,7 @@ const pinolambda = (
3234
storageProvider: ContextStorageProvider,
3335
): DestinationStream => ({
3436
write(buffer: string) {
37+
const context = storageProvider.getContext() || {};
3538
if (options.prettyPrint) {
3639
// prettyPrint buffer is not ndjson formatted
3740
process.stdout.write(buffer);
@@ -42,7 +45,7 @@ const pinolambda = (
4245
* This preserves the default log format of cloudwatch
4346
*/
4447
const { level, msg } = JSON.parse(buffer);
45-
const { awsRequestId } = storageProvider.getContext();
48+
const { awsRequestId } = context;
4649
const time = new Date().toISOString();
4750
let line = `${time}\t${awsRequestId}\t${level.toUpperCase()}\t${msg}\t${buffer}`;
4851
line = line.replace(/\n/, '\r');
@@ -58,6 +61,10 @@ const pinolambda = (
5861
* that provides convinience methods for use with AWS Lambda
5962
*/
6063
export default (extendedPinoOptions?: ExtendedPinoOptions): PinoLambdaLogger => {
64+
if (!isLamdbaExecution) {
65+
return (pino(extendedPinoOptions) as unknown) as PinoLambdaLogger;
66+
}
67+
6168
const storageProvider: ContextStorageProvider =
6269
extendedPinoOptions?.storageProvider || GlobalContextStorageProvider;
6370

0 commit comments

Comments
 (0)