Debug Logger not working on builds (only works in editor), how can I enable logging in builds?

Hello

I have tried a couple solutions to getting logging to work in a build but for some reason it’s not working. The latest thing I’ve tried was this script I found online. The code works just fine in the editor. If I press play I can see the logs and it saves it to a file

using UnityEngine;

public class X_LogReporter : MonoBehaviour
{
    string myLog = "*begin log";
    string filename = "";
    bool doShow = true;
    int kChars = 700;
    void OnEnable() { Application.logMessageReceived += Log; }
    void OnDisable() { Application.logMessageReceived -= Log; }
    void Update() {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            doShow = !doShow;
        }
    }
    public void Log(string logString, string stackTrace, LogType type)
    {
        // for onscreen...
        myLog = myLog + "\n" + logString;
        if (myLog.Length > kChars) { myLog = myLog.Substring(myLog.Length - kChars); }

        // for the file ...
        if (filename == "")
        {
            string d = System.Environment.GetFolderPath(
               System.Environment.SpecialFolder.Desktop) + "/GRRBLS_LOG";
            System.IO.Directory.CreateDirectory(d);
            string r = Random.Range(1000, 9999).ToString();
            filename = d + "/log-" + r + ".txt";
        }
        try { System.IO.File.AppendAllText(filename, logString + "\n"); }
        catch { }
    }

    void OnGUI()
    {
        if (!doShow) { return; }
        GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,
           new Vector3(Screen.width / 1200.0f, Screen.height / 800.0f, 1.0f));
        GUI.TextArea(new Rect(10, 10, 540, 370), myLog);
    }
}

In the editor the log shows up fine:

8480441--1127453--21.png

When I build and run the game, it stops working

Any suggestions on what I might be missing or where to look. All my research indicates Debug.Log should work in builds and I can’t see what I might have wrong.

I can understand the logging GUI, but writing to a file is not necessary since you get these written to the player log anyway: Unity - Manual: Log files

That’s also where you should check for any issues related to logging. Or, in case you haven’t checked the development build flag in build settings, enable that. If there are any exceptions you’ll see them in the build.

PS: From the try/catch I get the impression you already ran into the various exceptions that can occur when writing too much or in parallel. :wink:

I understand there is a log file, however I am curious why the logs aren’t being received in builds (even with development build toggled) only in the editor

Received as in displayed in your custom console? That I don’t know.

You can try sending out a Debug.LogError(“test”) somewhere. In a development build this will make the built-in error console appear. If that works, and any Debug.Log() statement is also printed in the player .log file then the logging system is generally working and any issues must be with your custom console.

To debug something like this in the player it can be easiest to just insert Debug.LogError to see what is being called and where logs are missing to narrow down the issue.

Yea I have debug logs (with error logs right away too) that fire as soon as the game launches but for some reason the logger in a build does not log anything. If I go check the log file location its empty. So both my custom log interceptor and the log file are not getting written to in builds, but they are if its in Play mode in the editor

Ok great news the issue was the root logger was checking if the application was in EditorMode before doing the log.