Пример #1
0
def get_test_record():
    logger = Logger('test_logger')
    with logger.critical('test_task', reraise=False) as test_rec:
        time.sleep(0.7)
        test_rec['item'] = 'my_item'
        test_rec.warn("things aren't looking great")
        test_rec.failure('task status: {status_str}')
        raise ValueError('unexpected value for {item}')
    return test_rec
Пример #2
0
    def _setup_api_log(self):
        self.log_file_obj = open(self.log_path, 'ab')
        self.exc_log_file_obj = open(self.exc_log_path, 'ab')

        self.api_log = Logger(self.act_name.lower() + '_log')
        self._api_fmtr = SensibleFormatter(API_LOG_FMT)
        self._api_emtr = StreamEmitter(self.log_file_obj)
        self._api_fltr = SensibleFilter(success='info',
                                        failure='debug',
                                        exception='debug')
        self._api_sink = SensibleSink(formatter=self._api_fmtr,
                                      emitter=self._api_emtr,
                                      filters=[self._api_fltr])
        self.api_log.add_sink(self._api_sink)

        self._exc_emtr = StreamEmitter(self.exc_log_file_obj)
        self._exc_fltr = SensibleFilter(success=None,
                                        failure=None,
                                        exception='info')
        self._exc_sink = SensibleSink(formatter=self._api_fmtr,
                                      emitter=self._exc_emtr,
                                      filters=[self._exc_fltr])
        self.api_log.add_sink(self._exc_sink)
Пример #3
0
def test_wrap_all_json():
    agg_sink = AggregateSink()
    log = Logger('wrapper_log', sinks=[agg_sink])

    wrap_all(log, 'info', json)

    json.loads('{}')

    assert agg_sink.begin_events[0].name == 'json.loads'

    unwrap_all(json)

    json.loads('{}')

    assert len(agg_sink.begin_events) == 1

    return
def test_wrap_all_json():
    agg_sink = AggregateSink()
    log = Logger('wrapper_log', sinks=[agg_sink])

    wrap_all(log, 'info', json, skip_exc=True)

    json.loads('{}')

    assert agg_sink.begin_events[0].name == 'json.loads'

    unwrap_all(json)

    json.loads('{}')

    if IS_PYPY:
        # different impl
        assert len(agg_sink.begin_events) == 1
    else:
        assert len(agg_sink.begin_events) == 3

    return
Пример #5
0
def test_basic_counter():
    csink = CounterSink()
    log = Logger('ctr_log', [csink])
    log.debug('t').success('est')

    assert csink.counter_map[log]['t'] == 1

    for i in range(1000):
        log.debug('e').success('st')

    assert csink.counter_map[log]['e'] == 1000
    assert csink.counter_map[log].get('t') == 0

    cdict = csink.to_dict()
    assert cdict == {'ctr_log': {'e': 1000, '__all__': 1001, '__missing__': 1}}
    return
Пример #6
0
def test_wrap():
    logger = Logger('t')

    @logger.wrap('critical')
    def t_func():
        return True

    assert t_func() is True

    @logger.wrap('critical', inject_as='yep')
    def y_func(yep):
        return bool(yep)

    assert y_func() is True

    # try inject_as with an argument that isn't there
    with pytest.raises(ValueError):

        @logger.wrap('critical', inject_as='nope')
        def t_func():
            return False

    return
Пример #7
0
import os

from lithoxyl import Logger, SensibleSink, SensibleFormatter, StreamEmitter, SensibleFilter
from lithoxyl.emitters import FileEmitter

CUR_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_PATH = os.path.dirname(CUR_PATH)
LOG_PATH = PROJECT_PATH + '/pacetrack.log'
JSUB_LOG_PATH = PROJECT_PATH + '/jsub_logs/'

tlog = Logger('pacetrack')
file_fmt = SensibleFormatter(
    '{status_char}{iso_end_notz} - {duration_s}s - {action_name} - {end_message}',
    begin=
    '{status_char}{iso_begin_notz} -   --   - {action_name} - {begin_message}')


def build_stream_sink(stream):
    emt = StreamEmitter(stream)
    file_filter = SensibleFilter(success='info',
                                 failure='debug',
                                 exception='debug')
    file_sink = SensibleSink(formatter=file_fmt,
                             emitter=emt,
                             filters=[file_filter])
    return file_sink


default_file_sink = build_stream_sink(open(LOG_PATH, 'a'))
tlog.add_sink(default_file_sink)
Пример #8
0
class LoggingMiddleware(Middleware):
    provides = (
        'api_log',
        'api_act',
    )

    def __init__(self, log_path, act_name='API'):
        self.log_path = os.path.abspath(log_path)
        root, ext = os.path.splitext(self.log_path)
        self.exc_log_path = '%s.exc%s' % (root, ext)
        self.act_name = act_name

        self._setup_api_log()

    def request(self, next, request, _route):
        try:
            act_name = '%s%s' % (request.method, _route.pattern)
            with self.api_log.critical(act_name) as api_act:
                # basic redacted url
                api_act['path'] = request.path
                api_act.data_map.update(request.args.items())
                try:
                    ret = next(api_act=api_act, api_log=self.api_log)
                except clastic.errors.BadRequest as br:
                    api_act.data_map.update(br.to_dict())
                    api_act.failure()
                    ret = br
                api_act['code'] = ret.status_code
        except Exception:
            exc_info = ExceptionInfo.from_current()
            text = u'\n\n' + exc_info.get_formatted() + '\n\n'
            self.exc_log_file_obj.write(text.encode('utf8'))
            self.exc_log_file_obj.flush()
            raise
        return ret

    def endpoint(self, next, api_act):
        ret = next()
        try:
            api_act['username'] = ret['user'].username
        except Exception:
            pass
        return ret

    def _setup_api_log(self):
        self.log_file_obj = open(self.log_path, 'ab')
        self.exc_log_file_obj = open(self.exc_log_path, 'ab')

        self.api_log = Logger(self.act_name.lower() + '_log')
        self._api_fmtr = SensibleFormatter(API_LOG_FMT)
        self._api_emtr = StreamEmitter(self.log_file_obj)
        self._api_fltr = SensibleFilter(success='info',
                                        failure='debug',
                                        exception='debug')
        self._api_sink = SensibleSink(formatter=self._api_fmtr,
                                      emitter=self._api_emtr,
                                      filters=[self._api_fltr])
        self.api_log.add_sink(self._api_sink)

        self._exc_emtr = StreamEmitter(self.exc_log_file_obj)
        self._exc_fltr = SensibleFilter(success=None,
                                        failure=None,
                                        exception='info')
        self._exc_sink = SensibleSink(formatter=self._api_fmtr,
                                      emitter=self._exc_emtr,
                                      filters=[self._exc_fltr])
        self.api_log.add_sink(self._exc_sink)
Пример #9
0
# -*- coding: utf-8 -*-

import os

from lithoxyl import (Logger, StreamEmitter, SensibleSink, SensibleFilter,
                      SensibleFormatter)

from lithoxyl.sinks import DevDebugSink

# import lithoxyl; lithoxyl.get_context().enable_async()

chert_log = Logger('chert')

fmt = ('{status_char}+{import_delta_s}'
       ' - {duration_ms:>8.3f}ms'
       ' - {parent_depth_indent}{end_message}')

begin_fmt = ('{status_char}+{import_delta_s}'
             ' --------------'
             ' {parent_depth_indent}{begin_message}')

stderr_fmtr = SensibleFormatter(fmt, begin=begin_fmt)
stderr_emtr = StreamEmitter('stderr')
stderr_filter = SensibleFilter(success='info',
                               failure='debug',
                               exception='debug')
stderr_sink = SensibleSink(formatter=stderr_fmtr,
                           emitter=stderr_emtr,
                           filters=[stderr_filter])
chert_log.add_sink(stderr_sink)
Пример #10
0
from lithoxyl import (Logger, StreamEmitter, SensibleSink, SensibleFilter,
                      SensibleFormatter)

from lithoxyl.sinks import DevDebugSink

fmt = ('{status_char}+{import_delta_s:.3f}'
       ' - {duration_s:>8.3f}s'
       ' - {parent_depth_indent}{event_message}')

begin_fmt = ('{status_char}+{import_delta_s:.3f}'
             ' -------------'
             ' {parent_depth_indent}{event_message}')

comment_fmt = ('{status_char} - {iso_begin} - {event_message}')

sky_log = Logger('sky')
sky_log.log_file_path = None


def _on_import_log_setup():
    # called at the bottom of the module
    stderr_fmtr = CompactFormatter()
    stderr_emtr = StreamEmitter('stderr', sep='')
    stderr_filter = SensibleFilter(begin='info',
                                   success='info',
                                   failure='info',
                                   exception='debug')
    stderr_sink = SensibleSink(formatter=stderr_fmtr,
                               emitter=stderr_emtr,
                               filters=[stderr_filter])
    sky_log.add_sink(stderr_sink)
Пример #11
0
import os

from lithoxyl import Logger, SensibleSink, SensibleFormatter, StreamEmitter, SensibleFilter
from lithoxyl.emitters import FileEmitter

class FixedFileEmitter(FileEmitter):
    def __init__(self, filepath, encoding=None, **kwargs):
        self.encoding = encoding
        super(FixedFileEmitter, self).__init__(filepath, encoding, **kwargs)

CUR_PATH = os.path.dirname(os.path.abspath(__file__))
LOG_FILE_PATH = os.path.join(CUR_PATH, 'logs', 'update_log.txt')

tlog = Logger('toplog')

file_fmt = SensibleFormatter('{status_char}{iso_end_local_noms_notz} - {duration_s}s - {action_name} - {event_message}')
file_emt = FixedFileEmitter(LOG_FILE_PATH)
file_filter = SensibleFilter(success='critical',
                              failure='info',
                              exception='debug')
file_sink = SensibleSink(formatter=file_fmt,
                         emitter=file_emt,
                         filters=[file_filter])
tlog.add_sink(file_sink)


def set_debug(enable=True):
    if not enable:
        raise NotImplementedError()
    dbg_fmtr = file_fmt
    dbg_emtr = StreamEmitter('stderr')
Пример #12
0
import os

from lithoxyl import Logger, SensibleSink, Formatter, StreamEmitter
from lithoxyl.emitters import FileEmitter
from lithoxyl.filters import ThresholdFilter

class FixedFileEmitter(FileEmitter):
    def __init__(self, filepath, encoding=None, **kwargs):
        self.encoding = encoding
        super(FixedFileEmitter, self).__init__(filepath, encoding, **kwargs)

CUR_PATH = os.path.dirname(os.path.abspath(__file__))
LOG_FILE_PATH = os.path.join(CUR_PATH, 'logs', 'update_log.txt')

tlog = Logger('toplog')

file_fmt = Formatter('{status_char}{end_local_iso8601_noms_notz} - {duration_secs}s - {record_name} - {message}')
file_emt = FixedFileEmitter(LOG_FILE_PATH)
file_filter = ThresholdFilter(success='critical',
                              failure='info',
                              exception='debug')
file_sink = SensibleSink(formatter=file_fmt,
                         emitter=file_emt,
                         filters=[file_filter])
tlog.add_sink(file_sink)


def set_debug(enable=True):
    if not enable:
        raise NotImplementedError()
    dbg_fmtr = file_fmt
Пример #13
0
import os

from lithoxyl import (Logger, StreamEmitter, SensibleSink, SensibleFilter,
                      SensibleFormatter)

from lithoxyl.sinks import DevDebugSink

# import lithoxyl; lithoxyl.get_context().enable_async()

script_log = Logger('dev_script')

fmt = ('{status_char}+{import_delta_s}'
       ' - {duration_ms:>8.3f}ms'
       ' - {parent_depth_indent}{end_message}')

begin_fmt = ('{status_char}+{import_delta_s}'
             ' --------------'
             ' {parent_depth_indent}{begin_message}')

stderr_fmtr = SensibleFormatter(fmt, begin=begin_fmt)
stderr_emtr = StreamEmitter('stderr')
stderr_filter = SensibleFilter(success='info',
                               failure='debug',
                               exception='debug')
stderr_sink = SensibleSink(formatter=stderr_fmtr,
                           emitter=stderr_emtr,
                           filters=[stderr_filter])
script_log.add_sink(stderr_sink)

dds = DevDebugSink(post_mortem=bool(os.getenv('ENABLE_PDB')))
script_log.add_sink(dds)
Пример #14
0
def test_wrap_all_element_tree():
    log = Logger('test', sinks=[])

    # test old-style class wrapping / unwrapping
    wrap_all(log, target=ElementTree)
    unwrap_all(ElementTree)
Пример #15
0
# -*- coding: utf-8 -*-

import os

from lithoxyl import (Logger,
                      StreamEmitter,
                      SensibleSink,
                      SensibleFilter,
                      SensibleFormatter)

from lithoxyl.sinks import DevDebugSink

# import lithoxyl; lithoxyl.get_context().enable_async()

chert_log = Logger('chert')

fmt = ('{status_char}+{import_delta_s}'
       ' - {duration_ms:>8.3f}ms'
       ' - {parent_depth_indent}{end_message}')

begin_fmt = ('{status_char}+{import_delta_s}'
             ' --------------'
             ' {parent_depth_indent}{begin_message}')

stderr_fmtr = SensibleFormatter(fmt,
                                begin=begin_fmt)
stderr_emtr = StreamEmitter('stderr')
stderr_filter = SensibleFilter(success='info',
                               failure='debug',
                               exception='debug')
stderr_sink = SensibleSink(formatter=stderr_fmtr,
Пример #16
0
import os

from lithoxyl import Logger, SensibleSink, Formatter, StreamEmitter
from lithoxyl.filters import ThresholdFilter

tlog = Logger('toplog')

stderr_fmt = Formatter('{status_char}{end_local_iso8601_noms_notz} - {duration_secs}s - {record_name} - {message}')
stderr_emt = StreamEmitter('stderr')
stderr_filter = ThresholdFilter(success='debug',
                                failure='debug',
                                exception='debug')
stderr_sink = SensibleSink(formatter=stderr_fmt,
                           emitter=stderr_emt,
                           filters=[stderr_filter])
#                           on=['begin', 'complete'])  # TODO: clunk
tlog.add_sink(stderr_sink)


class DevDebugSink(object):
    # TODO: configurable max number of traceback signatures, after
    #       which exit/ignore?

    def __init__(self, reraise=False, post_mortem=False):
        self.reraise = reraise
        self.post_mortem = post_mortem

    #def on_complete(self, record):
    #    some conditions and a pdb perhaps
Пример #17
0
stderr_sink = SensibleSink(formatter=stderr_fmt,
                           emitter=stderr_emt,
                           filters=[stderr_filter])


#ff = FancyFilter(success='info',
#                 failure='debug',
#                 exception='debug',
#                 begin='debug',
#                 with_begin=True)

from lithoxyl import context

# context.get_context().enable_async()

log = Logger('test', sinks=[stderr_sink])


def one_two():
    with log.critical('first') as r1:
        r1['x'] = 'hi'
        with log.critical('second'):
            print 'did some work'
        raise ValueError("oh no, one of those")
    return


for i in range(20):
    one_two()

Пример #18
0
                               begin='debug')
stderr_sink = SensibleSink(formatter=stderr_fmt,
                           emitter=stderr_emt,
                           filters=[stderr_filter])

#ff = FancyFilter(success='info',
#                 failure='debug',
#                 exception='debug',
#                 begin='debug',
#                 with_begin=True)

from lithoxyl import context

# context.get_context().enable_async()

log = Logger('test', sinks=[stderr_sink])


def one_two():
    with log.critical('first') as r1:
        r1['x'] = 'hi'
        with log.critical('second'):
            print 'did some work'
        raise ValueError("oh no, one of those")
    return


for i in range(20):
    one_two()

import os
Пример #19
0
Файл: log.py Проект: hatnote/top
import os

from lithoxyl import Logger, SensibleSink, Formatter, StreamEmitter
from lithoxyl.filters import ThresholdFilter

tlog = Logger("toplog")

stderr_fmt = Formatter("{status_char}{end_local_iso8601_noms_notz} - {duration_secs}s - {record_name} - {message}")
stderr_emt = StreamEmitter("stderr")
stderr_filter = ThresholdFilter(success="debug", failure="debug", exception="debug")
stderr_sink = SensibleSink(formatter=stderr_fmt, emitter=stderr_emt, filters=[stderr_filter])
#                           on=['begin', 'complete'])  # TODO: clunk
tlog.add_sink(stderr_sink)


class DevDebugSink(object):
    # TODO: configurable max number of traceback signatures, after
    #       which exit/ignore?

    def __init__(self, reraise=False, post_mortem=False):
        self.reraise = reraise
        self.post_mortem = post_mortem

    # def on_complete(self, record):
    #    some conditions and a pdb perhaps

    def on_exception(self, record, exc_type, exc_obj, exc_tb):
        if self.post_mortem:
            import pdb

            pdb.post_mortem()