Logger
Reciple uses a custom logger to print messages to the console. This logger is based on @prtty/print.
You can use the existing logger instance by calling useLogger(), it is available in the global scope. This instance is a singleton, so you can use it across multiple modules.
import { BaseModule } from "reciple";
class MyModule extends BaseModule {
onEnable() {
useLogger().log("MyModule is enabled!");
useLogger().warn("MyModule is enabled!");
useLogger().error("MyModule is enabled!");
}
} It is also possible to create your own logger instance by cloning the existing one.
const myLogger = useLogger().clone();
myLogger.log("Hello, world!"); The cloned logger instance will only be available in the current module.
You can also configure the cloned logger instance by passing an object to the
clonemethod. This object will be merged with the existing logger options.const myLogger = useLogger().clone({ label: 'MyModule' }); myLogger.log("Hello, world!");
Coniguration
To configure the global logger, you can add a new property to the exported config object in your reciple.config.js or reciple.config.ts file. You can set a custom logger instance that can be used globally.
// reciple.config.js
export const config = {
// ...
logger: useLogger().clone({ label: 'My Logger' })
// ...
}
Log Files
The logger can also be configured to log to a file. This can be useful if you want to preserve logs for debugging purposes.
import { FileWriteStreamMode, Utils } from "@prtty/print";
await useLogger().createFileWriteStream({
mode: FileWriteStreamMode.Rename,
filename: './logs/latest.log',
renameFile: Utils.gzipCompressLog
}); The createFileWriteStream method creates a new file write stream and returns the logger instance. This can be used in your configuration to make sure the global logger logs to a file.
// reciple.config.js
export const config = {
// ...
logger: await useLogger().createFileWriteStream({
mode: FileWriteStreamMode.Rename,
filename: './logs/latest.log',
renameFile: Utils.gzipCompressLog
})
// ...
} When a write stream is created, all clones of the logger will use the same write stream unless the clone is configured with a different write stream.