Skip to content

Commit

Permalink
Log chat history
Browse files Browse the repository at this point in the history
Log all chat messages using the logging framework.
  • Loading branch information
kh31d4r authored and mosbth committed Oct 15, 2024
1 parent e438975 commit 4510b58
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .docker/marvin/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM python:3-alpine

COPY . /app
RUN mkdir -p /app
RUN mkdir -p /app/logs
WORKDIR /app

RUN python3 -m pip install -r .requirements.txt
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aggregate.error
aggregate.ignore
aggregate.log
db.sqlite
logs/
# pycharm
.idea/
marvin_config.json
Expand Down
1 change: 1 addition & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self):
self.CONFIG = {}
self.ACTIONS = []
self.GENERAL_ACTIONS = []
self.MSG_LOG = logging.getLogger("message")

def getConfig(self):
"""Return the current configuration"""
Expand Down
3 changes: 1 addition & 2 deletions discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from bot import Bot

LOG = logging.getLogger("bot")

class DiscordBot(discord.Client, Bot):
"""Bot implementing the discord protocol"""
Expand Down Expand Up @@ -46,7 +45,7 @@ async def checkMarvinActions(self, message):

async def on_message(self, message):
"""Hook run on every message"""
LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content)
self.MSG_LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content)
if message.author.name == self.user.name:
# don't react to own messages
return
Expand Down
7 changes: 6 additions & 1 deletion irc_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ def sendPrivMsg(self, message, channel):
"""Send and log a PRIV message"""
if channel == self.CONFIG["channel"]:
self.ircLogAppend(user=self.CONFIG["nick"].ljust(8), message=message)
self.MSG_LOG.debug("%s <%s> %s", channel, self.CONFIG["nick"], message)

msg = "PRIVMSG {CHANNEL} :{MSG}\r\n".format(CHANNEL=channel, MSG=message)
self.sendMsg(msg)

def sendMsg(self, msg):
"""Send and occasionally print the message sent"""
LOG.info("SEND: %s", msg.rstrip("\r\n"))
LOG.debug("SEND: %s", msg.rstrip("\r\n"))
self.SOCKET.send(msg.encode())

def decode_irc(self, raw, preferred_encs=None):
Expand Down Expand Up @@ -224,6 +225,10 @@ def checkIrcActions(self, words):
def checkMarvinActions(self, words):
"""Check if Marvin should perform any actions"""
if words[1] == 'PRIVMSG' and words[2] == self.CONFIG["channel"]:
self.MSG_LOG.debug("%s <%s> %s",
words[2],
words[0].split(":")[1].split("!")[0],
" ".join(words[3:]))
self.ircLogAppend(words)

if words[1] == 'PRIVMSG':
Expand Down
24 changes: 23 additions & 1 deletion logging.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,41 @@
"standard": {
"format": "%(asctime)s %(threadName)s %(levelname)s %(module)s - %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%S%z"
},
"message": {
"format": "%(asctime)s %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%S%z"
}
},
"handlers": {
"stdout": {
"class": "logging.StreamHandler",
"formatter": "standard",
"stream": "ext://sys.stdout"
},
"message": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "message",
"filename": "logs/messages.log",
"maxBytes": 10485760,
"backupCount": 10
}
},
"loggers": {
"root": {
"level": "DEBUG",
"level": "DEBUG"
},
"main": {
"handlers": ["stdout"]
},
"bot": {
"handlers": ["stdout"]
},
"action": {
"handlers": ["stdout"]
},
"message": {
"handlers": ["message"]
}
}
}
Empty file added logs/.gitkeep
Empty file.

0 comments on commit 4510b58

Please sign in to comment.