All Products
Search
Document Center

Simple Log Service:Collect Unity3D logs

Last Updated:Apr 09, 2024

This topic describes how to use the web tracking feature of Simple Log Service to collect Unity3D logs.

Background information

Unity3D is a cross-platform game engine that is developed by Unity Technologies. The engine allows you to create interactive content such as 3D video games, VR buildings, and real-time 3D animations.

Simple Log Service allows you to use the web tracking feature to collect Unity3D logs. For more information about the web tracking feature, see Use the web tracking feature to collect logs. In this topic, Unity Debug.Log is used as an example to describe how to collect Unity3D logs.

Procedure

  1. Enable the web tracking feature. For more information, see Use the web tracking feature to collect logs.

  2. Create a Unity3D logging handler.

    In the Unity editor, create a C# file that is named LogOutputHandler.cs, add the following script to the file, and modify the following variables:

    • project: the name of the Simple Log Service project that you want to manage. For more information, see Manage a project.

    • logstore: the name of the Logstore that you want to manage. For more information, see Manage a Logstore.

    • serviceAddr: the endpoint of the Simple Log Service project. For more information, see Endpoints.

    using UnityEngine;
    using System.Collections;
    public class LogOutputHandler : MonoBehaviour
    {
        //Register the HandleLog function on scene start to fire on debug.log events
        public void OnEnable()
        {
            Application.logMessageReceived += HandleLog;
        }
        //Remove callback when object goes out of scope
        public void OnDisable()
        {
            Application.logMessageReceived -= HandleLog;
        }
        string project = "your project name";
        string logstore = "your logstore name";
        string serviceAddr = "http address of your log service project";
        //Capture debug.log output, send logs to Loggly
        public void HandleLog(string logString, string stackTrace, LogType type)
        {
            string parameters = "";
            parameters += "Level=" + WWW.EscapeURL(type.ToString());
            parameters += "&";
            parameters += "Message=" + WWW.EscapeURL(logString);
            parameters += "&";
            parameters += "Stack_Trace=" + WWW.EscapeURL(stackTrace);
            parameters += "&";
            //Add any User, Game, or Device MetaData that would be useful to finding issues later
            parameters += "Device_Model=" + WWW.EscapeURL(SystemInfo.deviceModel);
            string url = "http://" + project + "." + serviceAddr + "/logstores/" + logstore + "/track?APIVersion=0.6.0&" + parameters;
            StartCoroutine(SendData(url));
        }
        public IEnumerator SendData(string url)
        {
            WWW sendLog = new WWW(url);
            yield return sendLog;
        }
    }

    You can use the preceding script to asynchronously send logs to Simple Log Service. You can also specify other fields in the script to collect the fields.

  3. Generate Unity3D logs.

    Create a file that is named LogglyTest.cs, and add the following script to the file:

    using UnityEngine;
    using System.Collections;
    public class LogglyTest : MonoBehaviour {
        void Start () {
            Debug.Log ("Hello world");
        }
    }
  4. View the collected logs.

    After you run your Unity3D application, logs are generated and sent to Simple Log Service. You can view the logs in the Simple Log Service console.