简体   繁体   中英

Best practices for writing a log for a Java server

I'm looking for some advice on best/common practices with writing a log for a game server I wrote in Java (first time with server/client concept and I've never done anything with logging). I've tried some Googling, but the keywords involved are kind of vague and not coming up with any answers to my questions.

I have a server with users, game lobbies, and games. I think that I should create a log to keep a history of everything that happens on the server. And in this log I think I should record status changes of both the server itself (startup, shutdown, commands being processed), and socket connections (new/lost clients, incoming/outgoing messages).

I'm not sure if that's the best thing to do, or instead/also have a log with more human readable messages like "Bob781 joined lobby #4." or "microman12 beat sun44 in a game."

  • Should I write logs to the console or to a file? (2 separate files?)
  • What formats are best or most commonly used? Should they be the same formats?
  • I've come across the " Common Log Format ", is this the best format choice for socket connections? Can it be used for the server statuses?
  • Should I use something in java.util.logging or just output the strings myself?

You always want to look for existing solutions before starting from scratch. When you use/create the logging, I would suggest the following tips based on my experience

  1. Make sure you can reliable retrieve the logs whenever you might need them.
  2. Make it easy to find a log file based on what you're looking for. For example if there is a problem with a specific game lobby, you should be able to easily navigate to the file or directory of files that pertain to that game lobby in question. Nothing worse than having to parse a huge gigantic log file...
  3. Make each logging message meaningful. Don't just log stuff for the sake of logging. You will want to be able to read a log message and know relatively where in the server code that message is coming from. This will help later down the road when you run into bugs in your server code.
  4. Have each log message time stamped at the point it's created and not when it's written to the log file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM