Skip to content

Commit

Permalink
squash merge feature-improved-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tgies committed Mar 23, 2013
1 parent ee43e8c commit 118d57a
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions ratpans.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
#!/usr/bin/env python

import sys
import time
from subprocess import call
import logging
import yaml

STARTTIME = time.localtime()

LOGTIMEFORMAT = "%Y%m%d %H:%M:%S"
CONSOLEFORMAT = '%(name)s: %(message)s'
LOGFILEFORMAT = '%(asctime)s %(name)s: [%(levelname)s] %(message)s'

TARSNAP = 'tarsnap'

print 'ratpans: aight'
# set up logger
log = logging.getLogger('ratpans')
log.setLevel(logging.INFO)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
consoleformatter = logging.Formatter(CONSOLEFORMAT, datefmt=LOGTIMEFORMAT)
console.setFormatter(consoleformatter)
log.addHandler(console)

log.info('aight')

with open('ratpans-config.yml', 'r') as c:
sc = c.read()

config = yaml.load(sc)
# TODO: validate config

if config['logfile'] and config['logfile'] != 'off':
try:
logfile = logging.FileHandler(config['logfile'])
logfile.setLevel(logging.ERROR)
logfileformatter = logging.Formatter(LOGFILEFORMAT, datefmt=LOGTIMEFORMAT)
logfile.setFormatter(logfileformatter)
log.addHandler(logfile)
except IOError as e:
log.error('error: cannot open logfile "{0}": {1} [errno {2}]'
.format(config['logfile'], e.strerror, e.errno))
else:
log.critical('started logging to "{0}" (started at {1})'
.format(config['logfile'], time.strftime(LOGTIMEFORMAT, STARTTIME)))

j = open('jobs.yml', 'r')
jobs = yaml.load_all(j)

Expand All @@ -31,7 +60,7 @@
else:
keyfile_option = []

print 'ratpans: Job ' + job['name'] + ' starting'
log.critical('Job ' + job['name'] + ' starting')

# TODO: builder class for tarsnap cmdln or class that just abstracts
# away calling tarsnap probably. REALLY need to be validating things
Expand All @@ -41,12 +70,13 @@
cmdln = [TARSNAP] + keyfile_option + ['-cf', job['name'] + '-' +
time.strftime('%Y%m%d')] + job['files']

print cmdln
log.debug(cmdln)
ret = call(cmdln)
if ret == 0:
print 'ratpans: Job ' + job['name'] + ' completed successfully'
log.critical('Job ' + job['name'] + ' completed successfully')
else:
print >> sys.stderr, 'ratpans: error: Job ' + job['name'] + \
' failed with code ' + str(ret)
log.error('Job ' + job['name'] + \
' failed with code ' + str(ret))

log.critical('done')
j.close()

0 comments on commit 118d57a

Please sign in to comment.