summaryrefslogtreecommitdiffstats
path: root/src/virtBootstrap/virt_bootstrap.py
diff options
context:
space:
mode:
authorRadostin Stoyanov <rstoyanov1@gmail.com>2017-06-29 18:44:32 +0100
committerCédric Bosdonnat <cbosdonnat@suse.com>2017-07-01 07:42:49 +0200
commit1dd194f36079fd822f667419e030fbd07a8480b9 (patch)
treed6f77ef711135c4489cbcceb2b31f9269a3fc9c0 /src/virtBootstrap/virt_bootstrap.py
parent96109db4bd08ffc1e1dcca6f87994761642ce413 (diff)
downloadvirt-bootstrap.git-1dd194f36079fd822f667419e030fbd07a8480b9.tar.gz
virt-bootstrap.git-1dd194f36079fd822f667419e030fbd07a8480b9.tar.xz
virt-bootstrap.git-1dd194f36079fd822f667419e030fbd07a8480b9.zip
Set logger name/format/level
This patch aims to set logger name for the virtBootstrap module. Set logging format to: %(levelname)s: %(message)s and use an elegant way of setting log level.
Diffstat (limited to 'src/virtBootstrap/virt_bootstrap.py')
-rwxr-xr-xsrc/virtBootstrap/virt_bootstrap.py45
1 files changed, 36 insertions, 9 deletions
diff --git a/src/virtBootstrap/virt_bootstrap.py b/src/virtBootstrap/virt_bootstrap.py
index 90379cc..681e50d 100755
--- a/src/virtBootstrap/virt_bootstrap.py
+++ b/src/virtBootstrap/virt_bootstrap.py
@@ -23,10 +23,10 @@ and calls corresponding methods on appropriate object.
import argparse
import gettext
+import logging
import sys
import os
from textwrap import dedent
-from logging import getLogger, DEBUG, INFO, WARNING, error
from subprocess import CalledProcessError, Popen, PIPE
try:
from urlparse import urlparse
@@ -51,6 +51,10 @@ except IOError:
import builtin
builtin.__dict__['_'] = str
+# pylint: disable=invalid-name
+# Create logger
+logger = logging.getLogger(__name__)
+
def get_source(args):
"""
@@ -86,18 +90,16 @@ def bootstrap(args):
"""
Get source object and call unpack method
"""
- # Set log level
- logger = getLogger()
- logger.setLevel(DEBUG if args.debug else WARNING if args.quiet else INFO)
source = get_source(args)
if not os.path.exists(args.dest):
os.makedirs(args.dest)
elif not os.path.isdir(args.dest): # Show error if not directory
- error("Destination path '%s' is not directory.", args.dest)
+ logger.error("Destination path '%s' is not directory.", args.dest)
sys.exit(1)
elif not os.access(args.dest, os.W_OK): # Check write permissions
- error("No write permissions on destination path '%s'", args.dest)
+ logger.error("No write permissions on destination path '%s'",
+ args.dest)
sys.exit(1)
source.unpack(args.dest)
@@ -106,6 +108,27 @@ def bootstrap(args):
set_root_password(args.dest, args.root_password)
+def set_logging_conf(loglevel=None):
+ """
+ Set format and logging level
+ """
+ # Get logger
+ module_logger = logging.getLogger('virtBootstrap')
+
+ # Create console handler
+ console_handler = logging.StreamHandler()
+
+ # Set logging format
+ log_format = ('%(levelname)-8s: %(message)s')
+ console_handler.setFormatter(logging.Formatter(log_format))
+
+ # Add the handlers to logger
+ module_logger.addHandler(console_handler)
+
+ # Set logging level
+ module_logger.setLevel(loglevel or logging.INFO)
+
+
def main():
parser = argparse.ArgumentParser(
description=_("Container bootstrapping tool"),
@@ -140,9 +163,10 @@ def main():
parser.add_argument("-f", "--format", default='dir',
choices=['dir', 'qcow2'],
help=_("Format to be used for the root filesystem"))
- parser.add_argument("-d", "--debug", action="store_true",
- help=_("Show debug messages"))
- parser.add_argument("-q", "--quiet", action="store_true",
+ parser.add_argument("-d", "--debug", action="store_const", dest="loglevel",
+ const=logging.DEBUG, help=_("Show debug messages"))
+ parser.add_argument("-q", "--quiet", action="store_const", dest="loglevel",
+ const=logging.WARNING,
help=_("Suppresses messages notifying about"
"current state or actions of virt-bootstrap"))
# TODO add UID / GID mapping parameters
@@ -150,6 +174,9 @@ def main():
try:
args = parser.parse_args()
+ # Configure logging lovel/format
+ set_logging_conf(args.loglevel)
+
# do the job here!
bootstrap(args)