disable logging by default but allow enabling bot/server-wide

This commit is contained in:
jesopo 2020-01-27 23:51:30 +00:00
parent 3b55e00dd0
commit b5c068a0ce

View file

@ -7,16 +7,25 @@ from src import ModuleManager, utils
ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
LOGS_DIRECTORY = os.path.join(ROOT_DIRECTORY, "logs") LOGS_DIRECTORY = os.path.join(ROOT_DIRECTORY, "logs")
@utils.export("channelset", utils.BoolSetting("log", SETTING = utils.BoolSetting("channel-log",
"Enable/disable channel logging")
@utils.export("channelset",utils.BoolSetting("log",
"Enable/disable channel logging")) "Enable/disable channel logging"))
@utils.export("serverset", SETTING)
@utils.export("botset", SETTING)
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _enabled(self, server, channel):
return channel.get_setting("log",
server.get_setting("channel-log",
self.bot.get_setting("channel-log", False)))
def _file(self, server_name, channel_name): def _file(self, server_name, channel_name):
return self.data_directory("%s/%s.log" % (server_name, channel_name)) return self.data_directory("%s/%s.log" % (server_name, channel_name))
def _log(self, event, channel): def _log(self, server, channel, line):
if channel.get_setting("log", True): if self._enabled(server, channel):
with open(self._file(str(event["server"]), str(channel)), "a") as log: with open(self._file(str(server), str(channel)), "a") as log:
timestamp = datetime.datetime.now().strftime("%x %X") timestamp = datetime.datetime.now().strftime("%x %X")
log.write("%s %s\n" % (timestamp, event["line"])) log.write("%s %s\n" % (timestamp, line))
@utils.hook("formatted.message.channel") @utils.hook("formatted.message.channel")
@utils.hook("formatted.notice.channel") @utils.hook("formatted.notice.channel")
@ -33,7 +42,8 @@ class Module(ModuleManager.BaseModule):
@utils.hook("formatted.chghost") @utils.hook("formatted.chghost")
def on_formatted(self, event): def on_formatted(self, event):
if event["channel"]: if event["channel"]:
self._log(event, event["channel"]) self._log(event["server"], event["channel"], event["line"])
elif event["user"]: elif event["user"]:
for channel in event["user"].channels: for channel in event["user"].channels:
self._log(event, channel) self._log(event["server"], channel, event["line"])