forked from bobrathbone/piradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_class.py
122 lines (104 loc) · 2.76 KB
/
log_class.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
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
#!/usr/bin/env python
#
# $Id: log_class.py,v 1.7 2015/01/24 10:29:14 bob Exp $
# Raspberry Pi Internet Radio
# Logging class
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
# Modified to use /etc/radiod.conf from 3.15 onwards
#
# Log levels are :
# CRITICAL 50
# ERROR 40
# WARNING 30
# INFO 20
# DEBUG 10
# NOTSET 0
#
# See https://docs.python.org/2/library/logging.html
#
import os
import logging
import ConfigParser
config = ConfigParser.ConfigParser()
ConfigFile = "/etc/radiod.conf"
class Log:
CRITICAL = logging.CRITICAL
ERROR = logging.ERROR
WARNING = logging.WARNING
INFO = logging.INFO
DEBUG = logging.DEBUG
NONE = 0
module = '' # Module name for log entries
loglevel = logging.INFO
def __init__(self):
return
def init(self,module):
self.module = module
self.loglevel = self.getConfig()
return
def message(self,message,level):
# Set up logging, level
if level != self.NONE:
logger = logging.getLogger('gipiod')
hdlr = logging.FileHandler('/var/log/' + self.module + '.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(self.loglevel)
# write to log
if level == self.CRITICAL:
logger.critical(message)
elif level == self.ERROR:
logger.error(message)
elif level == self.WARNING:
logger.warning(message)
elif level == self.INFO:
logger.info(message)
elif level == self.DEBUG:
logger.debug(message)
logger.removeHandler(hdlr)
hdlr.close()
return
# Temporary set log level
def setLevel(self,level):
self.loglevel = level
return
# Get the log level from the configuration file
def getLevel(self):
return self.loglevel
# Get configuration loglevel option
def getConfig(self):
section = 'RADIOD'
option = 'loglevel'
strLogLevel = 'INFO'
# Get loglevel option
config.read(ConfigFile)
try:
strLogLevel = config.get(section,option)
except ConfigParser.NoSectionError:
msg = ConfigParser.NoSectionError(section),'in',ConfigFile
self.message(msg,self.ERROR)
if strLogLevel == "CRITICAL":
loglevel = self.CRITICAL
elif strLogLevel == "ERROR":
loglevel = self.ERROR
elif strLogLevel == "WARNING":
loglevel = self.WARNING
elif strLogLevel == "INFO":
loglevel = self.INFO
elif strLogLevel == "DEBUG":
loglevel = self.DEBUG
elif strLogLevel == "NONE":
loglevel = self.NONE
else:
loglevel = self.INFO
return loglevel
# End of log class