- Home /
Is there any way to view the console in a build?
Hello, I always have issues debugging my multilayer games becuase I don't know how to view console debug logs and log-errors in a Unity build.
Is there any way to do this? Or simply a way to access existing debug log messages through runtime scripts?
Thanks.
For standalone builds, the console output is dumped to the file ..._Data/output_log.txt It looks a bit more unintuitive than the editor's console log, since for every message printed, the callstack is also dumped.
Or you could use http://wiki.unity3d.com/index.php/DebugConsole which lets you print normal debug messages (DebugConsole.Log("...");) in the GUI layer of your game view.
EDIT: for a very convenient way that works in-app on all platforms, see @cybervaldez' answer
FYI the file locations have moved since this very old post, and vary by platform, and change w/ unity versions; the wiki item mentioned did not show the console (and is for years broken and was deleted for that reason, heh!)
A very comfortable way of displaying your logs at runtime is KGFDebug. Check it out.
Hi @NinjaSquirrel !
The easiest way is to use the Debug.LogAssertion.
Debug.LogAssertion("$$anonymous$$essage to show in Development Build/Console");
If you want you can also create a simple log file to save your debug using some WebService or even saving in your computer (if standalone builds).
You do something like:
private void $$anonymous$$yDebug(string message){
Debug.Log(message);
Debug.LogAssertion (message); //you can see in game console
WWWForm form = new WWWForm();
form.AddField("new$$anonymous$$essage", message);
string url = "http://yourserver/saveUnityLog.php";
WWW download = new WWW(url, form);
}
In the file saveUnityLog.php you just need to add a new line in some file like myLog.txt. ;)
NOTE: If in standalone you can use the localhost for sure. Also is a good idea add a datetime for each message.
Good luck.
I have a solution in my new video :)
There was a quicker way to give that information.
If you have Android Studio installed, then you see all the logs in the Android Studio's Logcat.
Answer by bboysil · Mar 18, 2015 at 03:55 PM
An even simpler way to do it.
Just attach this script to any scene gameObject:
This works perfectly with ANY build (debug, production) and ANY platform.
It shows EXACTLY what you would see on the console (Debug.Log, Debug.Error, errors, crashes etc)
using UnityEngine;
namespace DebugStuff
{
public class ConsoleToGUI : MonoBehaviour
{
//#if !UNITY_EDITOR
static string myLog = "";
private string output;
private string stack;
void OnEnable()
{
Application.logMessageReceived += Log;
}
void OnDisable()
{
Application.logMessageReceived -= Log;
}
public void Log(string logString, string stackTrace, LogType type)
{
output = logString;
stack = stackTrace;
myLog = output + "\n" + myLog;
if (myLog.Length > 5000)
{
myLog = myLog.Substring(0, 4000);
}
}
void OnGUI()
{
//if (!Application.isEditor) //Do not display in editor ( or you can use the UNITY_EDITOR macro to also disable the rest)
{
myLog = GUI.TextArea(new Rect(10, 10, Screen.width - 10, Screen.height - 10), myLog);
}
}
//#endif
}
}
Here's another version in which you can
toggle with the space bar
it also creates a full log file anywhere you want (in the example, on the desktop)
it "corrects" the simple GUI window so the text size is always readable on all screens and all platforms
using UnityEngine;
public class ConsoleToGUI : 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) + "/YOUR_LOGS";
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);
}
}
Obviously, if you prefer to use Unity.UI rather than the legacy "GUI", it is completely trivial to write it to a UI.Text in your Canvas.
This is good, however it does not capture logs produced on a thread. Is there a solution for this?
It don't work with logs produced by Unity Jobs threads. (and ECS)
While a good starting point, I believe this solution is inferior to my answer which is more performant, able to silence duplicate log messages, able to hold the most recent N logs in memory while not causing memory overload, and able to collect all logs, even from different threads.
Answer by 5argon · Aug 06, 2017 at 04:57 AM
Sorry for necro-ing the post but in Unity 2017.1 you can connect your device to Unity and then look at the log from your computer. I found this better than trying to print the log on your device's screen. On the downside it prints a bit more than you want (it includes Android system message sometimes, for example.)
If you Build and Run a development build, it will also automatically connect to it. Won't work with Release build. On Windows, you may need to confirm that you accept private network connections in the warning popup that appears on launch.
Answer by justinl · Apr 19, 2013 at 05:21 PM
An easy way is to set your build to a Development Build (in the build settings) and then use Debug.LogError(). This will show up in the console build.
Note that you indeed have to log an error to make the console visible, i.e. Debug.LogError("This message will make the console appear in Development Builds");
Although you can hide the console using Debug.developerConsoleVisible = false
it's not possible to show the console by settings the value to true
. This is documented in https://docs.unity3d.com/ScriptReference/Debug-developerConsoleVisible.html
Answer by Fattie · Sep 23, 2020 at 04:52 PM
Fortunately nowadays (2020) it is now quite easy to do this
toggle with the space bar
it also creates a full log file anywhere you want (in the example, on the desktop)
it "corrects" the simple GUI window so the text size is always readable on all screens and all platforms
using UnityEngine;
public class ConsoleToGUI : 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) + "/YOUR_LOGS";
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);
}
}
Obviously, if you prefer to use Unity.UI rather than the legacy "GUI", it is completely trivial to write it to a UI.Text in your Canvas.
This is 100% plagiarized from the accepted answer (2nd code snippet), even including the info preceding and following the code snippet! I don't understand what value this brings.
Well, it's not ^^. Fattie himself did edit the accepted answer to insert his answer into the accepted one since the original author of the accepted answer hasn't been seen since 2019. When you check the revision history, you will notice that the last changes of the accepted answer were done by Fattie. (Note the revision history is a bit messy, but should record any changes made).
Thanks for clarification, I do admit I was confused as everyone who frequents the forums knows Fattie and you are domain experts so to plagiarize someone else's answer would be very weird! I don't appear to have access to revision history or the button is hidden. Unity could really take some tips from StackOverflow. Either way, I still don't understand the point of keeping this answer up if Fattie already edited the accepted answer - there are now 2 identical answers.
Answer by BTables · Apr 09, 2014 at 08:49 AM
Came looking for this answer, none of these answers are correct, found the correct answer in the uLinkConsoleGUI script:
UnityEngine.Application.RegisterLogCallback(CaptureLog); https://docs.unity3d.com/ScriptReference/Application.LogCallback.html
Three of the answers presented here (including the accepted one) are "correct", they all provide methods and tips on how to access debug output. What is not differentiated, however, is the build platform. For example, you'll only find the output_log.txt in standalone builds, not on mobile builds.
The RegisterLogCallback() you mentioned does not answer the question, as it does not show you any "console debug log", it just gives you an in-game method to access the most recent log message. If you want to log that into a physical file (or even just display it on-screen), you would have to write additional code, which you neither presented, nor mentioned.
I came looking for the same information as the original post, none of the answers were what I was looking for. Which was a specific API that gives "a way to access existing debug log messages through runtime scripts". The accepted solution proposes to completely bypass the built in logging engine. This is bad practice in my opinion
Hm, the current wiki version of DebugConsole does indeed log on-screen only, not in the actual console, which doesn't make much sense. In the version we're using, there's a simple call to Debug.Log to fix this. I'd update the wiki myself, but I don't have my login with me, and registering a new account apparently is a buggy PITA...
Obviously this is the correct answer - fortunately it's now easy to do this in Unity, at last.
Your answer
Follow this Question
Related Questions
How to use Webgl Debug Symbols? 0 Answers
libil2cpp.sym, libil2cpp.so.debug, libunity.sym.so... What should i use for il2cpp debugging? 1 Answer
Distribute terrain in zones 3 Answers
Double clicking console output does not take me to the corresponding line in code. 1 Answer
Developer Console doesn't show up when using BuildOptions.Developer 0 Answers