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

Resolved Unable to properly forward formatted strings from Debug.Log and friends to Unity.Logging

Discussion in 'Editor & General Support' started by Selmar, Jun 26, 2023.

  1. Selmar

    Selmar

    Joined:
    Sep 13, 2011
    Posts:
    55
    To be able to log things that come from various locations in a dedicated server build, I created a custom log handler to forward logs from Debug.Log and friends to Unity.Logging, like so:

    Code (CSharp):
    1. public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    2. {
    3.    switch(logType)
    4.    {
    5.        case LogType.Log:
    6.            Log.Info(format, args);
    7.            break;
    8.        // .. other log types here
    9.    }
    10. }
    11.  
    12. // .. somewhere in start
    13. Debug.unityLogger.logHandler = new RedirectLogger();
    However, I get strange results. The first time after compiling it works for some of them:
    upload_2023-6-26_13-41-58.png


    When I then stop play and play again, none of them work anymore (?!):
    upload_2023-6-26_13-42-53.png


    Ignoring the different results first and second time, I dug a little. It seems there is code generation going on behind the scenes and the
    object[]
    is simply passed as argument one instead of being treated as a list of arguments.

    Is there a way to make this work, or should I wrap all my debug logs another way?
    What about
    Debug.LogError
    and friends that come from other packages?
     
  2. Selmar

    Selmar

    Joined:
    Sep 13, 2011
    Posts:
    55
    I created this monstrosity, which does the job for now...

    Code (CSharp):
    1. switch(logType)
    2. {
    3.     case LogType.Log:
    4.         switch (args.Length)
    5.         {
    6.             case 1: Log.Info(format, args[0].ToString()); break;
    7.             case 2: Log.Info(format, args[0].ToString(), args[1].ToString()); break;
    8.             case 3: Log.Info(format, args[0].ToString(), args[1].ToString(), args[2].ToString()); break;
    9. // ... etc
    10. }
    I guess in the spirit of performance, I suppose it is not intended to support this kind of thing out of the box?
     
  3. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    359
    I'm not sure we directly support parameter arrays in the logging package. The package has been designed to be compatible with Burst, and since parameter arrays are a no-go there that's probably why this use case has not been considered before. It's a good piece of feedback, though. Certainly something we could look into adding. Thanks for bringing it up!

    In the meantime, you could achieve what you want to do with
    string.Format
    , I believe. Something like this:
    Code (CSharp):
    1. public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    2. {
    3.    switch(logType)
    4.    {
    5.        case LogType.Log:
    6.            Log.Info(string.Format(format, args));
    7.            break;
    8.    }
    9. }
    Note however that if the goal is only to redirect "normal" Unity logs to a logger from the logging package, this can already be done with the
    RedirectUnityLogs
    configuration. Here's an example:
    Code (CSharp):
    1. var logger = new LoggerConfig()
    2.     .MinimumLevel.Info()
    3.     .RedirectUnityLogs()
    4.     .CreateLogger();
    The above code will redirect everything normally logged to Unity's
    Debug.Log
    mechanism to
    logger
    .
     
    Selmar likes this.
  4. Selmar

    Selmar

    Joined:
    Sep 13, 2011
    Posts:
    55
    Indeed, silly me completely forgot about String.Format!

    Thanks for the information.