This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
47 lines (39 loc) · 2.07 KB
/
app.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
from twisted.logger import FileLogObserver, FilteringLogObserver, globalLogPublisher, InvalidLogLevelError, \
Logger, LogLevel, LogLevelFilterPredicate
from twisted.python.logfile import DailyLogFile
from heufybot.bot import HeufyBot
from heufybot.utils.logutils import consoleLogObserver, logFormat
from signal import signal, SIGINT
import argparse
if __name__ == "__main__":
# Parse the command line arguments
parser = argparse.ArgumentParser(description="A modular Twisted IRC bot.")
parser.add_argument("-c", "--config", help="The configuration file to use", type=str, default="heufybot.yaml")
parser.add_argument("-f", "--logfile", help="The file the log will be written to", type=str, default="heufybot.log")
parser.add_argument("-l", "--loglevel", help="The logging level the bot will use", type=str, default="INFO")
options = parser.parse_args()
# Start the bot
heufybot = HeufyBot(options.config)
# Determine the logging level
logFilter = LogLevelFilterPredicate()
try:
logFilter.setLogLevelForNamespace("heufybot", LogLevel.levelWithName(options.loglevel.lower()))
invalidLogLevel = False
except InvalidLogLevelError:
logFilter.setLogLevelForNamespace("heufybot", LogLevel.info)
invalidLogLevel = True
# Set up logging
logFile = DailyLogFile("heufybot.log", "")
fileObserver = FileLogObserver(logFile, logFormat)
fileFilterObserver = FilteringLogObserver(fileObserver, (logFilter,))
consoleFilterObserver = FilteringLogObserver(consoleLogObserver, (logFilter,))
heufybot.log = Logger("heufybot")
globalLogPublisher.addObserver(fileFilterObserver)
globalLogPublisher.addObserver(consoleFilterObserver)
heufybot.log.info("Starting bot...")
# Yell at the user if they specified an invalid log level
if invalidLogLevel:
heufybot.log.warn("Picked up invalid log level {invalidLevel}, level has been set to INFO instead.",
invalidLevel=options.loglevel.lower())
signal(SIGINT, lambda signal, stack: heufybot.shutdown())
heufybot.startup()