mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 15:36:27 +00:00
refactor logging setup
This commit is contained in:
parent
88ebeadd25
commit
b701bd028b
2 changed files with 63 additions and 60 deletions
60
base/log.py
Normal file
60
base/log.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
def get_logfile_path(manifest_path):
|
||||||
|
import sys
|
||||||
|
import os.path
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
manifest_basename = os.path.basename(manifest_path)
|
||||||
|
manifest_name, _ = os.path.splitext(manifest_basename)
|
||||||
|
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
||||||
|
filename = "{timestamp}_{name}.log".format(timestamp=timestamp, name=manifest_name)
|
||||||
|
return os.path.normpath(os.path.join(os.path.dirname(__file__), '../logs', filename))
|
||||||
|
|
||||||
|
def setup_logger(logfile=None, debug=False):
|
||||||
|
root = logging.getLogger()
|
||||||
|
root.setLevel(logging.NOTSET)
|
||||||
|
|
||||||
|
file_handler = logging.FileHandler(logfile)
|
||||||
|
file_handler.setFormatter(FileFormatter('[%(asctime)s] %(message)s'))
|
||||||
|
file_handler.setLevel(logging.DEBUG)
|
||||||
|
root.addHandler(file_handler)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
console_handler = logging.StreamHandler(sys.stderr)
|
||||||
|
console_handler.setFormatter(ConsoleFormatter())
|
||||||
|
if debug:
|
||||||
|
console_handler.setLevel(logging.DEBUG)
|
||||||
|
else:
|
||||||
|
console_handler.setLevel(logging.INFO)
|
||||||
|
root.addHandler(console_handler)
|
||||||
|
|
||||||
|
|
||||||
|
class ConsoleFormatter(logging.Formatter):
|
||||||
|
def format(self, record):
|
||||||
|
from task import Task
|
||||||
|
if(isinstance(record.msg, Task)):
|
||||||
|
task = record.msg
|
||||||
|
if(task.description is not None):
|
||||||
|
return '\033[0;34m{description}\033[0m'.format(description=task.description)
|
||||||
|
else:
|
||||||
|
return '\033[0;34mRunning {task}\033[0m'.format(task=task)
|
||||||
|
return super(ConsoleFormatter, self).format(record)
|
||||||
|
|
||||||
|
|
||||||
|
class FileFormatter(logging.Formatter):
|
||||||
|
def format(self, record):
|
||||||
|
from task import Task
|
||||||
|
from datetime import datetime
|
||||||
|
if(isinstance(record.msg, Task)):
|
||||||
|
task = record.msg
|
||||||
|
if(task.description is not None):
|
||||||
|
record.msg = '{description} (running {task})'.format(task=task, description=task.description)
|
||||||
|
else:
|
||||||
|
record.msg = 'Running {task}'.format(task=task)
|
||||||
|
message = super(FileFormatter, self).format(record)
|
||||||
|
record.msg = task
|
||||||
|
else:
|
||||||
|
message = super(FileFormatter, self).format(record)
|
||||||
|
return message
|
63
base/main.py
63
base/main.py
|
@ -1,12 +1,12 @@
|
||||||
import logging
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
import log
|
||||||
args = get_args()
|
args = get_args()
|
||||||
setup_logger(args)
|
logfile = log.get_logfile_path(args.manifest)
|
||||||
|
log.setup_logger(logfile=logfile, debug=args.debug)
|
||||||
run(args)
|
run(args)
|
||||||
|
|
||||||
|
|
||||||
def get_args():
|
def get_args():
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
parser = ArgumentParser(description='Bootstrap Debian for the cloud.')
|
parser = ArgumentParser(description='Bootstrap Debian for the cloud.')
|
||||||
|
@ -16,32 +16,6 @@ def get_args():
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def setup_logger(args):
|
|
||||||
import sys
|
|
||||||
import os.path
|
|
||||||
from datetime import datetime
|
|
||||||
root = logging.getLogger()
|
|
||||||
root.setLevel(logging.NOTSET)
|
|
||||||
|
|
||||||
manifest_basename = os.path.basename(args.manifest)
|
|
||||||
manifest_name, _ = os.path.splitext(manifest_basename)
|
|
||||||
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
|
||||||
filename = "{timestamp}_{name}.log".format(timestamp=timestamp, name=manifest_name)
|
|
||||||
path=os.path.normpath(os.path.join(os.path.dirname(__file__), '../logs', filename))
|
|
||||||
file_handler = logging.FileHandler(path)
|
|
||||||
file_handler.setFormatter(FileFormatter('[%(asctime)s] %(message)s'))
|
|
||||||
file_handler.setLevel(logging.DEBUG)
|
|
||||||
root.addHandler(file_handler)
|
|
||||||
|
|
||||||
console_handler = logging.StreamHandler(sys.stderr)
|
|
||||||
console_handler.setFormatter(ConsoleFormatter())
|
|
||||||
if args.debug:
|
|
||||||
console_handler.setLevel(logging.DEBUG)
|
|
||||||
else:
|
|
||||||
console_handler.setLevel(logging.INFO)
|
|
||||||
root.addHandler(console_handler)
|
|
||||||
|
|
||||||
|
|
||||||
def run(args):
|
def run(args):
|
||||||
from manifest import load_manifest
|
from manifest import load_manifest
|
||||||
(provider, manifest) = load_manifest(args.manifest)
|
(provider, manifest) = load_manifest(args.manifest)
|
||||||
|
@ -55,34 +29,3 @@ def run(args):
|
||||||
from bootstrapinfo import BootstrapInformation
|
from bootstrapinfo import BootstrapInformation
|
||||||
bootstrap_info = BootstrapInformation(manifest=manifest, debug=args.debug)
|
bootstrap_info = BootstrapInformation(manifest=manifest, debug=args.debug)
|
||||||
tasklist.run(bootstrap_info)
|
tasklist.run(bootstrap_info)
|
||||||
|
|
||||||
|
|
||||||
class ConsoleFormatter(logging.Formatter):
|
|
||||||
|
|
||||||
def format(self, record):
|
|
||||||
from task import Task
|
|
||||||
if(isinstance(record.msg, Task)):
|
|
||||||
task = record.msg
|
|
||||||
if(task.description is not None):
|
|
||||||
return '\033[0;34m{description}\033[0m'.format(description=task.description)
|
|
||||||
else:
|
|
||||||
return '\033[0;34mRunning {task}\033[0m'.format(task=task)
|
|
||||||
return super(ConsoleFormatter, self).format(record)
|
|
||||||
|
|
||||||
|
|
||||||
class FileFormatter(logging.Formatter):
|
|
||||||
|
|
||||||
def format(self, record):
|
|
||||||
from task import Task
|
|
||||||
from datetime import datetime
|
|
||||||
if(isinstance(record.msg, Task)):
|
|
||||||
task = record.msg
|
|
||||||
if(task.description is not None):
|
|
||||||
record.msg = '{description} (running {task})'.format(task=task, description=task.description)
|
|
||||||
else:
|
|
||||||
record.msg = 'Running {task}'.format(task=task)
|
|
||||||
message = super(FileFormatter, self).format(record)
|
|
||||||
record.msg = task
|
|
||||||
else:
|
|
||||||
message = super(FileFormatter, self).format(record)
|
|
||||||
return message
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue