유니티 커스텀 로그 작성하기

출판일자 

읽는 데 걸리는 시간 2분


How to use Debug Log in Unity (without affecting performance) 🔗

유니티에서 로그는 기본적으로 Debug.Log 류와 MonoBehaviour.print() 가 있다.

문제점

로깅은 따로 설정이 없으면 배포 이후에도 호출된다. 호출 시마다 garbage 를 만든다. (parameter 가 object 로 boxing 됨)


개선방법

Player Settings 의 플래그 끄기

가장 쉬운 방법. Project Settings > Player > Resolution and Presentation > Use Player Log 을 꺼주자.

실제 file Write 만 멈추는 것이라 log 호출에 생기는 garbage 는 막을 수 없다.


빌드타임 코드제거

유니티 개발환경에서는 정적분석 툴을 따로 명시해주지 않기 때문에, 빌드 시점에 로그 코드 제거 등을 위해서는 직접 확장을 해줘야 한다. 빌드시점 코드제거의 방법은 2가지가 있다.

  1. - 전처리기를 이용 #if UNITY_EDITOR, #if UNITY_ANDROID 으로 플랫폼 단위 설정 Script Symbol 직접 정의 설정 #if USE_LOG
  2. -

System.Diagnostics.Conditional Attribute 이용 script symbol 에 따라 컴파일에 포함여부를 결정하는 Attribute


static unityLogger 사용 끄기


Debug.unityLogger.logEnabled = false;
Debug.unityLogger.logEnabled = Debug.isDebugBuild;

모든 디버그 메시지의 호출을 막는다!


채널별로 Logger 직접 생성

public static class Logging { 
    public static Logger myLogger = new  Logger(Debug.unityLogger.logHandler);
 }

public static class Logging {
    public static Logger combatLogger = new Logger(Debug.unityLogger.logHandler);
    public static Logger playerLogger = new Logger(Debug.unityLogger.logHandler);

    public static void LoadLoggers() {
        // Call this function when the game starts
        combatLogger.logEnabled = true;
        playerLogger.logEnabled = true;
    }
}

유니티 Default Logger 와 별개로, 플래그 설정에 독립적. 카테고리(채널) 별 로깅 기본동작을 다르게 할 수 있음.