Is there a way to log every message in socket.io chat site to a file?

Well, as shown on the title, I’m working on a chatting website and I think it would just be a step up to add a Moderation Logging by text file. It would work something like this:

(I want every message to log incase someone says that they never said that we could just ban them cause we have solid proof, of course all USERS WOULD KNOW that their messages are being recorded.)

  • Message sent on site
  • Message that was sent is logged word for word inside messagelogs.txt

Thank you for helping me out!

Yeah, basically just

fs.promises.appendFile("./path/to/file", messageContent)

Will append to an existing file or create one if it doesn’t exist. The file will get massive but that doesn’t matter very much, you’re not reading it (you’d just d/l it and grep through it locally at some point when you needed it). Can just check the size before appending and if it’s bigger than {some number of Mb} then start appending to a new file. Edit: also, include the date and time the file was created in the filename to help preserve your sanity.

An alternative I would consider is to use winston and log the messages through the system logging facilities, assuming the use of a unix-like OS. The setup in your node app is almost as easy as writing to a file.

With winston, you could log whatever info (message, username, IP, timestamps, etc.) in JSON (or some other structured format) and feed that into your standard logging utilities to do simple stuff like rotation, compression, and archival or you could feed it into elasticsearch, kibana, and friends (or others) and do all kinds of analysis.

Regardless of which you do, write structured data (like JSON). Your future self will thank your current self when the data processing starts.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.