-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
54 lines (47 loc) · 2.17 KB
/
utils.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
# Copyright 2015 Pietro Bertera <[email protected]>
#
# This work is based on the https://github.com/tirfil/PySipProxy
# from Philippe THIRION.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
def setup_logger(logger_name, log_file=None, debug=False, str_format='%(asctime)s %(levelname)s %(message)s', handler=None):
"""Register a logging instance with name `logger_name`
Args:
logger_name (str): the logger instance name, you can retreive the instance using `logging.getLogger("logger_name")`
log_file (str, optional): if defined a `logging.FileHandler` will be used, default `None`
debug (bool, optional): if `True` the logger level will be `logging.DEBUG` else `logging.INFO`
str_format (str, optional): the logger format string, default is '%(asctime)s %(levelname)s %(message)s'
handler (logging.Handler, optional): if present the handler will be added to the logger
Returns: the ``logging.Logger` instance
"""
l = logging.getLogger(logger_name)
if debug == True:
l.setLevel(logging.DEBUG)
else:
l.setLevel(logging.INFO)
formatter = logging.Formatter(str_format)
if handler:
handler.setFormatter(formatter)
l.addHandler(handler)
return l
elif log_file:
fileHandler = logging.FileHandler(log_file, mode='w')
fileHandler.setFormatter(formatter)
l.addHandler(fileHandler)
else:
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
l.addHandler(streamHandler)
return l