-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support lambda structured logging format (#51)
- Loading branch information
1 parent
7f32ac6
commit 6722990
Showing
7 changed files
with
95 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pino from 'pino'; | ||
|
||
export const formatLevel = (level: string | number): string => { | ||
if (typeof level === 'string') { | ||
return level.toLocaleUpperCase(); | ||
} else if (typeof level === 'number') { | ||
return pino.levels.labels[level]?.toLocaleUpperCase(); | ||
} | ||
return level; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './lambda'; | ||
export * from './pino'; | ||
export * from './structured'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ILogFormatter, LogData } from '../types'; | ||
import { formatLevel } from './format'; | ||
|
||
/** | ||
* Formats the log in structured JSON format while | ||
* including the Lambda context data automatically | ||
* @see https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions | ||
*/ | ||
export class StructuredLogFormatter implements ILogFormatter { | ||
format({ awsRequestId, level, ...data }: LogData): string { | ||
return JSON.stringify({ | ||
timestamp: new Date().toISOString(), | ||
level: formatLevel(level), | ||
requestId: awsRequestId, | ||
message: data, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters