Repository: borgbackup/borg

Issue:
Add --error to only output log messages of error or higher
Back in 2015, the following was added, but refactored out prior to the 1.0.0 release:

https://github.com/borgbackup/borg/commit/25140e8c8218fe19ef9ffc8029e3f53516b2d546#diff-dfaa777fd33eac4e20768ac811e52696R649

That gave the ability to specify the log level for all levels (debug, info, warning, error, critical).  The current default seems to be to only output "warning" or higher, but there is no way to tell borg that you only want to see "error" or higher.

Adding "--error" would allow me to suppress warning messages in the output.



Discussion:
There's the BORG_LOGGING_CONF environment variable, which uses these: https://docs.python.org/3/library/logging.config.html

Usually applications have `-q`, `--quiet` for this.

To suppress warnings? Borg doesn't generate many of those, and the few around ought to be noticed/fixed.

quiet is the default, but it does not include warnings and errors.

and I am rather tending to remove -v / --verbose than to add -q / --quiet (because it seems to trigger expectations about the way it works that are not like it really works).

that said, adding --error might be an option if somebody prefers to not have some specific (and expected) warnings show up. @tgharold had the specific issue that a `borg check` cron job always emails him that "orphan chunks check skipped" warning because he only checked the last 5 archives, not all.

this could be fixed in 3 ways (to stop the spamming):
- add --error so one can decide to not see warnings (which might have bad side effects in case some important warning passes unnoticed)
- change log level of that message to info (orphan chunks are no big problem, the just waste space, but do not cause malfunction)
- kill that message completely

```
logger.warning('Orphaned objects check skipped (needs all archives checked).')
```

This should be on INFO level imho, because it's expected from the command given to Borg.

@enkore agreed, that's a good reason.


Resolving patch:
diff --git a/borg/archiver.py b/borg/archiver.py
--- a/borg/archiver.py
+++ b/borg/archiver.py
@@ -818,12 +818,21 @@ def preprocess_args(self, args):
 
     def build_parser(self, args=None, prog=None):
         common_parser = argparse.ArgumentParser(add_help=False, prog=prog)
+        common_parser.add_argument('--critical', dest='log_level',
+                                   action='store_const', const='critical', default='warning',
+                                   help='work on log level CRITICAL')
+        common_parser.add_argument('--error', dest='log_level',
+                                   action='store_const', const='error', default='warning',
+                                   help='work on log level ERROR')
+        common_parser.add_argument('--warning', dest='log_level',
+                                   action='store_const', const='warning', default='warning',
+                                   help='work on log level WARNING (default)')
         common_parser.add_argument('-v', '--verbose', '--info', dest='log_level',
                                    action='store_const', const='info', default='warning',
-                                   help='enable informative (verbose) output, work on log level INFO')
+                                   help='work on log level INFO')
         common_parser.add_argument('--debug', dest='log_level',
                                    action='store_const', const='debug', default='warning',
-                                   help='enable debug output, work on log level DEBUG')
+                                   help='work on log level DEBUG')
         common_parser.add_argument('--lock-wait', dest='lock_wait', type=int, metavar='N', default=1,
                                    help='wait for the lock, but max. N seconds (default: %(default)d).')
         common_parser.add_argument('--show-rc', dest='show_rc', action='store_true', default=False,
diff --git a/borg/remote.py b/borg/remote.py
--- a/borg/remote.py
+++ b/borg/remote.py
@@ -195,6 +195,10 @@ def borg_cmd(self, args, testing):
                 opts.append('--info')
             elif root_logger.isEnabledFor(logging.WARNING):
                 pass  # warning is default
+            elif root_logger.isEnabledFor(logging.ERROR):
+                opts.append('--error')
+            elif root_logger.isEnabledFor(logging.CRITICAL):
+                opts.append('--critical')
             else:
                 raise ValueError('log level missing, fix this code')
         if testing:


Test patch: