调试配置(name、color / style、timeFlag 等),详见 DebugConfiguration
type AppSettings = DebugSettings & { http?: boolean; ws?: boolean }
// biome-ignore lint/suspicious/noUnsafeDeclarationMerging: implTraits guarantees runtime implementation
class AppService {}
implTraits(AppService, debuggable<AppSettings>({
name: 'AppService',
color: '#4dabf7', // 仅颜色值,自动包装为 color: #4dabf7
// style: 'color: #4dabf7; font-weight: bold', // 或传完整 CSS
timeFlag: true,
}))
// biome-ignore lint/correctness/noUnusedVariables: trait type extension via implTraits
interface AppService extends DebuggableTrait<AppSettings> {}
const svc = new AppService()
svc.setDebug({ debug: false, http: true })
svc.debug('http', 'request sent', { url: '/api/users' }) // 输出
svc.debug('ws', 'connected') // 静默
创建一个
DebuggableTrait实现对象,配合implTraits混入到目标类。config在工厂调用时固定,之后通过原型共享给所有实例(只读)。 运行期的调试开关(debugSettings)通过setDebug()写到各实例的自有属性上, 互不干扰。