facebook twitter hatena line email

Unity/Csharp/Log

提供: 初心者エンジニアの簡易メモ
2023年9月9日 (土) 02:21時点におけるAdmin (トーク | 投稿記録)による版 (カスタムログ)

移動: 案内検索

ログの記述方法

using UnityEngine;
Debug.Log("ログです");
Debug.LogError("エラーログです");
Debug.Log("hoge=" + hoge);
Debug.Log($"hoge={hoge}");

ログは、Consoleに表示される。

Consoleは、Unityメインメニュー/Window/General/Consoleで、開く。

カスタムログ

CustomLogHandler.cs

using System;
using UnityEngine;

public class CustomLogHandler : ILogHandler
{
    private readonly ILogHandler _logHandler;

    public CustomLogHandler(ILogHandler logHandler)
    {
        _logHandler = logHandler;
    }

    public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    {
        _logHandler.LogFormat(logType, context, $"[{logType}] {format}", args);
    }

    public void LogException(Exception exception, UnityEngine.Object context)
    {
        _logHandler.LogException(exception, context);
    }
}

MainScene.cs

class MainScene
{
    void Awake()
    {
        Debug.unityLogger.logHandler = new CustomLogHandler(Debug.unityLogger.logHandler);
    }
}

参考:https://nekojara.city/unity-log-customization