-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasiclogger.py
43 lines (32 loc) · 1.26 KB
/
basiclogger.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
class pyLogger:
""" A basic python logger using logging
Logs to the file passed as argument
Formats each log entry whith the date, the module name, and the level
"""
def __init__(self, logname, level='INFO'):
""" Gets the filename and the level """
loglevel = logging.NOTSET
if level == 'CRITICAL':
loglevel = logging.CRITICAL
elif level == 'ERROR':
loglevel = logging.ERROR
elif level == 'WARNING':
loglevel = logging.WARNING
elif level == 'INFO':
loglevel == logging.INFO
elif level == 'DEBUG':
loglevel = logging.DEBUG
# Argumentos del logger. Por defecto el nivel debería ser INFO
self.log = logging.getLogger(logname)
if not len(self.log.handlers):
self.log = logging.Logger(logname)
handler = logging.FileHandler(logname)
handler.setLevel(loglevel)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.log.propagate = False
self.log.setLevel(loglevel)