improve log typings

This commit is contained in:
Ben Allfree 2024-09-24 15:26:41 +00:00
parent 6be202ba26
commit 7f0ba21af5
2 changed files with 16 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
'pockethost': patch
---
Internal refactorings

View File

@ -123,20 +123,24 @@ export const createLogger = (config: Partial<LoggerConfig>) => {
throw new Error(`Fatal error: ${stringify(args)}`)
}
const create = (s: string, configOverride?: Partial<LoggerConfig>) =>
const create = (name: string, configOverride?: Partial<LoggerConfig>) =>
createLogger({
..._config,
...configOverride,
pfx: [..._config.pfx, s],
pfx: [..._config.pfx, name],
})
const breadcrumb = (s: string) => {
pfx.push(s)
const breadcrumb = (s: string | object) => {
if (typeof s === 'string') {
pfx.push(s)
} else {
Object.entries(s).forEach(([k, v]) => pfx.push(`${k}: ${v}`))
}
return api
}
// Compatibility func
const child = (extra: any) => create(stringify(extra))
const child = (name: string) => create(name)
const api = {
raw,
@ -159,6 +163,8 @@ export const createLogger = (config: Partial<LoggerConfig>) => {
return api
}
export type LoggerServiceApi = ReturnType<typeof createLogger>
export const LoggerService = mkSingleton((config: Partial<LoggerConfig> = {}) =>
createLogger(config),
)