Configuration and logging
@navios/server ships two small services most applications need from the first day: a typed configuration service and a logger.
Configuration
provideConfig creates an injection token for a ConfigService whose content comes from a load function. Declare it once and inject it wherever a setting is needed.
loadmay be asynchronous. It runs once per container, the first time the token is resolved.schemais optional. With it, the loaded value is validated and the service is typed from the schema's output. It can be any Standard Schema and must validate synchronously. Without it, the service is typed from the return type ofload.- A configuration that does not match fails with the path and the message of every issue, such as
[Navios] Invalid configuration AppConfig: port: Invalid input: expected number, received string, and without the values: the message of an issue is left out when the value, or a part of it, occurs in it.AppConfigis thenameoption; without it the message saysConfig. - Keys are dot-separated paths and are type-checked:
'database.url'compiles,'database.uri'does not. Optional sections have paths as well.
Reading values
Environment variables
envString(key, default?) and envInt(key, default) read single variables, as in load above. A variable that is missing or empty gives the default. envInt throws when the variable is not an integer, naming it: the default would hide a typo such as PORT=80O0.
EnvConfigProvider is a ready-made token for a ConfigService over a snapshot of process.env, taken when a container first resolves it. It suits a small application that does not need a schema:
Configuration in tests
A config token is an ordinary injection token, so a test replaces it with a provider: provide(AppConfig, { value: new ConfigService({ port: 0, database: { url: 'test', poolSize: 1 } }) }). See Replacing dependencies.
Logging
Inject a Logger with a context name. The name appears with every message the instance writes.
The methods are log, error, warn, debug, verbose and fatal. Pass an error after the message, logger.error('Payment failed', error): the console logger prints its stack and its causes.
Choosing the output
Where the output goes is decided by the application's logger option.
The levels are verbose, debug, log, warn, error and fatal. The console logger uses colours when stdout is a terminal. With json: true an error is on the line of its message, as error: { name, message, stack, cause } with the fields of its own, such as code or status, and the errors of an AggregateError.
Without the option, the output is the provider of LoggerOutput, the console logger by default. A module or a plugin can provide another: provide(LoggerOutput, PinoOutput). A list of levels applies to that one too when it has setLogLevels, as every ConsoleLogger does. What plugins and transports log before every plugin is set up is written after that, in order; when the start-up fails before, it goes to the output that can be had at once, else to the console.
A custom logger
A LoggerService is any object with log, error and warn methods; debug, verbose and fatal are optional. This is the place to connect pino, winston or a hosted log service.
Every Logger injected anywhere in the application writes to this object, with the context name appended as the last argument: logger.log('hello') from the service above calls jsonLines.log('hello', 'SignupService'). Navios's own messages, such as the log entry for an unexpected error, go to it too.
Request ids
The console logger prints the id of the request being handled with each message when the application was created with asyncContext: true. The id is ctx.id. requestId: false in the options of a console logger you create turns it off.