From 1efd00be910ce5a46d6642e8a451efcf78d1aeb6 Mon Sep 17 00:00:00 2001 From: Shigma Date: Mon, 5 Feb 2024 04:23:54 +0800 Subject: [PATCH] feat: add builtin logger --- package.json | 2 ++ packages/cordis/package.json | 9 ++++--- packages/cordis/src/index.ts | 1 - packages/cordis/src/logger.ts | 37 ++++++++++++++++++++++++++++ packages/cordis/src/worker/daemon.ts | 2 +- packages/cordis/src/worker/logger.ts | 6 +---- packages/hmr/src/error.ts | 2 +- packages/hmr/src/index.ts | 2 +- packages/loader/package.json | 3 +-- packages/loader/src/index.ts | 5 ++-- packages/loader/src/shared.ts | 7 +++--- 11 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 packages/cordis/src/logger.ts diff --git a/package.json b/package.json index 62a0ee3..cace5e7 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,11 @@ }, "devDependencies": { "@cordisjs/eslint-config": "^1.0.4", + "@sinonjs/fake-timers": "^6.0.1", "@types/chai": "^4.3.11", "@types/chai-as-promised": "^7.1.8", "@types/node": "^20.10.2", + "@types/sinonjs__fake-timers": "^6.0.4", "c8": "^7.14.0", "chai": "^4.3.10", "chai-as-promised": "^7.1.1", diff --git a/packages/cordis/package.json b/packages/cordis/package.json index d9d7d8b..1d1cdc6 100644 --- a/packages/cordis/package.json +++ b/packages/cordis/package.json @@ -9,6 +9,7 @@ "exports": { ".": "./lib/index.js", "./cli": "./lib/cli.js", + "./logger": "./lib/logger.js", "./worker": "./lib/worker/index.js", "./worker/main": "./lib/worker/main.js", "./src/*": "./src/*", @@ -38,11 +39,11 @@ "cli" ], "dependencies": { - "@cordisjs/core": "^3.8.0", - "@cordisjs/loader": "0.4.0", - "@cordisjs/logger": "^0.1.4", + "@cordisjs/core": "3.8.0", + "@cordisjs/loader": "^0.4.0", "cac": "^6.7.14", "cosmokit": "^1.5.2", - "kleur": "^4.1.5" + "kleur": "^4.1.5", + "reggol": "^1.6.3" } } diff --git a/packages/cordis/src/index.ts b/packages/cordis/src/index.ts index 7a3ece7..79372c8 100644 --- a/packages/cordis/src/index.ts +++ b/packages/cordis/src/index.ts @@ -1,2 +1 @@ export * from '@cordisjs/core' -export * from '@cordisjs/loader' diff --git a/packages/cordis/src/logger.ts b/packages/cordis/src/logger.ts new file mode 100644 index 0000000..344ed8b --- /dev/null +++ b/packages/cordis/src/logger.ts @@ -0,0 +1,37 @@ +import { Context } from '@cordisjs/core' +import Logger from 'reggol' + +export { Logger } + +declare module '@cordisjs/core' { + interface Context { + baseDir: string + logger: LoggerService + } +} + +interface LoggerService { + (name: string): Logger +} + +export function apply(ctx: Context) { + ctx.root.baseDir = globalThis.process?.cwd() || '' + + ctx.provide('logger', undefined, true) + + ctx.logger = function (name: string) { + return new Logger(name, { [Context.current]: this }) + } + + ctx.on('internal/info', function (format, ...args) { + this.logger('app').info(format, ...args) + }) + + ctx.on('internal/error', function (format, ...args) { + this.logger('app').error(format, ...args) + }) + + ctx.on('internal/warning', function (format, ...args) { + this.logger('app').warn(format, ...args) + }) +} diff --git a/packages/cordis/src/worker/daemon.ts b/packages/cordis/src/worker/daemon.ts index 8d5aeaf..25f88c8 100644 --- a/packages/cordis/src/worker/daemon.ts +++ b/packages/cordis/src/worker/daemon.ts @@ -15,7 +15,7 @@ export function apply(ctx: Context, config: Config = {}) { if (config.autoRestart) { process.send!({ type: 'exit' }) } - ctx.logger('app').info(`terminated by ${signal}`) + ctx.emit('internal/info', `terminated by ${signal}`) ctx.parallel('exit', signal).finally(() => process.exit()) } diff --git a/packages/cordis/src/worker/logger.ts b/packages/cordis/src/worker/logger.ts index 4405e0e..7a892bf 100644 --- a/packages/cordis/src/worker/logger.ts +++ b/packages/cordis/src/worker/logger.ts @@ -1,4 +1,4 @@ -import { apply as logger, Logger } from '@cordisjs/logger' +import { apply as logger, Logger } from '../logger.js' import { Context } from '@cordisjs/core' declare module '@cordisjs/loader' { @@ -39,10 +39,6 @@ export function apply(ctx: Context, config: Config = {}) { new Logger('app').warn(error) }) - ctx.on('loader/update', (type, entry) => { - new Logger('loader').info('%s plugin %c', type, entry.name) - }) - ctx.loader.prolog = [] Logger.targets.push({ diff --git a/packages/hmr/src/error.ts b/packages/hmr/src/error.ts index a27233e..d7a51a8 100644 --- a/packages/hmr/src/error.ts +++ b/packages/hmr/src/error.ts @@ -1,4 +1,4 @@ -import { Logger } from '@cordisjs/logger' +import { Logger } from 'cordis/logger' import { BuildFailure } from 'esbuild' import { codeFrameColumns } from '@babel/code-frame' import { readFileSync } from 'fs' diff --git a/packages/hmr/src/index.ts b/packages/hmr/src/index.ts index 36fff44..c5e986e 100644 --- a/packages/hmr/src/index.ts +++ b/packages/hmr/src/index.ts @@ -5,7 +5,7 @@ import Schema from 'schemastery' import { FSWatcher, watch, WatchOptions } from 'chokidar' import { relative, resolve } from 'path' import { handleError } from './error.js' -import { Logger } from '@cordisjs/logger' +import { Logger } from 'cordis/logger' import {} from '@cordisjs/timer' import { fileURLToPath, pathToFileURL } from 'url' diff --git a/packages/loader/package.json b/packages/loader/package.json index b6ed50e..1734595 100644 --- a/packages/loader/package.json +++ b/packages/loader/package.json @@ -37,8 +37,7 @@ "service" ], "devDependencies": { - "@cordisjs/core": "^3.8.0", - "@cordisjs/logger": "^0.1.4" + "@cordisjs/core": "^3.8.0" }, "peerDependencies": { "@cordisjs/core": "^3.8.0" diff --git a/packages/loader/src/index.ts b/packages/loader/src/index.ts index 9b36afc..5b54f59 100644 --- a/packages/loader/src/index.ts +++ b/packages/loader/src/index.ts @@ -1,4 +1,3 @@ -import {} from '@cordisjs/logger' import Loader from './shared.ts' import { promises as fs } from 'fs' import * as dotenv from 'dotenv' @@ -54,8 +53,8 @@ class NodeLoader extends Loader { fullReload(code = Loader.exitCode) { const body = JSON.stringify(this.envData) process.send?.({ type: 'shared', body }, (err: any) => { - if (err) this.app.logger('loader').error('failed to send shared data') - this.app.logger('loader').info('trigger full reload') + if (err) this.app.emit('internal/error', 'failed to send shared data') + this.app.emit('internal/info', 'trigger full reload') process.exit(code) }) } diff --git a/packages/loader/src/shared.ts b/packages/loader/src/shared.ts index f0aa308..4dc3d24 100644 --- a/packages/loader/src/shared.ts +++ b/packages/loader/src/shared.ts @@ -9,7 +9,6 @@ declare module '@cordisjs/core' { interface Events { 'config'(): void 'exit'(signal: NodeJS.Signals): Promise - 'loader/update'(this: Context, type: string, entry: Entry): void } interface Context { @@ -218,7 +217,7 @@ export abstract class Loader { state.fork.update(entry.config) } else { if (!this.isTruthyLike(entry.when)) return - parent.emit('loader/update', 'apply', entry) + parent.emit('internal/info', 'apply plugin %c', entry.name) const plugin = await this.resolve(entry.name) if (!plugin) return const ctx = parent.extend() @@ -234,7 +233,7 @@ export abstract class Loader { unload(parent: Context, entry: Entry) { const state = this.states[entry.id] if (state?.fork) { - parent.emit('loader/update', 'unload', entry) + parent.emit('internal/info', 'unload plugin %c', entry.name) state.fork.dispose() } } @@ -264,7 +263,7 @@ export abstract class Loader { this.app.on('internal/update', (fork) => { const state = this.states[fork.id!] if (!state) return - fork.parent.emit('loader/update', 'reload', state.entry) + fork.parent.emit('internal/info', 'reload plugin %c', state.entry.name) }) this.app.on('internal/before-update', (fork, config) => {