Skip to content

Commit

Permalink
Merge pull request #171 from larsbutler/better-req-resp-logging
Browse files Browse the repository at this point in the history
Better HTTP request/response logging
  • Loading branch information
pkit committed Jul 31, 2014
2 parents 0dedfee + d4e3418 commit d3059e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
23 changes: 21 additions & 2 deletions zpmlib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
LOG = zpmlib.get_logger(__name__)


class SwiftLogFilter(logging.Filter):

def filter(self, record):
# We want to filter out 404 errors when fetching containers.
# In cases where this happens, we already deal with the exception,
# so there's no need to constantly show spurious ERROR level messages
# to the user.
if (record.levelname == 'ERROR'
and record.msg.msg == 'Container GET failed'
and record.msg.http_status == 404):
return False
return True


def set_up_arg_parser():
parser = argparse.ArgumentParser(
description='ZeroVM Package Manager',
Expand Down Expand Up @@ -89,8 +103,13 @@ def inner(namespace, *args, **kwargs):
:class:`argparse.Namespace` instance. This is the only
required/expected parameter for a command function.
"""
root_logger = logging.getLogger(None)
root_logger.setLevel(zpmlib.LOG_LEVEL_MAP.get(namespace.log_level))
log_level = zpmlib.LOG_LEVEL_MAP.get(namespace.log_level)
logging.getLogger('zpmlib').setLevel(log_level)
# This allows us to see `swiftclient` log messages, specifically HTTP
# REQ/RESP
swift_log = zpmlib.get_logger('swiftclient')
swift_log.setLevel(log_level)
swift_log.addFilter(SwiftLogFilter())
return func(namespace, *args, **kwargs)

return log_level_deco(inner)
Expand Down
18 changes: 18 additions & 0 deletions zpmlib/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import mock

from zpmlib import commands
from swiftclient.exceptions import ClientException


def test_all_commands_sorted():
cmd_names = [cmd.__name__ for cmd in commands.all_commands()]
assert cmd_names == sorted(cmd_names)


def test_swift_log_filter():
log_filter = commands.SwiftLogFilter()

record = mock.Mock()
record.levelname = 'INFO'

filtered_record = mock.Mock()
filtered_record.levelname = 'ERROR'
filtered_record.msg = ClientException('Container GET failed',
http_status=404)

assert log_filter.filter(record) is True
assert log_filter.filter(filtered_record) is False

0 comments on commit d3059e7

Please sign in to comment.