Skip to content

Custom Loggers

Danny edited this page Jun 2, 2021 · 1 revision

Custom Loggers

Custom loggers are a new part of the framework which allow you to define your own loggers so you can write to any output you want, they only need to confirm to a certain structure.

A logger must implement the ILogger interface and should contain the following functions:

export class MyCustomLogger implements ILogger {

    public constructor(allowedLevels?: Array<'info' | 'warn' | 'error' | 'verbose' | 'debug'>) {}

    public log(level: 'info' | 'warn' | 'error' | 'verbose' | 'debug', title: string, message: string, err?: Error): void {}

    public info(title: string, message: string): void {}

    public warn(title: string, message: string): void {}

    public error(title: string, message: string, err?: Error): void {}

    public verbose(title: string, message: string, err?: Error): void {}

    public debug(title: string, content: any, err?: Error): void {}
}

As you can see the log method is a generic catch all, and then you have each individual method as they are also used by the framework.

Using Custom Logger

To define a custom logger you must write your code and then import it into the main file you call the framework.

import { Framework } from 'rewyre';
import { MyCustomLogger } from './somewhere/logger';

(async () => {

    // Define the framework.
    const framework = new Framework({
        log_levels: ['info', 'warn', 'error', 'verbose', 'debug'], // These define which log levels should be logged.
        logger: MyCustomLogger,
    });
})();

The above will register and use your logger in replacement of the built in one.

For more details of implementation see the built in one:

src/modules/logger.ts

Useful Links:

Clone this wiki locally