summaryrefslogtreecommitdiff
path: root/obalie/log.py
blob: bf65996223d1516faf3bb941070a5355cb91a22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# vim: set filencoding=utf8
"""
Logging Utlities for obalie

@author: Mike Crute (mcrute@ag.com)
@organization: SoftGroup Interactive, Inc.
@date: April 20, 2010
"""


import sys
import logging
from logging import Formatter, StreamHandler
from functools import wraps


# Setup root logger
root_logger = logging.getLogger('obalie')

# If the root logger is not already configured then we
# should probably configure it with some sane defaults.
if root_logger.level == logging.NOTSET:
#   XXX: RESET THIS!
#    root_logger.setLevel(logging.WARN)
    root_logger.setLevel(logging.DEBUG)

    handler = StreamHandler(sys.stdout)
    formatter = Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')

    handler.setFormatter(formatter)
    root_logger.addHandler(handler)


def inject_logger(cls):
    """
    Injects a logger into the kwargs for a class.
    """
    @wraps(cls)
    def decorator(*args, **kwargs):
        inst = args[0] # self
        kwargs['logger'] = logging.getLogger('.'.join([inst.__module__, inst.__class__.__name__]))
        cls(*args, **kwargs)

    return decorator