Skip to content

Commit

Permalink
refa: refactor logger to standalone plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 21, 2024
1 parent f214ee4 commit ab9d527
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 68 deletions.
2 changes: 0 additions & 2 deletions packages/cordis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
"dependencies": {
"@cordisjs/core": "4.0.0-alpha.0",
"@cordisjs/loader": "^1.0.0-alpha.0",
"@cordisjs/logger": "^1.0.0-alpha.0",
"@cordisjs/schema": "^1.0.0-alpha.0",
"cac": "^6.7.14",
"cosmokit": "^1.6.3",
"kleur": "^4.1.5",
Expand Down
36 changes: 0 additions & 36 deletions packages/cordis/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@
import * as core from '@cordisjs/core'
import { LoggerService } from '@cordisjs/logger'
import { SchemaService } from '@cordisjs/schema'

export * from '@cordisjs/core'
export { Schema, z } from '@cordisjs/schema'
export { Logger } from '@cordisjs/logger'

export interface Events<C extends Context = Context> extends core.Events<C> {}

export interface Context {
[Context.events]: Events<this>
}

export class Context extends core.Context {
baseDir: string

constructor() {
super()
this.baseDir = globalThis.process?.cwd?.() || ''

this.provide('logger', undefined, true)

this.plugin(LoggerService)
}
}

export abstract class Service<C extends Context = Context> extends core.Service<C> {
public schema: SchemaService

constructor(ctx: C, name: string) {
super(ctx, name)
this.schema = new SchemaService(this.ctx)
}
}

export default function () {}
25 changes: 10 additions & 15 deletions packages/cordis/src/worker/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from '@cordisjs/logger'
import Logger from 'reggol'
import { Context } from '../index.ts'

declare module '@cordisjs/loader' {
Expand All @@ -20,7 +20,7 @@ export interface Config {
showTime?: string | boolean
}

export const inject = ['loader']
// export const inject = ['loader']

export function apply(ctx: Context, config: Config = {}) {
function handleException(error: any) {
Expand All @@ -34,20 +34,15 @@ export function apply(ctx: Context, config: Config = {}) {
new Logger('app').warn(error)
})

ctx.on('loader/entry-scope', (entry, type) => {
if (entry.options.group) return
ctx.logger('loader').info('%s plugin %c', type, entry.options.name)
})

ctx.loader.prolog = []
// ctx.loader.prolog = []

Logger.targets.push({
colors: 3,
record: (record) => {
ctx.loader.prolog.push(record)
ctx.loader.prolog = ctx.loader.prolog.slice(-1000)
},
})
// Logger.targets.push({
// colors: 3,
// record: (record) => {
// ctx.loader.prolog.push(record)
// ctx.loader.prolog = ctx.loader.prolog.slice(-1000)
// },
// })

const { levels } = config
// configurate logger levels
Expand Down
13 changes: 7 additions & 6 deletions packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { ModuleJob, ModuleLoader } from 'cordis/loader'
import { FSWatcher, watch, WatchOptions } from 'chokidar'
import { relative, resolve } from 'node:path'
import { handleError } from './error.ts'
import {} from '@cordisjs/plugin-logger'
import {} from '@cordisjs/plugin-timer'
import { fileURLToPath, pathToFileURL } from 'node:url'
import enUS from './locales/en-US.yml'
import zhCN from './locales/zh-CN.yml'

declare module 'cordis' {
interface Context {
hmr: Watcher
hmr: HMR
}

interface Events {
Expand All @@ -36,8 +37,8 @@ interface Reload {
runtime?: Plugin.Runtime
}

class Watcher extends Service {
static inject = ['loader', 'timer']
class HMR extends Service {
static inject = ['loader', 'timer', 'logger']

private base: string
private internal: ModuleLoader
Expand Down Expand Up @@ -69,7 +70,7 @@ class Watcher extends Service {
/** stashed changes */
private stashed = new Set<string>()

constructor(ctx: Context, public config: Watcher.Config) {
constructor(ctx: Context, public config: HMR.Config) {
super(ctx, 'hmr')
if (!this.ctx.loader.internal) {
throw new Error('--expose-internals is required for HMR service')
Expand Down Expand Up @@ -322,7 +323,7 @@ class Watcher extends Service {
}
}

namespace Watcher {
namespace HMR {
export interface Config extends WatchOptions {
base?: string
root: string[]
Expand Down Expand Up @@ -351,4 +352,4 @@ namespace Watcher {
})
}

export default Watcher
export default HMR
2 changes: 1 addition & 1 deletion packages/loader/src/config/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class Entry<C extends Context = Context> {
const plugin = this.loader.unwrapExports(exports)
this.patch()
this.scope = this.ctx.plugin(plugin, this._resolveConfig(plugin))
this.context.emit('loader/entry-scope', this, 'apply')
this.loader.showLog(this, 'apply')
this._initTask = undefined
}
}
9 changes: 7 additions & 2 deletions packages/loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export abstract class Loader<C extends Context = Context> extends ImportTree<C>

ctx.on('internal/update', (scope, config) => {
if (!scope.entry) return
scope.parent.emit('loader/entry-scope', scope.entry, 'reload')
this.showLog(scope.entry, 'reload')
}, { global: true })

ctx.on('internal/update', (scope, config) => {
Expand Down Expand Up @@ -99,7 +99,7 @@ export abstract class Loader<C extends Context = Context> extends ImportTree<C>
// plugin hmr: delete(plugin) -> runtime dispose -> scope dispose
if (!ctx.registry.has(scope.runtime?.plugin!)) return

scope.parent.emit('loader/entry-scope', scope.entry, 'unload')
this.showLog(scope.entry, 'unload')

// case 4: scope is disposed by loader behavior
// such as inject checker, config file update, ancestor group disable
Expand All @@ -113,6 +113,11 @@ export abstract class Loader<C extends Context = Context> extends ImportTree<C>
ctx.plugin(isolate)
}

showLog(entry: Entry, type: string) {
if (entry.options.group) return
this.ctx.get('logger')?.('loader').info('%s plugin %c', type, entry.options.name)
}

locate(ctx = this.ctx) {
let scope = ctx.scope
while (1) {
Expand Down
13 changes: 10 additions & 3 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@cordisjs/logger",
"name": "@cordisjs/plugin-logger",
"description": "Logger service for cordis",
"version": "1.0.0-alpha.0",
"type": "module",
Expand Down Expand Up @@ -33,11 +33,18 @@
"service",
"plugin"
],
"cordis": {
"service": {
"implements": [
"logger"
]
}
},
"devDependencies": {
"@cordisjs/core": "^4.0.0-alpha.0"
"cordis": "^4.0.0-alpha.0"
},
"peerDependencies": {
"@cordisjs/core": "^4.0.0-alpha.0"
"cordis": "^4.0.0-alpha.0"
},
"dependencies": {
"cosmokit": "^1.6.3",
Expand Down
23 changes: 20 additions & 3 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Context, Service } from '@cordisjs/core'
import { Context, Service } from 'cordis'
import { defineProperty, hyphenate } from 'cosmokit'
import Logger from 'reggol'

export { Logger }

declare module '@cordisjs/core' {
declare module 'cordis' {
interface Context {
logger: LoggerService
}

interface Intercept {
logger: LoggerService.Config
}
}

declare module 'reggol' {
Expand All @@ -18,6 +22,12 @@ declare module 'reggol' {
}
}

export namespace LoggerService {
export interface Config {
name?: string
}
}

export interface LoggerService extends Pick<Logger, Logger.Type | 'extend'> {
(name: string): Logger
}
Expand Down Expand Up @@ -46,7 +56,14 @@ export class LoggerService extends Service {
static {
for (const type of ['success', 'error', 'info', 'warn', 'debug', 'extend']) {
LoggerService.prototype[type] = function (this: LoggerService, ...args: any[]) {
return this(hyphenate(this.ctx.name))[type](...args)
let config: LoggerService.Config = {}
let intercept = this.ctx[Context.intercept]
while (intercept) {
config = Object.assign({}, intercept.logger, config)
intercept = Object.getPrototypeOf(intercept)
}
const name = config.name || hyphenate(this.ctx.name)
return this(name)[type](...args)
}
}
}
Expand Down

0 comments on commit ab9d527

Please sign in to comment.