Unity实现简易⽇志输出功能
在使⽤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,"");
break;
case RuntimePlatform.WindowsPlayer:
path = Path.Combine(Application.dataPath, "");
break;
case RuntimePlatform.WindowsEditor:
path = Path.Combine(Application.dataPath, "");
break;
case RuntimePlatform.IPhonePlayer:
path = Path.Combine(Application.persistentDataPath, "");
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)
{
writeline方法的作用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();
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论