Offers a log service with predefined severity levels. When called, broadcasts the message to all connected transports and based on their own configuration decide whether or not to publish at it.
Inject LogService
at your provide and call any of its method based on severity:
import { LogService } from '@bechara/crux';
@Injectable()
export class FooService {
public constructor(
private readonly fooRepository: Repository<FooEntity>,
private readonly logService: LogService,
) { }
public async readFooById(id: number) {
let foo: FooEntity;
this.logService.debug(`Reading foo with id ${id}`);
try {
foo = await this.FooRepository.readById(id);
}
catch (e) {
this.logService.error(`Failed to read foo`, e, id);
throw new InternalServerErrorException();
}
this.logService.notice(`Successfully read foo with id ${id}`);
return foo;
}
}
The logging method accepts multiples arguments of the following typing:
type LogArguments = string | Error | Record<string, any>;
Which means you may call them in any combination of:
this.logService.error(a: string);
this.logService.error(a: string, b: Error);
this.logService.error(a: string, b: Record<string, any>);
this.logService.error(a: Error, b: Record<string, any>);
this.logService.error(a: string, b: Record<string, any>, c: Record<string, any>);
this.logService.error(a: string, b: Error, c: Record<string, any>);
this.logService.error(a: Error, b: Record<string, any>, c: Record<string, any>);
this.logService.error(a: string, b: Error, c: Record<string, any>, d: Record<string, any>);
// etc...
This package offers the following built-in transporters: Console and Loki.
Configuration will be acquired from environment according to the following variables.
Print messages at stdout, enabled by default.
Variable | Required | Type | Default |
---|---|---|---|
CONSOLE_SEVERITY | No | string | trace when NODE_ENV=local , warning otherwise |
Publish logs to Loki by pushing through its API.
To enable this integration provide LOKI_URL
, you may also provide basic auth credentials.
Variable | Required | Type | Default |
---|---|---|---|
LOKI_URL | Yes | string | |
LOKI_USERNAME | No | string | |
LOKI_PASSWORD | No | string | |
LOKI_SEVERITY | No | string | debug |