edit-icon download-icon

Unity3D logs

Last Updated: Mar 06, 2018

Unity3D is an integrated game development tool compatible with multiple platforms. Developed by Unity Technologies, this tool allows a player to easily create various interactive contents such as 3D video game, architectural visualization, and real-time 3D animation. Unity3D is a fully integrated and professional game engine.

You can use the Web Tracking function of Log Service to collect Unity3D logs conveniently. This document introduces how to use the Web Tracking function to collect Unity logs to Log Service by collecting the Unity Debug.Log.

1 Activate the Web Tracking function

For more information, see Web Tracking.

2 Register Unity3D LogHandler

Create a C# file LogOutputHandler.cs in the Unity editor. Enter the following codes and modify three member variables in the codes, which are:

  • project, indicating the name of the log project.
  • logstore, indicating the name of the Logstore.
  • serviceAddr, indicating the address of the log project. For more information, see Service endpoint.
  1. using UnityEngine;
  2. using System.Collections;
  3. public class LogOutputHandler : MonoBehaviour
  4. {
  5. //Register the HandleLog function on scene start to fire on debug.log events
  6. public void OnEnable()
  7. {
  8. Application.logMessageReceived += HandleLog;
  9. }
  10. //Remove callback when object goes out of scope
  11. public void OnDisable()
  12. {
  13. Application.logMessageReceived -= HandleLog;
  14. }
  15. string project = "your project name";
  16. string logstore = "your logstore name";
  17. string serviceAddr = "http address of your log service project";
  18. //Capture debug.log output, send logs to Loggly
  19. public void HandleLog(string logString, string stackTrace, LogType type)
  20. {
  21. string parameters = "";
  22. parameters += "Level=" + WWW.EscapeURL(type.ToString());
  23. parameters += "&";
  24. parameters += "Message=" + WWW.EscapeURL(logString);
  25. parameters += "&";
  26. parameters += "Stack_Trace=" + WWW.EscapeURL(stackTrace);
  27. parameters += "&";
  28. //Add any User, Game, or Device MetaData that would be useful to finding issues later
  29. parameters += "Device_Model=" + WWW.EscapeURL(SystemInfo.deviceModel);
  30. string url = "http://" + project + "." + serviceAddr + "/logstores/" + logstore + "/track?APIVersion=0.6.0&" + parameters;
  31. StartCoroutine(SendData(url));
  32. }
  33. public IEnumerator SendData(string url)
  34. {
  35. WWW sendLog = new WWW(url);
  36. yield return sendLog;
  37. }
  38. }

The preceding codes can asynchronously send logs to Alibaba Cloud Log Service. You can add more fields that you want to collect in the example.

3 Generate Unity logs

In the project, create the LogglyTest.cs file and add the following codes:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class LogglyTest : MonoBehaviour {
  4. void Start () {
  5. Debug.Log ("Hello world");
  6. }
  7. }

4 Preview logs in console

After completing the preceding steps, run the Unity program. Then, you can preview your sent logs in the Log Service console. For how to preview logs, see Preview logs.

The preceding example provides the methods for collecting logs such as Debug.Log, Debug.LogError, and Debug.LogException. The component object model of Unity, its program crash API, and other types of Log APIs can be used to conveniently collect the device information on the client.

Thank you! We've received your feedback.