- Home /
How do I find the player log file from code?
I want to write a script to upload logfiles with a bugreport. How do I find the logfile? (For production, so no editor logs.)
The documentation gives these paths:
macOS ~/Library/Logs/Unity/Player.log
Linux ~/.config/unity3d/CompanyName/ProductName/Player.log
Windows Unity 5.6 EXECNAME_Data\output_log.txt
Windows Unity 2017+ C:\Users\username\AppData\LocalLow\CompanyName\ProductName\output_log.txt
How do I translate these paths into code?
CommandLineArguments docs has an entry for a -logFile
option:
Specify where the Editor or Windows/Linux/OSX standalone log file are written.
If that's used, is there a way to get a path to the log file?
Answer by idbrii · Mar 23, 2018 at 10:42 PM
For code examples, I assume you have CombinePaths.
macOS
~/Library/Logs/Unity/Player.log
Straightforward: just use that path.
Linux
~/.config/unity3d/CompanyName/ProductName/Player.log
CompanyName/ProductName are values from Edit > Settings > Player. They're exposed in code:
var log_path = CombinePaths("~/.config/unity3d", Application.companyName, Application.productName, "Player.log");
.
Windows
Unity 2017+ C:\Users\username\AppData\LocalLow\CompanyName\ProductName\output_log.txt
On recent versions of Unity, this should work for Windows:
var log_path = CombinePaths(Environment.GetEnvironmentVariable("AppData"), "..", "LocalLow", Application.companyName, Application.productName, "output_log.txt");
.
Unity 5.6 EXECNAME_Data\output_log.txt
But for 5.6, docs say:
On Windows, EXECNAME_Data is a folder next to the executable with your game.
According to how do i get the application path, you can base it off of dataPath:
// Unity strips some characters from productName to make the exe name. I've
// also seen it convert to lower case.
var exe_basename = Application.productName.Replace(" ", "");
var log_path = CombinePaths(Application.dataPath, "..", exe_basename +"_Data", "output_log.txt");
.
cmdline
For -logFile
, I think you have to parse the System.Environment.GetCommandLineArgs.
Answer by Fattie · Aug 29, 2020 at 03:31 PM
It's now pretty easy to write a log file anywhere you want:
Just use:
void OnEnable() { Application.logMessageReceived += Log; }
void OnDisable() { Application.logMessageReceived -= Log; }
Full example:
This isn't very useful as it only supports Windows, I'd recommend @idbrii's answer above instead.
Specifically to windows because of the SpecialFolder.Desktop? It's the: void OnEnable() { Application.log$$anonymous$$essageReceived += Log; } void OnDisable() { Application.log$$anonymous$$essageReceived -= Log; } That's the important part. You can have log save wherever or print wherever, it's just an example. Change the path if you want to.