Logging in Unity3D
When game developers release games or game updates, it’s not uncommon for unexpected bugs and downtime to occur. Sometimes these result from developer error, but more often they result from unexpected loads, use cases, or data inputs. As a result, they can be quite hard to diagnose.
Too often, developers don’t know about an issue until they notice bug reports, bad reviews, or a negative trend in revenues. When they do discover it, they may not have enough data to know what exactly happened and must instead rely on what their users are telling them in reviews and bug reports to re-create the issue in a development environment. At this point, the issue has likely lasted several days and cost the game a lot of players and direct revenue.
Using Log Management to Obtain Data From Live Unity Clients
A much better solution is to capture live game operation data from your players’ devices and your own servers. Then, you can be immediately informed when something goes wrong and have the data to trace the exact cause.The Unity game engine has several native debugging tools. The Unity console, monodevelop debugger, and profiler are all crucial tools to debugging Unity-based games. However, if issues occur post-release, these debugging tools cannot tell you what your users’ clients are experiencing, so you will be reliant on bug reports to tell you what’s wrong.
In this tutorial, I will describe how to send logs from live end clients to Loggly, giving yourself all the data you need to spot and fix issues before your users even notice them.
Before We Begin
Before beginning this tutorial, create a free Loggly account by clicking here so you can follow along step by step. Once you sign up, you will get a customer token, which will in all examples below will be shown as TOKEN. To make these examples work in your code, simply exchange TOKEN with the customer token you were given when you signed up for Loggly.
Logging in Unity Engine
Logging via RESTful HTTP POST messages
The simplest method of logging to Loggly is sending logs RESTfully to the Loggly HTTP endpoint. Unity has several networking classes, but the most straightforward way to send logs from Unity is to combine the WWWForm & WWW classes. The WWW class is the primary Unity class for sending HTTP communications. The WWWForm class allows you to build JSON-like messages which you can then send with the WWW class, which Loggly is able to parse as JSON.
Let’s see how this works. First, let’s build a simple test WWWForm. Loggly has documentation that you can follow.
But really, getting logs to Loggly is literally that simple.
Logging Debug Output: Sending Debug.Log() Output to Loggly
For app logging and debugging purposes, Unity makes the Debug.Log() function available. It normally sends a log message and the program scope where the log was fired from to the Unity console, but the Application.RegisterLogCallback() function allows you to capture this output and send it to Loggly. Let’s see how to apply this in Unity in a self-contained script.
Now, whenever you use Debug.Log or any of its variants (such as Debug.LogError or Debug.LogException), that log will automatically be sent to Loggly. Note that when sending messages to Loggly, it’s useful to send device and game metadata such as screen resolutions, device models, and unique user/session IDs that can help you isolate issues.
Unity’s component-object model and the existence of several specialized crash & log libraries in the Unity API offer some very useful ways to gather useful system data from any of your game clients that are built with Unity. We will explore these in more detail in future blog posts.
Logging in Unity: What the Logs Look Like in Loggly
After we’ve sent the messages to Loggly, we notice on the left that we now have a list of JSON fields to examine in Dynamic Field Explorer as well as a plot of events throughout time on the top. Assuming that you’re using the method described above to send your logs, any fields will be parsed by Loggly as searchable JSON fields that we can drill down into and examine. All of this happens at log ingestion time, so you don’t have to wait for results every time you log into Loggly or create a search query.
Furthermore, you will be able to visualize all events with custom charts that help you spot trends. You’ll be able to set your own alerts to notify your developers when bugs or other performance issues have occurred, so that they can immediately get to work on them.
Customers tell the team at Loggly that Loggly truly changes the way they go about solving operational problems. But the best way to find out is to try it for yourself!
p.s. You can make use of the logging library above by downloading it from my GitHub account here.
Mike Turner is a partner in Charmed Matter Games, a full pipeline a small development studio founded by several veteran online & mobile online game developers and operators. He has been developing and operating MMO, social & mobile games of varying genres since 2006 and has a large depth of experience in what makes online games sustainable & profitable. He also is well aware of what can kill games and the companies behind them and seeks to educate people how to avoid those mistakes and achieve success. Follow him on twitter: @socialgamesguy