@bunito/logger
v0.0.27
Published
Logger module with injectable loggers, trace helpers, transports, and test logger utilities.
Maintainers
Readme
@bunito/logger
Logger module for bunito applications.
It provides injectable loggers, request-aware trace helpers, configurable log levels, and JSON or pretty output transports.
Installation 📦
bun add @bunito/loggerUsage ✨
import { Logger, LoggerModule } from '@bunito/logger';
import { Module, Provider } from '@bunito/container';
@Provider({
injects: [Logger],
})
class Worker {
constructor(private readonly logger: Logger) {}
run(): void {
this.logger.track().info('Worker started');
}
}
@Module({
imports: [LoggerModule],
providers: [Worker],
})
class AppModule {}Use Logger.track() when a log should include a fresh timestamp and duration
context while preserving the logger's existing context and prefix.
Testing 🧪
Importing @bunito/logger registers logger test factories on the shared
@bunito/testing Test context:
Test.LoggerModule: aLoggerModulereplacement exportingTestLoggerthrough the publicLoggertoken.Test.getLogger: a helper that returns theTestLoggerinstance for a given context.
import { App, Logger, Provider } from '@bunito/bunito';
import { Test } from '@bunito/testing';
@Provider({
injects: [Logger],
})
class Worker {
constructor(private readonly logger: Logger) {}
run(): void {
this.logger.info('started');
}
}
const app = await App.start({
imports: [Test.LoggerModule],
providers: [Worker],
});
const worker = await app.resolve(Worker);
worker.run();
expect(Test.getLogger(Worker).info).toBeCalledWith('started');TestLogger methods are Bun mocks for fatal, error, warn, info, ok,
verbose, debug, usePrefix, clone, and track.
License
MIT
