Logging

Game server logs can be largely divided into 1) logs for debugging, and 2) user activity logs for customer support. The reason we distinguish the two is because the former has much more data but doesn’t need to be stored in a DB and only needs to be looked into when there is a problem with the server. The latter has comparatively less data but needs to be kept in persistent storage like a DB. iFun Engine supports both Debugging logs and player activity log.

Debug logger

For debugging logs, iFun Engine uses Google glog. For details on Glog, see the glog document. Simple usage is as below.

Google logging usage example

void some_function() {
  // Supported log levels: INFO, WARNING, ERROR, FATAL
  LOG(INFO) << "This is written to the INFO log. "
            << "You do not need to add a newline at the end.";
  LOG(WARNING) << "This is written to the WARNING and INFO log.";
  LOG(ERROR) << "This is written to the ERROR, WARNING, and INFO log.";

  // DLOG that operates only in debug mode.
  DLOG(INFO) << "If you use DLOG instead of LOG, the log is written only if"
             << " NDEBUG is not defined.";
}

The glog in iFun Engine is created in /var/log/funapi by default. If you wish to change the location of the log files, change the settings for log_root of the Logging component. (For overriding the settings of a Component, see the MANIFEST.json section.)

To display the log in the console instead of writing to a file, use the logtostderr glog flag. This is useful during development and testing.

$./my_server-local --logtostderr

Activity logger

The activity log of iFun Engine is a log for player activity in a game, and may be used for evidence in future customer support. Thus, we assume that it is persisted in a DB or other persistent storage.

The player activity that needs to be logged is different for each game, so in iFun Engine we take the approach of describing the activities to be logged in a JSON file and generating the Logger functions for those activities. Then, the server uses the corresponding Logger to write the log. The activity log file is managed separately from the debugging log. If you use iFun Cloud to run your games, iFun Cloud collects the activity log files and stores them in a database.

If you used funapi_initiator to start a project, the JSON file for defining the logger is created. For example, if you make a project named MyServer, the logger definition file is MyServer_logger.json. The structure of the Logger definition file is as below.

Activity logger description file structure (e.g., MyServer_logger.json)

{
  "PlayerDropItem": {
    "player_name": "string",
    "item_name": "string"
  }
}

If defined as above, the following C++ function is generated when compiled.

The generated C++ logger function for the Activity logger

// The logger functions are generated inside a namespace called logger.
namespace logger {

void PlayerDropItem(string &player_name, string &item_name);

}  // namespace logger

Now, whenever a player drops an item, the server can call logger::PlayerItemDrop().

Location for the Activity logger logs

You can set the activity log location using MANIFEST.json. The following is an example MANIFEST.json.

"Logging": {
  "activity_log_output": "json://activity_log/test",
  "activity_log_rotation_interval": 60
}

In the example above, the log location changes depending on the value of activity_log_output. The following values are possible.

  • stdout: Log to standard output.

  • stderr: Log to standard error.

  • glog: Write the log mixed with the glog created by the program log.

  • file://path/to/filename: Under the directory path/to/date, create files of the form filename.time, and write each field separated by a comma in a line. Here, path/to/date is affected by the log_root setting mentioned above. In other words, it will be a sub-directory of log_root.

  • json://path/to/file: Similar to file except that the log is in JSON format instead of CSV.

  • syslog://procname: Write to the INFO level syslog. Here, use the program name for procname. This feature is available only for Linux.

  • mongodb://user:passwd@host:port/database: Writes to a specific MongoDB collection. For details, please refer to Writing Activity Logs to MongoDB.

Writing Activity Logs to MongoDB

Important

MongoDB logger is provided as a separte package. So, please install the package as follows:

# For Ubuntu
sudo apt-get install libfunapi1-mongodblogger

# For CentOS
sudo yum install funapi1-mongodblogger

Add the MondoDbBase component in your MANIFEST.json like this:

"Logging": {
  "activity_log_output": "mongodb://localhost:27017/activity_log",
},
"MongoDbBase": {
}
  • activity_log_output: It should be a MongoDB connection string in the form of mongodb://user:passwd@host:port/database. (user, password, and port are optional.) You can also specify replica set here. For details about the connection string, please see MongoDB CXX Driver’s Connection String

  • MongoDbBase: This component is mandatory to initialize MongoDB driver initialization.

Now you can see iFun Engine writing activity logs into the MongoDB collection.