Logging server messages (custom servers) - Amazon GameLift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Logging server messages (custom servers)

You can capture custom server messages from your Amazon GameLift custom servers in log files. To learn about logging for Realtime Servers, see Logging server messages (Realtime Servers).

Important

There is a limit on the size of a log file per game session (see Amazon GameLift endpoints and quotas in the Amazon Web Services General Reference). When a game session ends, Amazon GameLift uploads the server logs to Amazon Simple Storage Service (Amazon S3). Amazon GameLift will not upload logs that exceed the limit. Logs can grow very quickly and exceed the size limit. You should monitor your logs and limit the log output to necessary messages only.

Configuring logging for custom servers

With Amazon GameLift custom servers, you write your own code to perform logging, which you configure as part of your server process configuration. Amazon GameLift uses your logging configuration to identify the files that it must upload to Amazon S3 at the end of each game session.

The following instructions show how to configure logging using simplified code examples:

C++
To configure logging (C++)
  1. Create a vector of strings that are directory paths to game server log files.

    std::string serverLog("serverOut.log"); // Example server log file std::vector<std::string> logPaths; logPaths.push_back(serverLog);
  2. Provide your vector as the LogParameters of your ProcessParameters object.

    Aws::GameLift::Server::ProcessParameters processReadyParameter = Aws::GameLift::Server::ProcessParameters( std::bind(&Server::onStartGameSession, this, std::placeholders::_1), std::bind(&Server::onProcessTerminate, this), std::bind(&Server::OnHealthCheck, this), std::bind(&Server::OnUpdateGameSession, this), listenPort, Aws::GameLift::Server::LogParameters(logPaths));
  3. Provide the ProcessParameters object when you call ProcessReady().

    Aws::GameLift::GenericOutcome outcome = Aws::GameLift::Server::ProcessReady(processReadyParameter);

For a more complete example, see ProcessReady().

C#
To configure logging (C#)
  1. Create a list of strings that are directory paths to game server log files.

    List<string> logPaths = new List<string>(); logPaths.Add("C:\\game\\serverOut.txt"); // Example of a log file that the game server writes
  2. Provide your list as the LogParameters of your ProcessParameters object.

    var processReadyParameter = new ProcessParameters( this.OnGameSession, this.OnProcessTerminate, this.OnHealthCheck, this.OnGameSessionUpdate, port, new LogParameters(logPaths));
  3. Provide the ProcessParameters object when you call ProcessReady().

    var processReadyOutcome = GameLiftServerAPI.ProcessReady(processReadyParameter);

For a more complete example, see ProcessReady().

Writing to logs

Your log files exist after your server process has started. You can write to the logs using any method to write to files. To capture all of your server's standard output and error output, remap the output streams to log files, as in the following examples:

C++
std::freopen("serverOut.log", "w+", stdout); std::freopen("serverErr.log", "w+", stderr);
C#
Console.SetOut(new StreamWriter("serverOut.txt")); Console.SetError(new StreamWriter("serverErr.txt"));

Accessing server logs

When a game session ends, Amazon GameLift automatically stores the logs in an Amazon S3 bucket and retains them for a limited period of time. To get the location of the logs for a game session, you can use the GetGameSessionLogUrl API operation. To download the logs, use the URL that the operation returns.