forked from hellocoop/client-as
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from TiltingPoint/feat/logging/PLATFORM-3549
[PLATFORM-3549] Add structured logging
- Loading branch information
Showing
4 changed files
with
68 additions
and
16 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
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,47 @@ | ||
const LOG_LEVEL = process.env.LOG_LEVEL || "info"; | ||
|
||
interface LogAttributes { | ||
[key: string]: string | number | boolean | unknown | undefined | Error; | ||
} | ||
|
||
export function log(msg: string, level: "debug" | "info" | "warn" | "error", attributes?: LogAttributes) { | ||
if (LOG_LEVEL === "info" && level === "debug") { | ||
return; | ||
} | ||
|
||
if (LOG_LEVEL === "warn" && ["debug", "info"].includes(level)) { | ||
return; | ||
} | ||
|
||
if (LOG_LEVEL === "error" && level !== "error") { | ||
return; | ||
} | ||
|
||
const message = { msg, level, ...attributes }; | ||
//eslint-disable-next-line no-console | ||
console.log(JSON.stringify(message)); | ||
} | ||
|
||
export function debug(msg: string, attributes?: LogAttributes) { | ||
log(msg, "debug", attributes); | ||
} | ||
|
||
export function info(msg: string, attributes?: LogAttributes) { | ||
log(msg, "info", attributes); | ||
} | ||
|
||
export function warn(msg: string, attributes?: LogAttributes) { | ||
log(msg, "warn", attributes); | ||
} | ||
|
||
export function error(msg: string, attributes?: LogAttributes) { | ||
log(msg, "error", attributes); | ||
} | ||
|
||
export function formatError(error: Error | unknown): { error: string } { | ||
if (!(error instanceof Error)) { | ||
return { error: JSON.stringify({message: 'unable to format error'}) }; | ||
} | ||
const { message, stack, name } = error; | ||
return { error: JSON.stringify({ message, stack: stack?.replaceAll('\n', ';'), name }) }; | ||
} |
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