Formatted Logs

Is there a way to extend the existing logger within Sapphire? I saw that in the logger argument of the client constructor I can pass an instance of ILogger. So, I thought I could just extend the Logger class from @sapphire/framework and pass that to the instance argument. Doesn’t seem to work. It’s not formatting my logs. Seems to stick to the default logger. Are there any other options? I am kind of looking for a way to just work with the existing logger instead of creating an entirely new plugin. The only real change I am making is formatting log messages like so: [context]: message, where context shows where the log is coming from (I.e. MyCommand.chatInputRun`. And message is the normal log message. I could just pass a formatting function to the current logger. But this is a very error-prone solution. Thanks in advance.
Solution:
… Hmm. Alright then. Seems like an unnecessary amount of work just to change the format of how log messages are written. But guess I’ll have to do that.
Jump to solution
7 Replies
Je Suis Un Ami
Je Suis Un Ami8mo ago
Bump
Favna
Favna8mo ago
Look at how @sapphire/plugin-logger does it, or use that plugin outright.
Je Suis Un Ami
Je Suis Un Ami8mo ago
Yeah. I thought that's what I did. I extended the logger from the plugin.
import { Logger, LoggerOptions } from '@sapphire/plugin-logger';

/**
* TourneyMateLogger
*
* The tourneymate logger.
*/

export class TourneyMateLogger extends Logger implements ILogger {...}
import { Logger, LoggerOptions } from '@sapphire/plugin-logger';

/**
* TourneyMateLogger
*
* The tourneymate logger.
*/

export class TourneyMateLogger extends Logger implements ILogger {...}
And, in my custom client, I specified the the instance of the extended logger like so.
logger: {
level: LogLevel.Debug,
instance: new TourneyMateLogger({
level: LogLevel.Debug
})
},
logger: {
level: LogLevel.Debug,
instance: new TourneyMateLogger({
level: LogLevel.Debug
})
},
. Is there something I am supposed to do differently? What is happening is it is still using the old logger. Or, at least, it isn't showing my custom log formats. For reference, my custom log class looks like this.
export class TourneyMateLogger extends Logger implements ILogger {
constructor(options: LoggerOptions) {
super(options);
}

public override has(level: LogLevel): boolean {
return super.has(level);
}

public override debug(message: string, context: string | null = null): void {
super.debug(this.formatLogMessage(message, context));
}

public override error(message: string, context: string | null = null): void {
super.error(this.formatLogMessage(message, context));
}

public override fatal(message: string, context: string | null = null): void {
return super.fatal(this.formatLogMessage(message, context));
}

public override info(message: string, context: string | null = null): void {
super.info(this.formatLogMessage(message, context));
}

public override trace(message: string, context: string | null = null): void {
super.trace(this.formatLogMessage(message, context));
}

public override warn(message: string, context: string | null = null): void {
super.warn(this.formatLogMessage(message, context));
}

protected formatLogMessage(message: string, context: string | null = null): string {
if (context) {
return `[${context}]: ${message}`;
} else {
return message;
}
}
}
export class TourneyMateLogger extends Logger implements ILogger {
constructor(options: LoggerOptions) {
super(options);
}

public override has(level: LogLevel): boolean {
return super.has(level);
}

public override debug(message: string, context: string | null = null): void {
super.debug(this.formatLogMessage(message, context));
}

public override error(message: string, context: string | null = null): void {
super.error(this.formatLogMessage(message, context));
}

public override fatal(message: string, context: string | null = null): void {
return super.fatal(this.formatLogMessage(message, context));
}

public override info(message: string, context: string | null = null): void {
super.info(this.formatLogMessage(message, context));
}

public override trace(message: string, context: string | null = null): void {
super.trace(this.formatLogMessage(message, context));
}

public override warn(message: string, context: string | null = null): void {
super.warn(this.formatLogMessage(message, context));
}

protected formatLogMessage(message: string, context: string | null = null): string {
if (context) {
return `[${context}]: ${message}`;
} else {
return message;
}
}
}
All I am doing is formatting it to add a constext element to show where the log is coming from. However, log messages look like this:
024-03-12 16:22:21 - DEBUG - Checking for existing records. FirestoreGuildProfileResource.create
024-03-12 16:22:21 - DEBUG - Checking for existing records. FirestoreGuildProfileResource.create
Which isn't the format I spcified, making me think Sapphire isn't using my extension of the Logger plugin. Am I supposed to do something differently? Thought I could just extend the logger and keep using the existing plugin.
Favna
Favna8mo ago
Uh no the plugin will overwrite your custom logger class
Je Suis Un Ami
Je Suis Un Ami8mo ago
Oh. So, am I gonna have to write my own plugin? Or can I configure the plugin to use my custom format?
Favna
Favna8mo ago
the former
Solution
Je Suis Un Ami
Je Suis Un Ami8mo ago
… Hmm. Alright then. Seems like an unnecessary amount of work just to change the format of how log messages are written. But guess I’ll have to do that.
Want results from more Discord servers?
Add your server