Passing Custom Loggers to Different Layers in Effect Typescript
If the
And use it with a util function:
But when I tried to inject into layer's implementation in this way:
LoggerLogger can passed into the deeper layer dependency?I want to make the Effect.logEffect.log's output into different log file at different layers with my custom layer like this:const stashedLogger = (filePath: string = './logs/default.log') => Logger.make(
({ logLevel, message, date }) => {
if (logLevel === LogLevel.Info) {
globalThis.console.log(`[${date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', fractionalSecondDigits: 3, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' })}] INFO | ${message}`)
fs.appendFileSync(filePath, `[${date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', fractionalSecondDigits: 3, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' })}] INFO | ${message}\n`, { encoding: 'utf-8' })
return
}
}
)const stashedLogger = (filePath: string = './logs/default.log') => Logger.make(
({ logLevel, message, date }) => {
if (logLevel === LogLevel.Info) {
globalThis.console.log(`[${date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', fractionalSecondDigits: 3, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' })}] INFO | ${message}`)
fs.appendFileSync(filePath, `[${date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', fractionalSecondDigits: 3, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' })}] INFO | ${message}\n`, { encoding: 'utf-8' })
return
}
}
)And use it with a util function:
export const getLoggerLayer = (path: LoggerPath | string) => Logger.replace(Logger.defaultLogger, stashedLogger(path))export const getLoggerLayer = (path: LoggerPath | string) => Logger.replace(Logger.defaultLogger, stashedLogger(path))But when I tried to inject into layer's implementation in this way:
export const crawlerListServiceWithCustomLoggerLive = Layer.provide(
crawlerListServiceLive,
getLoggerLayer('./logs/crawler-list.log')
)
export const WechatCrawlerServiceRunnable = Effect.provide(
Effect.gen(function*(){
const crawlerService = yield* WechatCrawlerService
yield* crawlerService.startCrawler
}),
WechatCrawlerServiceLive.pipe(
Layer.provide(crawlerListServiceWithCustomLoggerLive),
//...other layers's implementation
Layer.provide(getLoggerLayer('./logs/wechat-crawler.log'))
)
)export const crawlerListServiceWithCustomLoggerLive = Layer.provide(
crawlerListServiceLive,
getLoggerLayer('./logs/crawler-list.log')
)
export const WechatCrawlerServiceRunnable = Effect.provide(
Effect.gen(function*(){
const crawlerService = yield* WechatCrawlerService
yield* crawlerService.startCrawler
}),
WechatCrawlerServiceLive.pipe(
Layer.provide(crawlerListServiceWithCustomLoggerLive),
//...other layers's implementation
Layer.provide(getLoggerLayer('./logs/wechat-crawler.log'))
)
)