New option for specific logfile #233
No reviewers
Labels
No Label
bug
data loss
design finalized
good first issue
new feature area
question / support
security
waiting for response
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: borgmatic-collective/borgmatic#233
Loading…
Reference in New Issue
No description provided.
Delete Branch "palto42/borgmatic:logging"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Instead of logging to console/stdout or syslog I wanted to write the log to a specific file.
For this purpose I added an option
--log-file
which accepts a /path/filename string.If this option is specified, it disables the logging to console and syslog. It also uses a specific logging format, assuming that the logfile is dedicated for borgmatic.
Thank you for submitting this! This sounds like a great addition. A few of questions/comments:
Could you talk a little more about your motivation behind this feature? Is it that syslog just isn't meeting your needs? Don't want your borgmatic output mixed in with other logs? Something else? And what's your thinking behind the suppression of the console logging? I want to make sure I capture the reasoning in the documentation, and also so that I make sure I understand the choices here!
When a log file is given, instead of suppressing/replacing console output entirely, what do you think of leaving the console and syslog handlers in place and adding a separate log file handler that doesn't affect the other handlers? And maybe supporting an independent log level just for the log file handler, as is done for syslog? My reasoning here is that a user may want to vary the log file log level independent of the console log level, and it seems more consistent with the existing console and syslog handler (which have independent log levels).
Would it be possible to write a test for the new parameter? You can check out the existing
test_configure_logging_*()
tests for examples.Feel free to add yourself to the AUTHORS file if you like!
My motivation for this new feature is to have a dedicated log file for the backups and don't mix it with syslog messages. For other non-system services there are typically also separate log file (e.g. Nginx, MariaDB).
If I specify a dedicated log file, I have no need for console of syslog output and want to avoid duplication. If a backup job with specified logfile is running, I can always follow the output with a
tail -f /path/to/logfile
if needed. In my view it would add unnecessary complexity to keep all 3 log options in parallel, but this is just my personal view/use case.I can see some benefit to keep the console output in case of an interactive session (if users don't want to use
tail
). For concurrent syslog and logfile I don't see a real use case, but maybe I'm missing one.What's not clear to me is the reason for keeping the console handler in case of a non-interactive session. If I run borgmatic from cron or systemd, then I usually don't want to send anything to stdout. As I was trying bormatic on the console, I even struggled a with output redirection via
>file 2>&1
where borgmatic still sent all messages to the console instead of the redircet file.Will have a look at the code for a feature test later (after we agreed on the feature details).
Got it, thanks. That makes sense. I'm guessing that a filtered syslog view isn't sufficient for that? E.g., with systemd, something like
journalctl -xe --unit borgmatic
?The main use case I can see for console output for a non-interactive session (with or without logs also going to a file) is to see error messages from a cron/systemd job. For instance, even with console
--verbosity 0
(the default), you'll get console error messages directly in your inbox if you're running it with cron. Seems like a pretty common use case, especially for those users who aren't using some of the fancier monitoring options.Here are a few ways I can think of to reduce the complexity a bit of having all three log options:
Yeah, shell redirection is kind of obnoxious especially once stderr is added to the mix.
Cool!
Agree with your proposal to replace the syslog handler instead of console handler. But if a logfile is specified, it should also be used in an interactive session.
Thanks for explaining the rationale to keep the console handler for non-interactive session. Need some more testing to see if I still want to disable it in cron (or higher level like ERROR or CRITICAL).
On more question on the syslog-verbosity:
if syslog_log_level is None:
be true?Yup, that makes sense to me.
syslog_log_level
can beNone
whenconfigure_logging()
is called during an early error condition. For instance, look forconfigure_logging(logging.CRITICAL)
inborgmatic/commands/borgmatic.py
. The rationale is that something has gone wrong even before the arguments have been successfully parsed, and we want to quickly configure logging so we can explode loudly (in a nice way).For a normal situation when arguments are parsed successfully, that is correct. It'll only be
None
if something went wrong parsing arguments.Oh, one more question: For your uses, do you think this log file value makes more sense as a command-line flag or a configuration file option? It occurred to me that you might not want to type it every time.
I updated the
logger.py
code so that the file-logger replaces the syslog handler and not the console output. I intend to look into some test code the next days.The command line flag is sufficient for my use case, just one more flag in addition to the verbosity.
Another topic I might evaluate is some "tuning" of the log output. With verbosity 0 I get nothing (except errors) and with verbosity 1 I get a bit too much detail (e.g. all individual files). What I would like to see in my log is just some basic info like tasks executed (prune, create, checks).
Code changes look good. Thank you!
This may not be quite what you're looking for, but the
--stats
flag with the default borgmatic verbosity of 0 will give you some basic summary information, without all the individual files play-by-play.Thanks for your hint with the
--stats
option. Main issue with this approach is that it will send the stats to both syslog and console so that it would cause an email with cron. But I will look into this topic separately from this PR.I added some exception handling for the logfile handler and 2 test cases.
Should I add any additional test cases?
Looks great! Thank you for adding tests.
One thing I did is move the error handling (path not found, permissions) up a level to the calling function. That way it's a little more consistent with some of the other types of errors that can occur.
I also added a separate
--log-file-verbosity
to reduce confusion.. Setting--syslog-verbosity
to influence a non-syslog log file might be a little counter-intuitive!Anyway, I just released this as part of borgmatic 1.4.5. Thanks again!
Oh, and docs here: https://torsion.org/borgmatic/docs/how-to/inspect-your-backups/#logging