Skip to content

Commit

Permalink
feat(logger): extend meta by ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 17, 2024
1 parent a2ea5b1 commit ebaeded
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, Service } from '@cordisjs/core'
import { defineProperty } from 'cosmokit'
import Logger from 'reggol'

export { Logger }
Expand All @@ -9,13 +10,19 @@ declare module '@cordisjs/core' {
}
}

declare module 'reggol' {
namespace Logger {
interface Meta {
ctx?: Context
}
}
}

export interface LoggerService extends Pick<Logger, Logger.Type | 'extend'> {
(name: string): Logger
}

export class LoggerService extends Service {
static [Service.provide] = 'logger'

constructor(ctx: Context) {
super(ctx, 'logger', true)

Expand All @@ -33,14 +40,13 @@ export class LoggerService extends Service {
}

[Service.invoke](name: string) {
return new Logger(name, { [Context.origin]: this })
return new Logger(name, defineProperty({}, 'ctx', this.ctx))
}

static {
for (const type of ['success', 'error', 'info', 'warn', 'debug', 'extend'] as const) {
LoggerService.prototype[type] = function (this: any, ...args: any[]) {
const caller: Context = this[Context.origin]
return this(caller.name)[type](...args)
for (const type of ['success', 'error', 'info', 'warn', 'debug', 'extend']) {
LoggerService.prototype[type] = function (this: LoggerService, ...args: any[]) {
return this(this.ctx.name)[type](...args)
}
}
}
Expand Down

0 comments on commit ebaeded

Please sign in to comment.