-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.py
94 lines (72 loc) · 2.63 KB
/
logger.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'Tzury Bar Yochay'
__version__ = '0.1'
__license__ = 'GPLv3'
import logging
from logging import handlers
from theme import default_theme as theme
__all__ = ['log', 'cdr_logger']
'''
levels: 'NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0
'''
send_to_socket = None
class Logger:
def __init__(self):
self.file_frmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
self.stream_frmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
self.logger = logging.getLogger('snoip.freespeech')
self.logger.setLevel(logging.INFO)
# file handler
self.fh = logging.FileHandler('snoip.freespeech.log')
# stream handler
self.ch = logging.StreamHandler()
# socket handler
#self.sh = handlers.SocketHandler('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)
self.ch.setFormatter(self.stream_frmt)
self.fh.setFormatter(self.file_frmt)
self.logger.addHandler(self.ch)
self.logger.addHandler(self.fh)
#self.logger.addHandler(self.sh)
def debug(self, *args):
self.logger.debug(
theme.style_prompt
+ ''.join(str(i) for i in args)
+ theme.style_normal)
def info(self, *args):
self.logger.info(
theme.style_yellow
+ ''.join(str(i) for i in args)
+ theme.style_normal)
def exception(self, *args):
self.logger.exception(
theme.style_fail
+ ''.join(str(i) for i in args)
+ theme.style_normal)
def warning(self, *args):
self.logger.warning(
theme.style_right
+ ''.join(str(i) for i in args)
+ theme.style_normal)
class CDRLogger:
def __init__(self):
self.file_frmt = logging.Formatter('%(asctime)s,%(message)s')
self.file_handler = \
logging.handlers.TimedRotatingFileHandler('cdr.log', 'D')
self.file_handler.setFormatter(self.file_frmt)
self.logger = logging.getLogger('snoip.freespeech.cdr')
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(self.file_handler)
def writeline(self, rec):
record = ','.join(
map(str, (rec.caller_ctx, rec.callee_ctx, rec.start_time,
rec.answer_time, rec.end_time)))
self.logger.debug(record)
log = Logger()
cdr_logger = CDRLogger()