Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement --terse option #44

Merged
merged 1 commit into from
Oct 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion tests/test_log_help.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import logging

import wal_e.log_help as log_help
from wal_e import log_help


def sanitize_log(log):
Expand Down Expand Up @@ -52,3 +53,43 @@ def test_fmt_logline_simple():
DETAIL: The detail
HINT: The hint
STRUCTURED: time=2012-01-01T00.1234-00 pid=1234"""


def test_log_levels(monkeypatch):
l = log_help.WalELogger()

class DidLog(object):
def __init__(self):
self.called = False

def __call__(self, *args, **kwargs):
self.called = True

# Try the default logging level, which should log INFO-level.
d = DidLog()
monkeypatch.setattr(l._logger, 'log', d)
l.log(level=logging.INFO, msg='hello')
assert d.called is True

# Test default elision of DEBUG-level statements.
d = DidLog()
monkeypatch.setattr(l._logger, 'log', d)
l.log(level=logging.DEBUG, msg='hello')
assert d.called is False

# Adjust log level ignore INFO and below
log_help.MINIMUM_LOG_LEVEL = logging.WARNING

# Test elision of INFO level statements once minimum level has
# been adjusted.
d = DidLog()
monkeypatch.setattr(l._logger, 'log', d)
l.log(level=logging.INFO, msg='hello')
assert d.called is False

# Make sure WARNING level is still logged even if minimum level
# has been adjusted.
d = DidLog()
monkeypatch.setattr(l._logger, 'log', d)
l.log(level=logging.WARNING, msg='HELLO!')
assert d.called is True
10 changes: 9 additions & 1 deletion wal_e/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def wrap_socket_monkey(*args, **kwargs):
log_help.configure(
format='%(name)-12s %(levelname)-8s %(message)s')

logger = log_help.WalELogger('wal_e.main', level=logging.INFO)
logger = log_help.WalELogger('wal_e.main')


def external_program_check(
Expand Down Expand Up @@ -189,6 +189,10 @@ def main(argv=None):
'Can also be defined via environment variable '
'WALE_GPG_KEY_ID')

parser.add_argument(
'--terse', action='store_true',
help='Only log messages as or more severe than a warning.')

subparsers = parser.add_subparsers(title='subcommands',
dest='subcommand')

Expand Down Expand Up @@ -312,6 +316,10 @@ def main(argv=None):
args = parser.parse_args()
subcommand = args.subcommand

# Adjust logging level if terse output is set.
if args.terse:
log_help.MINIMUM_LOG_LEVEL = logging.WARNING

# Handle version printing specially, because it doesn't need
# credentials.
if subcommand == 'version':
Expand Down
15 changes: 7 additions & 8 deletions wal_e/log_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import os


# Minimum logging level to emit logs for, inclusive.
MINIMUM_LOG_LEVEL = logging.INFO


class IndentFormatter(logging.Formatter):

def format(self, record, *args, **kwargs):
Expand Down Expand Up @@ -81,16 +85,8 @@ def terrible_log_output(s):

class WalELogger(object):
def __init__(self, *args, **kwargs):
# Enable a shortcut to create the logger and set its level all
# at once. To do that, pop the level out of the dictionary,
# which will otherwise cause getLogger to explode.
level = kwargs.pop('level', None)

self._logger = logging.getLogger(*args, **kwargs)

if level is not None:
self._logger.setLevel(level)

@staticmethod
def _fmt_structured(d):
"""Formats '{k1:v1, k2:v2}' => 'time=... pid=... k1=v1 k2=v2'
Expand Down Expand Up @@ -129,6 +125,9 @@ def fmt_logline(msg, detail=None, hint=None, structured=None):
return '\n'.join(msg_parts)

def log(self, level, msg, *args, **kwargs):
if level < MINIMUM_LOG_LEVEL:
return

detail = kwargs.pop('detail', None)
hint = kwargs.pop('hint', None)
structured = kwargs.pop('structured', None)
Expand Down
3 changes: 1 addition & 2 deletions wal_e/operator/s3_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import gevent.pool
import itertools
import json
import logging
import os
import sys

Expand All @@ -21,7 +20,7 @@
from wal_e.worker import PgBackupStatements
from wal_e.worker import PgControlDataParser

logger = log_help.WalELogger(__name__, level=logging.INFO)
logger = log_help.WalELogger(__name__)

# Provides guidence in object names as to the version of the file
# structure.
Expand Down
3 changes: 1 addition & 2 deletions wal_e/retries.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import functools
import logging
import sys
import traceback

import gevent

import wal_e.log_help as log_help

logger = log_help.WalELogger(__name__, level=logging.INFO)
logger = log_help.WalELogger(__name__)


def generic_exception_processor(exc_tup, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions wal_e/s3/calling_format.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import boto
import logging

from boto import s3
from boto.s3 import connection
from wal_e import log_help

logger = log_help.WalELogger(__name__, level=logging.INFO)
logger = log_help.WalELogger(__name__)

_S3_REGIONS = {
# A map like this is actually defined in boto.s3 in newer versions of boto
Expand Down
3 changes: 1 addition & 2 deletions wal_e/worker/s3_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import boto.exception
import gevent
import json
import logging
import re
import socket
import tarfile
Expand All @@ -29,7 +28,7 @@
from wal_e.s3 import calling_format
from wal_e.worker import s3_deleter

logger = log_help.WalELogger(__name__, level=logging.INFO)
logger = log_help.WalELogger(__name__)


generic_weird_key_hint_message = ('This means an unexpected key was found in '
Expand Down