Skip to content

Commit

Permalink
feat: improve sentry logger (#633)
Browse files Browse the repository at this point in the history
* feat: logger tags
* Update sentry.ts
* Update sentry.spec.ts
* feat: logger tags
* Update sentry.ts
* Update sentry.spec.ts
* Update pnpm-lock.yaml
* feat: update sentry.ts (#635)

Co-authored-by: Oleg-Tokar <[email protected]>
  • Loading branch information
AVVS and o-cappasity authored Aug 27, 2022
1 parent 7de177a commit d50fba9
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 204 deletions.
4 changes: 2 additions & 2 deletions packages/plugin-logger/src/logger/streams/sentry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Logger Sentry Stream Suite', () => {

const logger = pino({ level: 'debug' }, pinoms)

logger.warn({ userId: 123 }, 'Warning message')
logger.warn({ userId: 123, tags: { testTag: 'test' } }, 'Warning message')
logger.flush()

await Sentry.flush()
Expand All @@ -79,7 +79,7 @@ describe('Logger Sentry Stream Suite', () => {
_breadcrumbs: [],
_attachments: [],
_user: {},
_tags: {},
_tags: { testTag: 'test' },
_extra: {},
_contexts: {},
_sdkProcessingMetadata: {},
Expand Down
12 changes: 11 additions & 1 deletion packages/plugin-logger/src/logger/streams/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class ExtendedError extends Error {
}
}

const isObject = (obj: any): boolean => {
const type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}

export const pinoLevelToSentryLevel = (level: number): Sentry.SeverityLevel => {
if (level == 60) {
return "fatal"
Expand Down Expand Up @@ -65,9 +70,14 @@ export async function sentryTransport({ externalConfiguration, sentry, minLevel

return build(async function (source) {
for await (const obj of source) {
const level = obj.level
const { level, tags } = obj
const scope = new Sentry.Scope()
scope.setLevel(pinoLevelToSentryLevel(level))
if (isObject(tags)) {
for (const [tag, value] of Object.entries<string>(tags)) {
scope.setTag(tag, value)
}
}
if (level > minLevel) {
const stack = obj?.err?.stack
if (stack) {
Expand Down
Loading

0 comments on commit d50fba9

Please sign in to comment.