@@ -8,8 +8,8 @@ export interface ExtendedPinoOptions extends LoggerOptions {
8
8
9
9
export interface LamdbaEvent {
10
10
headers ?: {
11
- [ key : string ] : string
12
- }
11
+ [ key : string ] : string ;
12
+ } ;
13
13
}
14
14
15
15
export type PinoLambdaLogger = BaseLogger & {
@@ -22,6 +22,8 @@ const CORRELATION_ID = `${CORRELATION_HEADER}id`;
22
22
const CORRELATION_TRACE_ID = `${ CORRELATION_HEADER } trace-id` ;
23
23
const CORRELATION_DEBUG = `${ CORRELATION_HEADER } debug` ;
24
24
25
+ const isLamdbaExecution = ( ) : boolean => ! ! process . env . AWS_EXECUTION_ENV ;
26
+
25
27
/**
26
28
* Custom destination stream for Pino
27
29
* @param options Pino options
@@ -32,6 +34,7 @@ const pinolambda = (
32
34
storageProvider : ContextStorageProvider ,
33
35
) : DestinationStream => ( {
34
36
write ( buffer : string ) {
37
+ const context = storageProvider . getContext ( ) || { } ;
35
38
if ( options . prettyPrint ) {
36
39
// prettyPrint buffer is not ndjson formatted
37
40
process . stdout . write ( buffer ) ;
@@ -42,7 +45,7 @@ const pinolambda = (
42
45
* This preserves the default log format of cloudwatch
43
46
*/
44
47
const { level, msg } = JSON . parse ( buffer ) ;
45
- const { awsRequestId } = storageProvider . getContext ( ) ;
48
+ const { awsRequestId } = context ;
46
49
const time = new Date ( ) . toISOString ( ) ;
47
50
let line = `${ time } \t${ awsRequestId } \t${ level . toUpperCase ( ) } \t${ msg } \t${ buffer } ` ;
48
51
line = line . replace ( / \n / , '\r' ) ;
@@ -58,6 +61,10 @@ const pinolambda = (
58
61
* that provides convinience methods for use with AWS Lambda
59
62
*/
60
63
export default ( extendedPinoOptions ?: ExtendedPinoOptions ) : PinoLambdaLogger => {
64
+ if ( ! isLamdbaExecution ) {
65
+ return ( pino ( extendedPinoOptions ) as unknown ) as PinoLambdaLogger ;
66
+ }
67
+
61
68
const storageProvider : ContextStorageProvider =
62
69
extendedPinoOptions ?. storageProvider || GlobalContextStorageProvider ;
63
70
0 commit comments