marcelropos / HM-DiscordBot

A Discordbot by and for first semester students of HM.
8 stars 5 forks source link

Setup logging outputs and formatting #140

Closed maxwai closed 10 months ago

maxwai commented 12 months ago

Configure tracing so that all logs are send to the console, to a file in ./appdata/logs/.

The file should have log rotation if possible (keep X files at a maximum of Y MB).

As for formatting of the console and file messages (will be the same) try something like:

YYYY-MM-DD HH-mm-SS.sss Level module-name message

If there are more characteristics that could be good to log, add them. Everything except the message should be limited in length and padded to length so that, if you look at the logs, everything looks linear.

Additionally, make a subscriber that can be used to send the log to a discord channel. At creation, the subscriber gets a Bot Context, a guild id and a channel id. He also gets a flag if only log messages intended for his guild should be send or all. The subscriber can figure out from what guild a message is by reading the optional argument guild_id. For the formatting of the log messages to discord, just the message and Level is fine.

schitcrafter commented 10 months ago

For the formatting of console and log file, would https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/format/struct.Compact.html be fine? The source location of the message isn't padded, but I think it should be good

maxwai commented 10 months ago

For the formatting of console and log file, would https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/format/struct.Compact.html be fine? The source location of the message isn't padded, but I think it should be good

Yes that looks fine

schitcrafter commented 10 months ago

Is log rotation based on file sized a fixed requirement? The tracing_appender crate (part of the tracing ecosystem) only has support for time-based log rotation (i.e. hourly, daily, etc.), and while there exists another crate for this (rolling_file), it's not part of tracing itself, and I'm not sure about how active it is.

maxwai commented 10 months ago

Is log rotation based on file sized a fixed requirement? The tracing_appender crate (part of the tracing ecosystem) only has support for time-based log rotation (i.e. hourly, daily, etc.), and while there exists another crate for this (rolling_file), it's not part of tracing itself, and I'm not sure about how active it is.

I think time based rotation should be fine. If we set it to 7 days

schitcrafter commented 10 months ago

I should mention, I've ended up just using size-based rotation based on rolling_file - both tracing_appender and rolling_file only have support for time-based log-rotation every minute hour and day, but no week/custom duration option.