summaryrefslogtreecommitdiff
path: root/crocoite/logger.py
blob: 7f4a4dea7765159f522be5ca5551d617874ff8aa (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (c) 2017–2018 crocoite contributors
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""
Simple logger inspired by structlog.

It is usually used like this: Classes are passed a logger instance. They bind
context to their name, so identifying the source of messages is easier. Every
log message carries a unique id (uuid) for automated identification as well as
a short human-readable message (msg) and arbitrary payload.
"""

import sys, json
from datetime import datetime
from functools import partial
from enum import IntEnum

from pytz import utc

from .util import StrJsonEncoder

class Level(IntEnum):
    DEBUG = 0
    INFO = 1
    WARNING = 2
    ERROR = 3

class Logger:
    def __init__ (self, consumer=[], bindings={}):
        self.bindings = bindings
        self.consumer = consumer

    def __call__ (self, level, *args, **kwargs):
        if not isinstance (level, Level):
            level = Level[level.upper ()]
        kwargs['level'] = level
        if args:
            if len (args) == 1:
                args, = args
            kwargs['msg'] = args
        # do not overwrite arguments
        for k, v in self.bindings.items ():
            if k not in kwargs:
                kwargs[k] = v
        for c in self.consumer:
            kwargs = c (**kwargs)
        return kwargs

    def __getattr__ (self, k):
        """ Bind all method names to level, so Logger.info, Logger.warning, … work """
        return partial (self.__call__, k)

    def bind (self, **kwargs):
        d = self.bindings.copy ()
        d.update (kwargs)
        # consumer is not a copy intentionally, so attaching to the parent
        # logger will attach to all children as well
        return self.__class__ (consumer=self.consumer, bindings=d)

    def unbind (self, **kwargs):
        d = self.bindings.copy ()
        for k in kwargs.keys ():
            del d[k]
        return self.__class__ (consumer=self.consumer, bindings=d)

    def connect (self, consumer):
        self.consumer.append (consumer)

    def disconnect (self, consumer):
        self.consumer.remove (consumer)

class Consumer:
    def __call__ (self, **kwargs): # pragma: no cover
        raise NotImplementedError ()

class NullConsumer (Consumer):
    def __call__ (self, **kwargs):
        return kwargs

class PrintConsumer (Consumer):
    """
    Simple printing consumer
    """
    def __call__ (self, **kwargs):
        sys.stderr.write (str (kwargs))
        sys.stderr.write ('\n')
        sys.stderr.flush ()
        return kwargs

class JsonPrintConsumer (Consumer):
    def __init__ (self, minLevel=Level.INFO):
        self.minLevel = minLevel

    def __call__ (self, **kwargs):
        if kwargs['level'] >= self.minLevel:
            json.dump (kwargs, sys.stdout, cls=StrJsonEncoder)
            sys.stdout.write ('\n')
            sys.stdout.flush ()
        return kwargs

class DatetimeConsumer (Consumer):
    def __call__ (self, **kwargs):
        kwargs['date'] = datetime.utcnow ().replace (tzinfo=utc)
        return kwargs

class WarcHandlerConsumer (Consumer):
    def __init__ (self, warc, minLevel=Level.INFO):
        self.warc = warc
        self.minLevel = minLevel

    def __call__ (self, **kwargs):
        if kwargs['level'] >= self.minLevel:
            self.warc._writeLog (json.dumps (kwargs, cls=StrJsonEncoder))
        return kwargs