unity游戲框架學習-日志系統


概述:https://www.cnblogs.com/wang-jin-fu/p/10975660.html

 

日志系統功能包括:

1.日志開關。只有開發版本開啟日志,因為日志還是比較耗性能的。。。

2.堆棧日志界面:ERROR時彈出界面,該界面顯示錯誤的堆棧日志。大半部分錯誤日志是不會導致崩潰,如果不彈窗qa可能會漏掉一些重要的log信息。

3.接入SRDebugger,方便在qa測試時,在測試機查看詳細的日志信息,方便定位錯誤出現的原因。

4.FPS幀率的顯示

5.游戲正式上線以后,我們很難拿到用戶的錯誤日志,這時候我們需要把錯誤的日志上傳到我們的服務器

6.當游戲崩潰時我們是拿不到unity打印的日志的,這時候就需要接入FireBase了,它可以幫我們把崩潰的詳細日志上傳到網頁上,方便我們查看

 

日志系統目標用戶:

1.qa、運營等測試人員(可以拿到測試機),需要在手機上實現可視化的日志堆棧,方便查閱日志(當然你也可以把手機連到Android Studio和Xcode查看日志,就是比較麻煩而已),對定位bug有很大幫助。

2.用戶(獲取不到測試機),需要把日志上傳到服務器,崩潰日志需要接入FireBase,這樣就可以在FB后台看到崩潰的堆棧信息。

 

一、手機上顯示log信息:SRDebugger插件

SRDebugger文檔:https://www.stompyrobot.uk/tools/srdebugger/documentation/

SRDebugger下載:https://assetstore.unity.com/packages/tools/gui/srdebugger-console-tools-on-device-27688,嗯,這個24美刀~

SRDebugger界面示例:SRDebugger可以獲取當前運行系統的信息,包括操作系統、處理器、顯卡等硬件信息。

SRDebugger可以查看所有的程序運行日志,包括使用Debug.Log系列打印的日志,或是其他的未捕獲異常。

SRDebugger可以監控整個項目的內存使用信息,手動卸載資源,手動進行GC垃圾回收。

二、日志開關及堆棧信息獲取

日志開關無非就是框架封裝一層日志接口,所有業務的日志打印都走這個接口,在根據條件判斷是否調用Debug.Log方法。例如:

public void Log(string log)
    {
        if (Config.LogEnable)
        {
            Debug.Log(log);
        }
    }

日志堆棧信息獲取:Application.logMessageReceived接口。每次接收到日志消息,都會觸發的事件。注意在logMessageReceived回調里打印任何日志都不會生效(避免死循環)。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public string output = "";
    public string stack = "";

    void OnEnable()
    {
        Application.logMessageReceived += HandleLog;
    }

    void OnDisable()
    {
        Application.logMessageReceived -= HandleLog;
    }

    void HandleLog(string logString, string stackTrace, LogType type)
    {
        output = logString;
        stack = stackTrace;
    }
}
//
    // 摘要:
    //     The type of the log message in Debug.logger.Log or delegate registered with Application.RegisterLogCallback.
    public enum LogType
    {
        //
        // 摘要:
        //     LogType used for Errors.
        Error = 0,
        //
        // 摘要:
        //     LogType used for Asserts. (These could also indicate an error inside Unity itself.)
        Assert = 1,
        //
        // 摘要:
        //     LogType used for Warnings.
        Warning = 2,
        //
        // 摘要:
        //     LogType used for regular log messages.
        Log = 3,
        //
        // 摘要:
        //     LogType used for Exceptions.
        Exception = 4
    }

logString:日志信息。stackTrace:日志的詳細堆棧信息。

那么錯誤彈窗就是將錯誤信息以及錯誤的堆棧信息賦值給Text並顯示在界面上。

例如

public void DevelopLog(string logString, string stackTrace, LogType type)
    {
        if (type == LogType.Error || type == LogType.Exception)
        {
            string result = "LogString:" + logString;
            result += "\nStackTrace:" + stackTrace;
            m_content.text = log;

        }
    }

保存服務端的話就是將上面的result值傳給服務端,由服務端保存。當然你也可以額外添加一些參數,例如版本、時間、機型等。例如

string result = string.Format("Version:{0}\nTime:{0}\nLogString:{1}\nStackTrace:{2}", Config.Version, Time.time, logString, stackTrace);

三、崩潰信息上傳FireBase

Android API文檔:https://firebase.google.com/docs/android/setup?hl=zh-cn

Ios API文檔:https://firebase.google.com/docs/ios/setup?hl=zh-cn

這個兩個文檔有詳細的FireBase后台配置的步驟,以及GoogleService-Info.plist 、google-services.json文件的生成(這兩個文件需要放在你的工程目錄里)

 

如果你的sdk或者運營大哥幫你配置好了並給了你GoogleService-Info.plist 、google-services.json文件的話,只需要參考以下步驟就好了。

android:只需要兩步,記得把你的google-services.json文件拷貝到項目里

參考:https://firebase.google.com/docs/crashlytics/get-started?platform=android#android

 

1.在項目級 build.gradle 中,將您的 google-services 更新為 3.1.2 或更高版本,然后添加 Crashlytics 代碼庫和依賴項:

buildscript {
    repositories {
        // Add the following repositories:
        google()  // Google's Maven repository

        maven {
           url 'https://maven.fabric.io/public'
        }
    }

    dependencies {
        // ...

        // Check for v3.1.2 or higher
        classpath 'com.google.gms:google-services:4.2.0'  // Google Services plugin

        // Add dependency
        classpath 'io.fabric.tools:gradle:1.29.0'  // Crashlytics plugin

        
    }
}


allprojects {
    // ...

    repositories {
       // Check that you have the following line (if not, add it):
       google()  // Google's Maven repository
       // ...
    }
}

2.在應用級 build.gradle 中,將 firebase-core 更新為 v11.4.2 或更高版本,然后添加 Crashlytics 依賴項:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

dependencies {
    // ...

    // Check for v11.4.2 or higher
    implementation 'com.google.firebase:firebase-core:17.0.0'

    // Add dependency
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}

 

ios參考:https://firebase.google.com/docs/crashlytics/get-started?platform=ios#android

1.拉取代碼,打開 Podfile,然后添加以下行:

pod 'Fabric', '~> 1.10.2'
pod 'Crashlytics', '~> 3.13.3'

2.初始化

 如果你的ios端提示dsym未上傳,可以用命令行上傳(比較耗時間,建議只在正式包上傳),命令如下:

/path/to/pods/directory/Fabric/upload-symbols -gsp /path/to/GoogleService-Info.plist -p ios /path/to/dSYMs

//這邊的self.OutProjectPath是你的xcode項目目錄
cmddSYM = "%sPods/Fabric/upload-symbols -gsp %sGoogleService-Info.plist -p ios %sarchive/SF.xcarchive/dSYMs" % (self.OutProjectPath, self.OutProjectPath, self.OutProjectPath)

DSYM丟失參考api:https://firebase.google.com/docs/crashlytics/get-deobfuscated-reports?authuser=0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM