Logging in Unity3D

 

Logging In Unity3D Header

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.

Logging In Unity3D 1

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.

Logging In Unity3D 2

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


5 comments

  • Kareem

    4 months ago

    Thank you for this article. Are there any solutions to only send the entire log (or all log events) after a user has, for example, pushed a “report a bug” button in the app? Enabling constant logging is exceeding our data limits tremendously.

    • Linda Somme

      3 months ago

      There are ways to filter your logs before sending them to Loggly. Check out the Ultimate Guide to Logging for some general logging tips:
      https://www.loggly.com/ultimate-guide/

  • Maz Jaleel

    4 months ago

    Thanks for the post. I’m looking into integrating a logging aggregator for my Unity game, but this doesn’t look right to me at all. Services I’ve used in the past, like Firebase and Papertrail app, establish socket connections for maximum efficiency. The proposed solution here would result in too many request response cycles. I hope you would consider a better approach in the future…

    Best,
    Maz

    • Linda Somme

      3 months ago

      Maz,
      Thanks for the comment. We are always looking to improve Loggly. If you do write something, please let us know.

  • Joe

    10 months ago

    Where’s 2 & 3?

    2. Use an extended version of Log4net
    3. Use one of Unity’s existing logging plugins with a customized Loggly appender.

Share Your Thoughts

Top