Skip to content
Permalink
Browse files

Merge pull request #89 from abadger/avoid-circular-imports

Fix circular imports
  • Loading branch information...
wearpants committed Sep 6, 2018
2 parents be353da + 9b548aa commit d4c5b57abc551a44a6ee7ca01f5622cc3b277456
Showing with 27 additions and 20 deletions.
  1. +3 −7 twiggy/__init__.py
  2. +5 −4 twiggy/lib/validators.py
  3. +19 −9 twiggy/logger.py
@@ -33,19 +33,15 @@ def _populate_globals():

emitters = log._emitters

__internal_format = formats.LineFormat(conversion=formats.line_conversion)
__internal_output = outputs.StreamOutput(format=__internal_format, stream=sys.stderr)

internal_log = logger.InternalLogger(fields=__fields, output=__internal_output
).name('twiggy.internal').trace('error')
internal_log = logger.internal_log

devel_log = logger.InternalLogger(fields=__fields, output=outputs.NullOutput()
).name('twiggy.devel')


def _del_globals():
global __fields, log, emitters, __internal_format, __internal_output, internal_log, devel_log
del __fields, log, emitters, __internal_format, __internal_output, internal_log, devel_log
global __fields, log, emitters, internal_log, devel_log
del __fields, log, emitters, internal_log, devel_log


if not os.environ.get('TWIGGY_UNDER_TEST', None): # pragma: no cover
@@ -3,7 +3,8 @@
from six import integer_types, string_types
from six.moves import builtins

import twiggy
from .. import formats
from .. import levels


def _import_module(module_name):
@@ -182,7 +183,7 @@ def _validate_config(config):
raise ValueError("`outputs` must be a dict mapping output_names to outputs")

for output_name, output in config['outputs'].items():
new_output = {'args': [], 'kwargs': {}, 'format': twiggy.formats.line_format}
new_output = {'args': [], 'kwargs': {}, 'format': formats.line_format}

if not isinstance(output, Mapping):
raise ValueError("`outputs` must be a dict of dicts")
@@ -225,13 +226,13 @@ def _validate_config(config):
raise ValueError("Every entry in `emitters` must contain a `level` field")
if isinstance(emitter['level'], string_types):
try:
level = twiggy.levels.name2level(emitter['level'])
level = levels.name2level(emitter['level'])
except KeyError:
raise ValueError("The `level` field of `emitters` must be the string name of"
" a twiggy log level")
else:
level = emitter['level']
if not isinstance(level, twiggy.levels.LogLevel):
if not isinstance(level, levels.LogLevel):
raise ValueError("The `level` field of `emitters` must be a string naming"
" a Twiggy LogLevel")
new_emitter['level'] = level
@@ -1,14 +1,16 @@
from __future__ import print_function

import sys
import time
import traceback
import warnings
from functools import wraps

from six import iteritems

import twiggy as _twiggy
from . import formats
from . import levels
from . import outputs
from .lib import iso8601time
from .message import Message

@@ -255,8 +257,8 @@ def _emit(self, level, format_spec, args, kwargs):
if not self.filter(format_spec):
return
except Exception:
_twiggy.internal_log.info("Error in Logger filtering with {0} on {1}",
repr(self.filter), format_spec)
internal_log.info("Error in Logger filtering with {0} on {1}",
repr(self.filter), format_spec)
# just continue emitting in face of filter error

# XXX should we trap here too b/c of "Dictionary changed size during iteration" (or
@@ -272,9 +274,9 @@ def _emit(self, level, format_spec, args, kwargs):
args, kwargs)
except Exception:
# XXX use .fields() instead?
_twiggy.internal_log.info("Error formatting message level: {0!r}, format: {1!r},"
" fields: {2!r}, options: {3!r}, args: {4!r}, kwargs: {5!r}",
level, format_spec, self._fields, self._options, args, kwargs)
internal_log.info("Error formatting message level: {0!r}, format: {1!r},"
" fields: {2!r}, options: {3!r}, args: {4!r}, kwargs: {5!r}",
level, format_spec, self._fields, self._options, args, kwargs)
return

outputs = set()
@@ -283,8 +285,8 @@ def _emit(self, level, format_spec, args, kwargs):
try:
include = emitter.filter(msg)
except Exception:
_twiggy.internal_log.info("Error filtering with emitter {0}. Filter: {1}"
" Message: {2!r}", name, repr(emitter.filter), msg)
internal_log.info("Error filtering with emitter {0}. Filter: {1}"
" Message: {2!r}", name, repr(emitter.filter), msg)
include = True # output anyway if error

if include:
@@ -294,4 +296,12 @@ def _emit(self, level, format_spec, args, kwargs):
try:
o.output(msg)
except Exception:
_twiggy.internal_log.warning("Error outputting with {0!r}. Message: {1!r}", o, msg)
internal_log.warning("Error outputting with {0!r}. Message: {1!r}", o, msg)


__fields = {'time': time.gmtime}
__internal_format = formats.LineFormat(conversion=formats.line_conversion)
__internal_output = outputs.StreamOutput(format=__internal_format, stream=sys.stderr)

internal_log = InternalLogger(fields=__fields, output=__internal_output
).name('twiggy.internal').trace('error')

0 comments on commit d4c5b57

Please sign in to comment.
You can’t perform that action at this time.