Logger
When trying to log information to the console, using the default console.log()
will not contain the current time stamp of the log and its context. It will also not be written to the log file making tracing previous error difficult.
Reciple uses a custom logger that is built on top of
Console
. We used the logger fromprtyprnt
to provide more custimization and to have similar API to the defaultconsole.log()
.
You can use the logger from the .logger
property of the client that is passed from the onStart
, onLoad
, and onUnload
data.
export class MyModule {
async onStart({ client }) {
client.logger?.log(`Module is started`);
return true;
}
async onLoad({ client }) {
client.logger?.log(`Module is loaded`);
return true;
}
async onUnload({ client }) {
client.logger?.log(`Module is unloaded`);
}
}
?.
operator because the .logger
property could be undefined depending on your logger configuration.Creating Logger Context
Creating your own logger context will allow you to differentiate between different log messages and easily locate where the logs are coming from. This example is going to use the logger’s .clone()
method.
export class MyModule {
logger = null;
async onStart({ client }) {
// Clone the logger into the MyModule#logger property
this.logger = client.logger?.clone({ label: 'MyModule' });
this.logger?.log(`Module is started`);
return true;
}
async onLoad({ client }) {
this.logger?.log(`Module is loaded`);
return true;
}
async onUnload({ client }) {
this.logger?.log(`Module is unloaded`);
}
}
The module will now have its own logger, which will print the context name to the console and log file.
[19:53:36][MyModule/INFO] Module is started
[19:53:36][MyModule/INFO] Module is loaded
[19:53:36][MyModule/INFO] Module is unloaded