1.LogManager类
using System.Text;
using UnityEngine;
using System.IO;
using System;
public class LogManager : MonoSingleton<LogManager>
{
// 系统调试日志开关
public static bool systemLogEnable = true;
// 普通调试日志开关
public static bool debugLogEnable = true;
// 警告日志开关
public static bool warningLogEnable = true;
// 错误日志开关
public static bool errorLogEnable = true;
// 系统异常日志
public static string systemLogName="系统异常日志";
// 日志文件存储位置
public static string logFileSavePath;
// 使用StringBuilder来优化字符串的重复构造
private static StringBuilder logStr = new StringBuilder();
/// <summary>
/// 初始化
/// </summary>
public override void Init()
{
// 日期
var date = DateTime.Now.ToString("yyyy年MM月dd日");
logFileSavePath = string.Format("{0}/{1}", Application.persistentDataPath + "/Log", date);
if (!Directory.Exists(logFileSavePath))
{
Directory.CreateDirectory(logFileSavePath);
}
Application.logMessageReceived += OnLogCallBack;
}
/// <summary>
/// 打印日志回调
/// </summary>
/// <param name="condition">日志文本</param>
/// <param name="stackTrace">调用堆栈</param>
/// <param name="type">日志类型</param>
private static void OnLogCallBack(string condition, string stackTrace, LogType type)
{
// 过滤自己的输出
if (type == LogType.Log || !systemLogEnable)
{
return;
}
logStr.Append(GetLogTime());
switch (type)
{
case LogType.Error:
logStr.Append("ERROR -");
break;
case LogType.Assert:
break;
case LogType.Warning:
logStr.Append("WARN -");
break;
case LogType.Log:
logStr.Append("INFO -");
break;
case LogType.Exception:
break;
default:
break;
}
logStr.Append(condition);
logStr.Append("\n");
logStr.Append(stackTrace);
if (logStr.Length <= 0) return;
WriteLogFile(systemLogName, logStr.ToString());
logStr.Remove(0, logStr.Length);
}
//public static void UploadLog(string desc)
//{
// LogUploader.StartUploadLog(s_logFileSavePath, desc);
//}
public static void WriteLogFile(string logName,string logContext)
{
var _logPath = string.Format("{0}/{1}.log", logFileSavePath, logName);
if (!File.Exists(_logPath))
{
var fs = File.Create(_logPath);
fs.Close();
}
using (var sw = File.AppendText(_logPath))
{
sw.WriteLine(logContext.ToString());
}
}
/// <summary>
/// 输出日志时间
/// </summary>
/// <returns></returns>
public static string GetLogTime()
{
return "●" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " ";
}
}
2. Logger类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Logger
{
private string _logName = "default";
public Logger(string logName)
{
_logName = logName;
}
#region 日志
public void Info(object message)
{
string str = LogManager.GetLogTime() + "INFO -" + message;
if (!LogManager.debugLogEnable) return;
Debug.Log(message);
LogManager.WriteLogFile(_logName, str);
}
public void Warning(object message)
{
string str = LogManager.GetLogTime() + "WARN -" + message;
if (!LogManager.warningLogEnable) return;
Debug.LogWarning(message);
LogManager.WriteLogFile(_logName, str);
}
public void Error(object message)
{
string str = LogManager.GetLogTime() + "ERROR -" + message;
if (!LogManager.errorLogEnable) return;
Debug.LogError(message);
LogManager.WriteLogFile(_logName, str);
}
#endregion
}
3. 打印日志
![](https://img-blog.csdnimg.cn/direct/c3f35a8e495f4d43aeb6c12445a38490.png)