Skip to content

Commit

Permalink
Cleanup python logging
Browse files Browse the repository at this point in the history
In particular, use the logging library's native string interpolation
because it is more performant... it is only evaluated if the log line is
hit, versus building the string ourselves means the string is always
built, regardless of the log level.

Additionally, switched some `log.fatal()` to `log.exception()` so that
it picks up the exception details. This lowers the logging level by one
step, since `log.exception()` is level `ERROR` vs the rarely-used
`log.fatal()` is equal to level `CRITICAL`. But looking at the command
line args, the `quiet` option sets the logger to `WARNING` so both
`ERROR` / `CRITICAL` are always still hit.
  • Loading branch information
jeffwidman committed Jan 5, 2019
1 parent 4206dba commit 7d6f12e
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions tarsnapper/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, log, options, dryrun=False):
``options`` - options to pass to each tarsnap call
(a list of key value pairs).
In ``dryrun`` mode, will class will only pretend to make and/or
In ``dryrun`` mode, the class will only pretend to make and/or
delete backups. This is a global option rather than a method
specific one, because once the cached list of archives is tainted
with simulated data, you don't really want to run in non-dry mode.
Expand All @@ -65,7 +65,7 @@ def call(self, *arguments):
return self._exec_tarsnap(call_with)

def _exec_tarsnap(self, args):
self.log.debug("Executing: %s" % " ".join(args))
self.log.debug("Executing: %s", " ".join(args))
env = os.environ
child = pexpect.spawn(args[0], args[1:], env=env, timeout=None,
encoding='utf-8')
Expand Down Expand Up @@ -93,7 +93,7 @@ def _get_key_passphrase(self):

def _exec_util(self, cmdline, shell=False):
# TODO: can this be merged with _exec_tarsnap into something generic?
self.log.debug("Executing: %s" % cmdline)
self.log.debug("Executing: %s", cmdline)
p = subprocess.Popen(cmdline, shell=True)
p.communicate()
if p.returncode:
Expand Down Expand Up @@ -161,7 +161,7 @@ def get_backups(self, job):
# TODO: It'd take some work, but we could build a proper
# regex based on any given date format string, thus avoiding
# the issue for most cases.
self.log.error("Ignoring '%s': %s" % (backup_path, e))
self.log.exception("Ignoring '%s': %s", backup_path, e)
else:
backups[backup_path] = date

Expand All @@ -176,11 +176,11 @@ def expire(self, job):
"""

backups = self.get_backups(job)
self.log.info('%d backups are matching' % len(backups))
self.log.info('%d backups are matching', len(backups))

# Determine which backups we need to get rid of, which to keep
to_keep = expire.expire(backups, job.deltas)
self.log.info('%d of those can be deleted' % (len(backups)-len(to_keep)))
self.log.info('%d of those can be deleted', (len(backups)-len(to_keep)))

# Delete all others
to_delete = []
Expand All @@ -191,12 +191,12 @@ def expire(self, job):
to_keep.sort()
to_delete.sort()

self.log.debug('Keeping %s' % ' '.join(to_keep))
self.log.debug('Keeping %s', ' '.join(to_keep))

if len(to_delete) == 0:
return

self.log.info('Deleting %s' % ' '.join(to_delete))
self.log.info('Deleting %s', ' '.join(to_delete))

if not self.dryrun:
self.call('-d', '-f', *' -f '.join(to_delete).split(' '))
Expand All @@ -210,9 +210,9 @@ def make(self, job):
{'date': date_str, 'name': job.name})

if job.name:
self.log.info('Creating backup %s: %s' % (job.name, target))
self.log.info('Creating backup %s: %s', job.name, target)
else:
self.log.info('Creating backup: %s' % target)
self.log.info('Creating backup: %s', target)

if not self.dryrun:
args = ['-c']
Expand Down Expand Up @@ -280,7 +280,7 @@ class ListCommand(Command):
def run(self, job):
backups = self.backend.get_backups(job)

self.log.info('%s' % (job.name or "Unnamed job"))
self.log.info('%s', (job.name or "Unnamed job"))

# Sort backups by time
# TODO: This duplicates code from the expire module. Should
Expand All @@ -304,7 +304,7 @@ def setup_arg_parser(self, parser):

def expire(self, job):
if not job.deltas:
self.log.info(("Skipping '%s', does not define deltas") % job.name)
self.log.info("Skipping '%s', does not define deltas", job.name)
return

self.backend.expire(job)
Expand Down Expand Up @@ -345,7 +345,7 @@ def validate_args(self, args):

def run(self, job):
if not job.sources:
self.log.info(("Skipping '%s', does not define sources") % job.name)
self.log.info("Skipping '%s', does not define sources", job.name)
return

if job.exec_before:
Expand All @@ -369,8 +369,8 @@ def run(self, job):

if sources_missing:
if job.name:
self.log.info(("Not backing up '%s', because not all given "
"sources exist") % job.name)
self.log.info("Not backing up '%s', because not all given "
"sources exist", job.name)
else:
self.log.info("Not making backup, because not all given "
"sources exist")
Expand All @@ -379,8 +379,8 @@ def run(self, job):
try:
self.backend.make(job)
except Exception:
self.log.exception(("Something went wrong with backup job: '%s'")
% job.name)
self.log.exception("Something went wrong with backup job: '%s'",
job.name)

if job.exec_after:
self.backend._exec_util(job.exec_after)
Expand Down Expand Up @@ -510,8 +510,8 @@ def main(argv):
if args.config:
try:
jobs, global_config = config.load_config_from_file(args.config)
except config.ConfigError as e:
log.fatal('Error loading config file: %s' % e)
except config.ConfigError:
log.exception("Error loading config file:")
return 1
else:
# Only a single job, as given on the command line
Expand All @@ -523,7 +523,7 @@ def main(argv):
if args.jobs:
unknown = set(args.jobs) - set(jobs.keys())
if unknown:
log.fatal('Error: not defined in the config file: %s' % ", ".join(unknown))
log.error('Error: not defined in the config file: %s', ", ".join(unknown))
return 1
jobs_to_run = dict([(n, j) for n, j in jobs.items() if n in args.jobs])
else:
Expand All @@ -536,8 +536,8 @@ def main(argv):

for plugin in PLUGINS:
plugin.all_jobs_done(args, global_config, args.command)
except TarsnapError as e:
log.fatal("tarsnap execution failed:\n%s" % e)
except TarsnapError:
log.exception("tarsnap execution failed:")
return 1


Expand Down

0 comments on commit 7d6f12e

Please sign in to comment.