有的功能
- 可以自定义Log的颜色
- 可以添加模块
- 可以自定义Log输出级别,上线后关闭Log
- 可以写入本地,方便查看难以复现的bug
LogManager
using System;
using System.Collections.Generic;
using UnityEngine;
namespace XFramework.Log
{
public enum LogLevel
{
None = 0,
Info = 1,
Warning = 2,
Error = 3,
}
public enum LogColor
{
White,
Red,
Orange,
Yellow,
Green,
Cyan,
Blue,
Purple,
}
public class LogManager : Singleton<LogManager>
{
private readonly Dictionary<LogColor, string> dicColor = new Dictionary<LogColor, string>()
{
{ LogColor.White, "#ffffff" },
{ LogColor.Red, "#ff0000" },
{ LogColor.Orange, "#ffa500" },
{ LogColor.Yellow, "#ffff00" },
{ LogColor.Green, "#008000" },
{ LogColor.Cyan, "#00ffff" },
{ LogColor.Blue, "#0000ff" },
{ LogColor.Purple, "#800080" },
};
public List<LogLevel> logLevels;
public LogSave logSave;
public LogManager()
{
logLevels = new List<LogLevel>()
{
LogLevel.Info,
LogLevel.Warning,
LogLevel.Error
};
logSave = new LogSave();
}
public void Log(object message, LogColor color = LogColor.White, string module = null)
{
if (logLevels == null) return;
if (!logLevels.Contains(LogLevel.Info)) return;
var partPrefix = string.IsNullOrEmpty(module) ? "" : "[" + module + "]";
var colorHex = dicColor[color];
var msg = string.Format("<color={0}>{1}{2}</color>", colorHex, partPrefix, message);
Debug.Log(msg);
}
public void LogWarning(object message, LogColor color = LogColor.White, string module = null)
{
if (logLevels == null) return;
if (!logLevels.Contains(LogLevel.Warning)) return;
var partPrefix = string.IsNullOrEmpty(module) ? "" : "[" + module + "]";
var colorHex = dicColor[color];
var msg = string.Format("<color={0}>{1}{2}</color>", colorHex, partPrefix, message);
Debug.LogWarning(msg);
}
public void LogError(object message, LogColor color = LogColor.White, string module = null)
{
if (logLevels == null) return;
if (!logLevels.Contains(LogLevel.Error)) return;
var partPrefix = string.IsNullOrEmpty(module) ? "" : "[" + module + "]";
var colorHex = dicColor[color];
var msg = string.Format("<color={0}>{1}{2}</color>", colorHex, partPrefix, message);
Debug.LogError(msg);
}
public void LogSave(object message, LogColor color = LogColor.White, string module = null)
{
Log(message, color, module);
if (logSave.isSave)
{
var msg = message.ToString();
var trackStr = new System.Diagnostics.StackTrace().ToString();
logSave.OnLogCallBack(msg, trackStr, LogType.Log);
}
}
public void LogWarningSave(object message, LogColor color = LogColor.White, string module = null)
{
LogWarning(message, color, module);
if (logSave.isSave)
{
var msg = message.ToString();
var trackStr = new System.Diagnostics.StackTrace().ToString();
logSave.OnLogCallBack(msg, trackStr, LogType.Warning);
}
}
public void LogErrorSave(object message, LogColor color = LogColor.White, string module = null)
{
LogError(message, color, module);
if (logSave.isSave)
{
var msg = message.ToString();
var trackStr = new System.Diagnostics.StackTrace().ToString();
logSave.OnLogCallBack(msg, trackStr, LogType.Error);
}
}
}
}
LogSave
using System;
using System.IO;
using System.Text;
using UnityEngine;
namespace XFramework.Log
{
public class LogSave
{
public bool isSave = true;
private string logFileSavePath;
private StringBuilder logSb = new StringBuilder();
public LogSave()
{
var time = System.DateTime.Now.ToString("yyyyMMddhhmmss");
logFileSavePath = string.Format("{0}/log_{1}.log", Application.persistentDataPath, time);
Debug.Log("log file path: " + logFileSavePath);
}
/// <summary>
/// 打印日志回调
/// </summary>
/// <param name="condition">日志文本</param>
/// <param name="stackTrace">调用堆栈</param>
/// <param name="type">日志类型</param>
public void OnLogCallBack(string condition, string stackTrace, LogType type)
{
logSb.Append(string.Format("[{0}]", type));
logSb.Append(string.Format("[{0}]", System.DateTime.Now.ToString("hh:mm:ss")));
logSb.Append(condition);
logSb.Append("\n");
logSb.Append(stackTrace);
logSb.Append("\n");
if (logSb.Length <= 0) return;
if (!File.Exists(logFileSavePath))
{
var fs = File.Create(logFileSavePath);
fs.Close();
}
using (var sw = File.AppendText(logFileSavePath))
{
sw.WriteLine(logSb.ToString());
}
logSb.Remove(0, logSb.Length);
}
}
}
CustomLog
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XFramework.Log
{
public class CustomLog
{
public string module;
public LogColor logColor;
public CustomLog(string module, LogColor logColor)
{
this.module = module;
this.logColor = logColor;
}
public void Log(object message)
{
LogTool.Log(message, logColor, module);
}
public void LogWarning(object message)
{
LogTool.LogWarning(message, logColor, module);
}
public void LogError(object message)
{
LogTool.LogError(message, logColor, module);
}
}
}
最后是使用方式
//Log
LogTool.Log("this is a test message");
LogTool.LogWarning("this is a test warning message");
LogTool.LogError("this is a test error message");
LogTool.Log("this is a test message", LogColor.Red);
LogTool.LogWarning("this is a test warning message", LogColor.Red);
LogTool.LogError("this is a test error message", LogColor.Red);
LogTool.Log("this is a test message", LogColor.Red, "Test");
LogTool.LogWarning("this is a test warning message", LogColor.Red, "Test");
LogTool.LogError("this is a test error message", LogColor.Red, "Test");
//CustomLog
CustomLog uiLog = new CustomLog("UI", LogColor.Yellow);
CustomLog audioLog = new CustomLog("Audio", LogColor.Green);
CustomLog excelLog = new CustomLog("Excel", LogColor.Blue);
uiLog.Log("this is a ui message");
audioLog.Log("this is a audio message");
excelLog.Log("this is a excel message");
//close all log
LogTool.SetLogLevel(null);
//only show error log
LogTool.SetLogLevel(new List<LogLevel>() { LogLevel.Error });
网友评论