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:Jump to 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.
7 Replies
Bump
Look at how @sapphire/plugin-logger does it, or use that plugin outright.
Yeah. I thought that's what I did. I extended the logger from the plugin. And, in my custom client, I specified the the instance of the extended logger like so. . 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.
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:
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.
Uh no the plugin will overwrite your custom logger class
Oh. So, am I gonna have to write my own plugin? Or can I configure the plugin to use my custom format?
the former
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.