3

I am developing a game server based on the top of someone else framework, netty and spring.

And I wonder whether it is a good approach to generate a separate file using log4j for every game room or not. Since with many game room servers has to keep open a lot of log files. On the other hand with only one log file it would be a total mess up which would required parsing and filtering when analyzing a problem.

If many log files is still a good approach when I need to know how to implement that. I have had a look at this question configure log4j to log to custom file at runtime, but first answer is not clear, how to minimize lines of code required in every attempt to write to a separate log file. I have tried the second answer with putting in config file

log4j.appender.logfile.File=${logfile.name}

but the framework has

PropertyConfigurator.configure(System.getProperty("log4j.configuration"));

which fails if log file is not specified at the start time.

Any ideas and comments are welcome! Thanks

UPD: This link also seems helpful in my case. Log4j logging to separate files But still: is it good idea and how to switch dynamically the output log file with minimal coding.

Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101

2 Answers2

5

Really, when it comes down to it, having more than one log file for the game rooms would be overkill. The file I/O would have an effect on performance, even if that was negligible. Furthermore, as programmers, we have so many tools at our disposal for parsing and filtering information that it seems much more reasonable to do this after the fact.

For instance, as long as each game room matches a specific pattern when logging, you could easily use grep and the Linux > operator to parse all of the data for game room X and write to another file where you could analyze it separately. Having your production server do this, when you may or may not even be looking at the logs, isn't the best use of resources.

As an example, running this command could quickly get me all the information I need about game room X:

grep "game room x" mainLog.log > outputToAnalyze.log
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
2

I agree with jmort253, but if you really want a file per room create an appender for each:

If the rooms are static just define the appenders on the log4j config file, if not do it at run-time:

That is exactly what the accepted answer from here says:

  SimpleLayout layout = new SimpleLayout();    
  FileAppender appender = new FileAppender(layout,"<room_file_name>",false);    
  logger.addAppender(appender);

Basically, you don't need a configuration file, the code there replaces it and defines the appenders dynamically with their file name, so I recommend:

  1. Write a method to create dynamic appenders that receive the file name as a parameter (and maybe return the logger or also receive a logger and add the appender to it)
  2. Where ever the rooms are being created call that method
  3. Assign the logger with the new appender to each room and use it
Community
  • 1
  • 1
Adolfo
  • 798
  • 1
  • 7
  • 15