@truxie/logger
v0.1.2
Published
Structured logging for truxie — an injectable Logger over a pluggable transport
Readme
@truxie/logger
Structured logging for truxie: an injectable Logger over a transport you can replace.
npm install @truxie/logger@Module({imports: [LoggerModule.forRoot({level: 'info'})]})
export class AppModule {}
@Injectable()
class UserService {
constructor(private readonly logger: Logger) {}
create(email: string) {
this.logger.child('UserService').info('Created user', {email})
}
}The transport is a port
Logger is injected with LOG_TRANSPORT and knows nothing else — not that a console transport exists, and not how to build one. Shipping logs somewhere else is a provider change:
LoggerModule.forRoot({transport: new JsonStdoutTransport()})
// A transport with its own dependencies
LoggerModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
level: config.get('LOG_LEVEL'),
transport: new HttpTransport(config.get('LOG_ENDPOINT')),
}),
})LogTransport is one method:
interface LogTransport {
log(entry: LogEntry): void
}LogEntry carries level, message, timestamp, and optional context and data — enough to render a line or emit JSON, and nothing that assumes either.
Levels and context
debug < info < warn < error. Anything below the configured level is dropped before it reaches the transport, so a filtered-out call costs almost nothing.
logger.child('UserService') returns a logger that tags every entry with that context and inherits the parent's transport and level. Use it per class rather than threading a context argument through every call.
Options
| Option | Default | |
|---|---|---|
| transport | ConsoleTransport | Any LogTransport. |
| level | 'info' | Minimum level that reaches the transport. |
| timestamps | true | Include timestamps. |
