Simplify logging

There is no need to manage the logger through a utility function.
Therefore, remove all that logic and just use the logging module
correctly.
This commit is contained in:
Craig Tracey 2014-11-14 16:32:47 -05:00
parent c78cf7676c
commit c2a5698a0f
6 changed files with 28 additions and 63 deletions

View File

@ -14,16 +14,16 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
import logging
import os
import sys
from giftwrap import log
from giftwrap.gerrit import GerritReview
from giftwrap.openstack_git_repo import OpenstackGitRepo
from giftwrap.package import Package
from giftwrap.util import execute
LOG = log.get_logger()
LOG = logging.getLogger(__name__)
class Builder(object):

View File

@ -1,48 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2014, John Dewey
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import sys
from giftwrap.color import ColorStreamHandler
NAME = 'giftwrap'
_logger = None
def get_logger():
global _logger
if _logger:
return _logger
_logger = logging.getLogger(NAME)
log_handler = ColorStreamHandler(sys.stdout)
fmt = logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s',
datefmt='%F %H:%M:%S')
log_handler.setFormatter(fmt)
_logger.addHandler(log_handler)
_logger.setLevel(logging.INFO)
_logger.propagate = False
return _logger
def set_level_debug():
logger = get_logger()
logger.setLevel(logging.DEBUG)

View File

@ -15,13 +15,13 @@
# License for the specific language governing permissions and limitations
import datetime
import logging
import re
import time
from giftwrap import log
from git import Repo
LOG = log.get_logger()
LOG = logging.getLogger(__name__)
class OpenstackGitRepo(object):

View File

@ -15,13 +15,24 @@
# License for the specific language governing permissions and limitations
import argparse
import logging
import sys
from giftwrap import log
from giftwrap.build_spec import BuildSpec
from giftwrap.builder import Builder
from giftwrap.color import ColorStreamHandler
LOG = log.get_logger()
LOG = logging.getLogger(__name__)
def _setup_logger(level=logging.INFO):
logger = logging.getLogger()
logger.setLevel(level)
log_handler = ColorStreamHandler(sys.stdout)
fmt = logging.Formatter(fmt='%(asctime)s %(name)s %(levelname)s: '
'%(message)s', datefmt='%F %H:%M:%S')
log_handler.setFormatter(fmt)
logger.addHandler(log_handler)
def build(args):
@ -36,7 +47,7 @@ def build(args):
builder = Builder(buildspec)
builder.build()
except Exception as e:
LOG.exception("Unable to parse manifest. Error: %s", e)
LOG.exception("Oops something went wrong: %s", e)
sys.exit(-1)
@ -56,8 +67,10 @@ def main():
args = parser.parse_args()
log_level = logging.INFO
if args.debug:
log.set_level_debug()
log_level = logging.DEBUG
_setup_logger(log_level)
args.func(args)

View File

@ -15,17 +15,18 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import unittest2 as unittest
from nose.plugins.logcapture import LogCapture
from giftwrap import log
class TestLog(unittest.TestCase):
def test_get_logger(self):
lc = LogCapture()
lc.begin()
logger = log.get_logger()
logger = logging.getLogger('giftwrap')
logger.setLevel(logging.INFO)
logger.debug('test-debug')
logger.info('test-info')
lc.end()
@ -35,8 +36,8 @@ class TestLog(unittest.TestCase):
def test_get_logger_debug(self):
lc = LogCapture()
lc.begin()
logger = log.get_logger()
log.set_level_debug()
logger = logging.getLogger('giftwrap')
logger.setLevel(logging.DEBUG)
logger.info('test-info')
logger.debug('test-debug')
lc.end()

View File

@ -15,12 +15,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os
import subprocess
from giftwrap import log
LOG = log.get_logger()
LOG = logging.getLogger(__name__)
def execute(command, cwd=None, exit=0):