forked from DesertBot/DesertBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
54 lines (43 loc) · 1.84 KB
/
start.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
import argparse
import logging
import os
import sys
from twisted.internet import reactor
from twisted.python import log
from desertbot.config import Config, ConfigError
from desertbot.factory import DesertBotFactory
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='An IRC bot written in Python.')
parser.add_argument('-c', '--config',
help='the config file to read from',
type=str, required=True)
parser.add_argument('-l', '--loglevel',
help='the logging level (default INFO)',
type=str, default='INFO')
cmdArgs = parser.parse_args()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Set up logging for stdout on the root 'desertbot' logger
# Modules can then just add more handlers to the root logger
# to capture all logs to files in various ways
rootLogger = logging.getLogger('desertbot')
numericLevel = getattr(logging, cmdArgs.loglevel.upper(), None)
if isinstance(numericLevel, int):
rootLogger.setLevel(numericLevel)
else:
raise ValueError('Invalid log level {}'.format(cmdArgs.loglevel))
logFormatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',
'%H:%M:%S')
streamHandler = logging.StreamHandler(stream=sys.stdout)
streamHandler.setFormatter(logFormatter)
rootLogger.addHandler(streamHandler)
observer = log.PythonLoggingObserver(loggerName='desertbot.twisted')
observer.start()
config = Config(cmdArgs.config)
try:
config.loadConfig()
except ConfigError:
rootLogger.exception("Failed to load configuration file {}".format(cmdArgs.config))
else:
factory = DesertBotFactory(config)
reactor.run()
sys.exit(factory.exitStatus)