-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JSON logger options + log spans
- Loading branch information
Showing
8 changed files
with
132 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"effect-log": minor | ||
--- | ||
|
||
Add options for JSON logger. Add support for log spans. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,34 +1,62 @@ | ||
import { List } from "effect"; | ||
import * as FiberId from "effect/FiberId"; | ||
import * as HashMap from "effect/HashMap"; | ||
import * as Layer from "effect/Layer"; | ||
import * as Logger from "effect/Logger"; | ||
|
||
import { serializeUnknown } from "effect-log/internal"; | ||
|
||
export const make = (messageField?: string) => | ||
Logger.make(({ fiberId, logLevel, message, annotations, cause, date }) => { | ||
const tags: Record<string, unknown> = HashMap.reduce( | ||
annotations, | ||
{}, | ||
(acc, v, k) => ({ | ||
...acc, | ||
[k]: v, | ||
}), | ||
); | ||
|
||
tags["date"] = date; | ||
tags["logLevel"] = logLevel.label; | ||
tags[messageField ?? "message"] = serializeUnknown(message); | ||
tags["fiberId"] = FiberId.threadName(fiberId); | ||
|
||
if (cause._tag !== "Empty") { | ||
tags["cause"] = cause; | ||
} | ||
|
||
console.log(JSON.stringify(tags)); | ||
}); | ||
export interface Options { | ||
showFiberId: boolean; | ||
showTime: boolean; | ||
showSpans: boolean; | ||
messageField: string; | ||
} | ||
|
||
const defaultOptions: Options = { | ||
showFiberId: true, | ||
showTime: true, | ||
showSpans: true, | ||
messageField: "message", | ||
}; | ||
|
||
export const make = (options?: Partial<Options>) => | ||
Logger.make( | ||
({ fiberId, logLevel, message, annotations, cause, date, spans }) => { | ||
const _options = { ...defaultOptions, ...options }; | ||
|
||
const tags: Record<string, unknown> = HashMap.reduce( | ||
annotations, | ||
{}, | ||
(acc, v, k) => ({ | ||
...acc, | ||
[k]: v, | ||
}), | ||
); | ||
|
||
if (_options.showTime) { | ||
tags["date"] = date; | ||
} | ||
tags["logLevel"] = logLevel.label; | ||
tags[_options.messageField] = serializeUnknown(message); | ||
|
||
if (_options.showFiberId) { | ||
tags["fiberId"] = FiberId.threadName(fiberId); | ||
} | ||
|
||
if (_options.showSpans && List.isCons(spans)) { | ||
tags["spans"] = List.toArray(spans).map((span) => span.label); | ||
} | ||
|
||
if (cause._tag !== "Empty") { | ||
tags["cause"] = cause; | ||
} | ||
|
||
console.log(JSON.stringify(tags)); | ||
}, | ||
); | ||
|
||
export const layer: ( | ||
messageFields?: string, | ||
) => Layer.Layer<never, never, never> = (messageField) => | ||
Logger.replace(Logger.defaultLogger, make(messageField)); | ||
options?: Partial<Options>, | ||
) => Layer.Layer<never, never, never> = (options) => | ||
Logger.replace(Logger.defaultLogger, make(options ?? {})); |
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