Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to catch logs even when it is disabled in Unity settings

Discussion in 'Scripting' started by sudarshans, Mar 8, 2022.

  1. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    I wanted to write logs into a text file in a release build but in our release build we set
    Code (CSharp):
    1. Debug.unityLogger.logEnabled = false;
    So that app should not print logs to console (in ADB console / iOS terminal)
    But even after disabling logs is there any way to catch logs in code so that I can write into a text file?

    Im already aware of custom logger implementation but the current requirement is that I have to manage with the Debug.Log() itself but how to catch the logs in case of unityLogger is disabled?

    Any help would be appreciated.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    Well, instead of running the log code, just have your custom class that you pass a string to and write directly to a file. It that end, you're simply writing text to a file, so it doesn't care if you have logging enabled or not.
     
  3. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yeah, I have already mentioned it in my question itself, this is a specific requirement so just checking if there is any way.
     
  4. sudarshans

    sudarshans

    Joined:
    Nov 13, 2021
    Posts:
    26
    I have got the solution basically, we should assign our custom logger to logHandler like this
    Code (CSharp):
    1. Debug.unityLogger.logHandler = <your_custom_logger>
    So here all the logs will be finally received into your custom logger instead of printing to console.
    Your custom logger class should inherit from ILogHandler.

    Note: If you don't handle this implementation properly, it might go into an infinite loop and your app might crash (even in unity editor).