@temboplus/loggerlike
v1.0.0
Published
Standard interfaces for a logger component that can be implemented in different environments
Readme
@temboplus/loggerlike
A minimal TypeScript package that defines a standard logging interface for use across different environments. It provides no implementation — only the contracts that implementations must fulfill.
Installation
npm install @temboplus/loggerlikeInterfaces
LoggerLike
The core interface. Any logger you build or inject must implement this.
import { LoggerLike } from '@temboplus/loggerlike';
class MyLogger implements LoggerLike {
child(options: ChildOptions): LoggerLike { ... }
info(infoObject: InfoObject): void { ... }
debug(infoObject: InfoObject): void { ... }
warn(infoObject: InfoObject): void { ... }
error(infoObject: InfoObject): void { ... }
}InfoObject
The shape of every log entry. Requires a message field; any additional fields are allowed.
import { InfoObject } from '@temboplus/loggerlike';
const entry: InfoObject = {
message: 'User created',
userId: 42,
role: 'admin',
};ChildOptions
Passed to child() to create a scoped logger. Requires component and method; additional fields are allowed.
import { ChildOptions } from '@temboplus/loggerlike';
const options: ChildOptions = {
component: 'AuthService',
method: 'login',
};ConsoleLogger
A development-ready implementation included for convenience. It writes structured entries to the native console and supports child loggers for scoped output.
import { ConsoleLogger } from '@temboplus/loggerlike';
const logger = new ConsoleLogger();
const child = logger.child({ component: 'AuthService', method: 'login' });
child.info({ message: 'User authenticated', userId: 42 });
// => { component: 'AuthService', method: 'login', message: 'User authenticated', userId: 42 }
child.warn({ message: 'Token expiring soon', expiresIn: 300 });
child.error({ message: 'Login failed', reason: 'Invalid password' });Each log method maps to its native console equivalent:
| Method | Console call |
|---------|-----------------|
| info | console.log |
| debug | console.debug |
| warn | console.warn |
| error | console.error |
NullLogger
A no-operation implementation that silently discards all log entries. Useful when a LoggerLike must be supplied but logging should be suppressed entirely.
import { NullLogger } from '@temboplus/loggerlike';
const logger = new NullLogger();
logger.info({ message: 'This is silently discarded' });
const child = logger.child({ component: 'MyService', method: 'run' });
child.error({ message: 'This is also discarded' });Usage with dependency injection
The primary use case is typing a logger parameter against LoggerLike so any compliant implementation can be injected — including production loggers like Pino or Winston.
import { LoggerLike } from '@temboplus/loggerlike';
class UserService {
private logger: LoggerLike;
constructor(logger: LoggerLike) {
this.logger = logger.child({ component: 'UserService', method: 'constructor' });
}
create(userId: number) {
this.logger.info({ message: 'Creating user', userId });
}
}License
MIT
