实现代码
找到插件的 UnityOutput.cs
然后按照需求替换为下面的代码即可。如果提示 void ILogOutput.Flush() { }
接口不存在,删除这行代码即可。
using Best.HTTP.JSON.LitJson;
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
namespace Best.HTTP.Shared.Logger
{
/// <summary>
/// Provides an implementation of <see cref="ILogOutput"/> that writes log messages to the Unity Debug Console.
/// </summary>
public sealed class UnityOutput : ILogOutput
{
/// <summary>
/// 文件url 字符的颜色
/// </summary>
public string _Color = "FFA500";
/// <summary>
/// Gets a value indicating whether this log output accepts color codes.
/// </summary>
/// <remarks>
/// This property returns <c>true</c> when running in the Unity Editor and <c>false</c> otherwise.
/// </remarks>
public bool AcceptColor { get; } = UnityEngine.Application.isEditor;
/// <summary>
/// Writes a log message to the Unity Debug Console based on the specified log level.
/// </summary>
/// <param name="level">The log level of the message.</param>
/// <param name="logEntry">The log message to write.</param>
public void Write(Loglevels level, string logEntry)
{
switch (level)
{
case Loglevels.All:
case Loglevels.Information:
UnityEngine.Debug.Log(logEntry);
break;
case Loglevels.Warning:
UnityEngine.Debug.LogWarning(logEntry);
break;
case Loglevels.Error:
case Loglevels.Exception:
var errorLog = JsonMapper.ToObject<ErrorLog>(logEntry);
// 处理路径信息并修改颜色
string processedStack = ProcessStack(errorLog.stack);
string processedExStack = ProcessStack(errorLog.ex[0].stack);
// 重新构建日志条目
logEntry = $"[{errorLog.ll}] {errorLog.msg}\n" +
$"Stack: {processedStack}\n" +
$"Exception: {errorLog.ex[0].msg}\n" +
$"Exception Stack: {processedExStack}";
UnityEngine.Debug.LogError(logEntry);
break;
}
}
/// <summary>
/// This implementation does nothing.
/// </summary>
void ILogOutput.Flush() { }
void IDisposable.Dispose() => GC.SuppressFinalize(this);
/// <summary>
/// 修改内容,将url换成可以点击,并修改颜色
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
private string ProcessStack(string stack)
{
// 使用正则表达式匹配路径信息
string pattern = @"([A-Z]:\\[^:]+:\d+)";
string replacement = $"<color=#{_Color}>$1</color>";
// 替换路径信息为蓝色
string processedStack = Regex.Replace(stack, pattern, replacement);
// 替换换行符
processedStack = processedStack.Replace("\r\n", "\n");
return processedStack;
}
}
[SerializeField]
public class ErrorLog
{
/// <summary>
///
/// </summary>
public int tid;
/// <summary>
///
/// </summary>
public string div;
/// <summary>
///
/// </summary>
public string msg;
/// <summary>
///
/// </summary>
public List<ExItem> ex;
/// <summary>
///
/// </summary>
public string stack;
/// <summary>
///
/// </summary>
public List<CtxsItem> ctxs;
/// <summary>
///
/// </summary>
public long t;
/// <summary>
///
/// </summary>
public string ll;
/// <summary>
///
/// </summary>
public long bh;
}
[SerializeField]
public class ExItem
{
public string msg;
public string stack;
}
[SerializeField]
public class CtxsItem
{
public string TypeName;
public long Hash;
}
}
功能描述
besthttp 自身的错误日志打印:
替换为上面的代码之后的日志打印是:
修改颜色是修改:_Color