forked from nathaniel-ortiz/redditswapbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_conf.py
53 lines (41 loc) · 1.58 KB
/
log_conf.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
import sys, os
from ConfigParser import SafeConfigParser
import logging
import mySQLHandler
# load config file
containing_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
cfg_file = SafeConfigParser()
path_to_cfg = os.path.join(containing_dir, 'config.cfg')
cfg_file.read(path_to_cfg)
logging_dest = cfg_file.get('logging','dest')
mysql_hostname = cfg_file.get('mysql', 'hostname')
mysql_username = cfg_file.get('mysql', 'username')
mysql_password = cfg_file.get('mysql', 'password')
mysql_database = cfg_file.get('mysql', 'database')
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances.keys():
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class LoggerManager(object):
__metaclass__ = Singleton
_loggers = {}
def __init__(self, *args, **kwargs):
pass
@staticmethod
def getLogger(name=None):
#configure logging
LoggerManager._loggers[name] = logging.getLogger(name)
LoggerManager._loggers[name].setLevel(logging.INFO)
if logging_dest == 'mysql':
db = {'host':mysql_hostname, 'port':3306, 'dbuser':mysql_username, 'dbpassword':mysql_password, 'dbname':mysql_database}
sqlh = mySQLHandler.mySQLHandler(db)
LoggerManager._loggers[name].addHandler(sqlh)
else:
fileh = logging.FileHandler('actions.log')
fileh.setFormatter(logging.Formatter('%(asctime)s - %(module)s - %(message)s'))
LoggerManager._loggers[name].addHandler(fileh)
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)
return LoggerManager._loggers[name]