Unity实现简易日志输出功能

作者:Blinkedu 时间:2023-07-25 22:59:44 

在使用Unity中的Debug.Log()进行日志输出时很不方便,在打包出来的可执行文件中没有办法看到输出,所有就想自己实现一个简易的日志输出功能,可以输出到日志文件,因为能力实在是不够,所以有错误和不合理的地方,还请各位老师指点一下,谢谢啦

1.日志记录器接口


public interface ILogger
{
void Log(string condition, string stackTrace, UnityEngine.LogType type);
}

2.日志文件记录器


using System;
using UnityEngine;
using System.IO;

public class FileLogger : ILogger
{
private readonly string path;

/// <summary>
/// 构造方法
/// </summary>
/// <param name="isClear">是否清空原有的日志</param>
public FileLogger(bool isClear = false)
{
 switch (Application.platform)
 {
  case RuntimePlatform.Android:
   path = Path.Combine( Application.persistentDataPath,"log.txt");
   break;
  case RuntimePlatform.WindowsPlayer:
   path = Path.Combine(Application.dataPath, "log.txt");
   break;
  case RuntimePlatform.WindowsEditor:
   path = Path.Combine(Application.dataPath, "log.txt");
   break;
  case RuntimePlatform.IPhonePlayer:
   path = Path.Combine(Application.persistentDataPath, "log.txt");
   break;
  case RuntimePlatform.OSXEditor:
   break;
  default:
   break;
 }

if (isClear)
 {
  if (File.Exists(path))
  {
   File.Delete(path);
  }
 }
}

public void Log(string condition, string stackTrace, LogType type)
{
 using (StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.UTF8))
 {
  string msg = string.Format("[{0}] {1}: {2}\n{3}", GetNowTime(), type, condition, stackTrace);
  sw.WriteLine(msg);
 }
}

#region Tool Method
private string GetNowTime()
{
 return DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
}
#endregion
}

3.日志系统管理类 


using System;
using UnityEngine;

public class LogSys
{
private static ILogger logger;
public static ILogger Logger
{
 get { return logger; }
}

public bool IsOpen
{
 get { return Debug.unityLogger.logEnabled; }
}

private LogSys() { }

/// <summary>
/// 初始化
/// </summary>
/// <param name="_logger">日志输出器</param>
/// <param name="isOpen">是否开启日志输出</param>
public static void Init(ILogger _logger, bool isOpen = true)
{
 Init(isOpen);
 logger = _logger;
 Enable();
}

public static void Init(bool isOpen = true)
{
 Debug.unityLogger.logEnabled = isOpen;
}

/// <summary>
/// 过滤器
/// </summary>
/// <param name="logType">需要显示的日志类型</param>
public static void Filter(LogType logType = LogType.Log)
{
 Debug.unityLogger.filterLogType = logType;
}

public static void Enable()
{
 if (logger != null)
 {
  Application.logMessageReceived += logger.Log;
 }
}

public static void Disable()
{
 if (logger != null)
 {
  Application.logMessageReceived -= logger.Log;
 }

}
}

4.测试 


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
public Text logText;

void Awake()
{
 LogSys.Init(new FileLogger());
}

void Update()
{
 if (Input.GetKeyDown(KeyCode.Q))
 {
  Debug.Log("My name is Blinkedu.");
 }

if (Input.GetKeyDown(KeyCode.W))
 {
  Debug.LogWarning("My name is Blinkedu.");
 }

if (Input.GetKeyDown(KeyCode.E))
 {
  Debug.LogError("My name is Blinkedu.");
 }
}

private void OnDestroy()
{
 LogSys.Disable();
}
}

Unity实现简易日志输出功能

来源:https://blog.csdn.net/qq_30473517/article/details/88815881

标签:Unity,日志输出
0
投稿

猜你喜欢

  • C#实现泛型List分组输出元素的方法

    2022-03-10 07:33:15
  • Android 使用FragmentTabHost实现底部菜单功能

    2021-06-11 12:31:52
  • Android studio实现简单的计算器

    2022-09-07 23:23:28
  • Android实现纸飞机的简单操作

    2022-05-16 12:26:04
  • C#随机生成Unicode类型字符串

    2023-12-05 08:42:29
  • Maven如何打入依赖中指定的部分jar包

    2023-09-22 02:50:33
  • 详解Java对象的强、软、弱和虚引用+ReferenceQueue

    2021-11-30 16:23:01
  • mybatis 传入null值的解决方案

    2023-11-23 06:54:44
  • Java 中解决Unsupported major.minor version 51.0的问题

    2022-07-22 03:53:08
  • Spring AOP的概念与实现过程详解

    2023-02-17 02:54:12
  • arthas jprofiler做复杂链路的调用分析

    2022-01-15 12:01:25
  • Android仿简书搜索框效果的示例代码

    2023-06-18 16:02:58
  • c#连接mdf文件示例分享

    2022-12-15 10:48:33
  • Maven添加Tomcat插件实现热部署代码实例

    2021-12-09 02:03:49
  • Java日常练习题,每天进步一点点(33)

    2023-09-22 05:32:41
  • Java抽象类和抽象方法定义与用法实例详解

    2023-10-02 14:30:13
  • Android 中解决Viewpage调用notifyDataSetChanged()时界面无刷新的问题

    2022-11-11 22:21:50
  • C#实现Word和ODT文档相互转换详解

    2022-12-17 21:36:47
  • C++类静态成员与类静态成员函数详解

    2022-10-10 08:22:20
  • Spring component-scan XML配置与@ComponentScan注解配置

    2023-07-04 03:37:12
  • asp之家 软件编程 m.aspxhome.com