usingSystem;usingSystem.IO;namespaceUnityEngine

{///

///系统日志模块///

public classDebugger

{public static bool EnableLog; //是否启用日志,仅可控制普通级别的日志的启用与关闭,LogError和LogWarn都是始终启用的。

public static bool EnableTime = true;public static bool EnableSave = false; //是否允许保存日志,即把日志写入到文件中

public static bool EnableStack = false;public static string LogFileDir = Application.persistentDataPath + "/DebuggerLog/";public static string LogFileName = "";public static string Prefix = ">"; //用于与Unity默认的系统日志做区分。本日志系统输出的日志头部都会带上这个标记。

public static StreamWriter LogFileWriter = null;public static boolUseUnityEngine;private static string GetLogText(string tag, stringmessage)

{string str = "";if(EnableTime)

{

str= DateTime.Now.ToString("HH:mm:ss.fff") + " ";

}return (str + tag + "::" +message);

}private static stringGetLogTime()

{string str = "";if(EnableTime)

{

str= DateTime.Now.ToString("HH:mm:ss.fff") + " ";

}returnstr;

}public static void Log(objectmessage)

{if (!Debugger.EnableLog)return;string str = GetLogTime() +message;

Debug.Log(Prefix+ str, null);

LogToFile("[I]" + str, false);

}public static void Log(objectmessage, Object context)

{if (!Debugger.EnableLog)return;string str = GetLogTime() +message;

Debug.Log(Prefix+str, context);

LogToFile("[I]" + str, false);

}public static void Log(string tag, stringmessage)

{if (!Debugger.EnableLog)return;

message=GetLogText(tag, message);

Debug.Log(Prefix+ message, null);

LogToFile("[I]" + message, false);

}public static void Log(string tag, string format, params object[] args)

{if (!Debugger.EnableLog)return;string logText = GetLogText(tag, string.Format(format, args));

Debug.Log(Prefix+ logText, null);

LogToFile("[I]" + logText, false);

}public static void LogError(objectmessage)

{string str = GetLogTime() +message;

Debug.Log(Prefix+ str, null);

LogToFile("[E]" + str, true);

}public static void LogError(objectmessage, Object context)

{string str = GetLogTime() +message;

Debug.Log(Prefix+str, context);

LogToFile("[E]" + str, true);

}public static void LogError(string tag, stringmessage)

{

message=GetLogText(tag, message);

Debug.Log(Prefix+ message, null);

LogToFile("[E]" + message, true);

}public static void LogError(string tag, string format, params object[] args)

{string logText = GetLogText(tag, string.Format(format, args));

Debug.Log(Prefix+ logText, null);

LogToFile("[E]" + logText, true);

}///

///将日志写入到文件中///

///

///

private static void LogToFile(string message, bool EnableStack = false)

{if (!Debugger.EnableSave)return;if (LogFileWriter == null)

{

LogFileName= DateTime.Now.GetDateTimeFormats('s')[0].ToString();

LogFileName= LogFileName.Replace("-", "_");

LogFileName= LogFileName.Replace(":", "_");

LogFileName= LogFileName.Replace(" ", "");

LogFileName= LogFileName + ".log";if (string.IsNullOrEmpty(LogFileDir))

{try{if(UseUnityEngine)

{

LogFileDir= Application.persistentDataPath + "/DebuggerLog/";

}else{

LogFileDir= AppDomain.CurrentDomain.BaseDirectory + "/DebuggerLog/";

}

}catch(Exception exception)

{

Debug.Log(Prefix+ "获取 Application.persistentDataPath 报错!" + exception.Message, null);return;

}

}string path = LogFileDir +LogFileName;try{if (!Directory.Exists(LogFileDir))

{

Directory.CreateDirectory(LogFileDir);

}

LogFileWriter=File.AppendText(path);

LogFileWriter.AutoFlush= true;

}catch(Exception exception2)

{

LogFileWriter= null;

Debug.Log("LogToCache()" + exception2.Message + exception2.StackTrace, null);return;

}

}if (LogFileWriter != null)

{try{

LogFileWriter.WriteLine(message);if ((EnableStack || Debugger.EnableStack) &&UseUnityEngine)

{

LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace());

}

}catch(Exception)

{

}

}

}public static void LogWarning(objectmessage)

{string str = GetLogTime() +message;

Debug.Log(Prefix+ str, null);

LogToFile("[W]" + str, false);

}public static void LogWarning(objectmessage, Object context)

{string str = GetLogTime() +message;

Debug.Log(Prefix+str, context);

LogToFile("[W]" + str, false);

}public static void LogWarning(string tag, stringmessage)

{

message=GetLogText(tag, message);

Debug.Log(Prefix+ message, null);

LogToFile("[W]" + message, false);

}public static void LogWarning(string tag, string format, params object[] args)

{string logText = GetLogText(tag, string.Format(format, args));

Debug.Log(Prefix+ logText, null);

LogToFile("[W]" + logText, false);

}

}

}

unity 日志级别_【Unity】通用的Debugger日志模块相关推荐

  1. java自定义日志级别_自定义log4j日志级别

    因为项目中需要输出一些特别的日志来做数据统计.如果开启log4j提供的INFO日志级别,每天生成的日志文件就会变得越来越大.这样就得写个定 时任务来删除这个文件.为了只输出所需的日志级别,唯有自己定义 ...

  2. 更改日志级别_如何在运行时更改日志记录级别

    更改日志级别 在运行时中更改日志记录级别非常重要,这主要在生产环境中非常重要,在生产环境中,您可能希望在有限的时间内进行调试日志记录. 好了,更改根记录器非常简单–假设您有一个具有所需记录级别的输入参 ...

  3. glog 设置日志级别_如何动态改变日志级别

    前言 关于日志级别,大部分项目可能都设置为info级别,当然也可能有一些追求性能或者说包含很多敏感信息的项目直接将级别设置为warn或者error:这时候如果项目中出现一些未知异常,需要用到很详细的日 ...

  4. 日志级别_Feign:请求压缩amp;日志级别【了解】

    1 .请求压缩 Spring Cloud Feign支持对请求和响应进行GZIP压缩,以减少通信过程中的性能损耗.通过下面的参数即可开启请求与响应的压缩功能(consumer中进行配置): feign ...

  5. 一文搞懂Java日志级别,重复记录、丢日志问题

    1 SLF4J 日志行业的现状 框架繁 不同类库可能使用不同日志框架,兼容难,无法接入统一日志,让运维很头疼! 配置复杂 由于配置文件一般是 xml 文件,内容繁杂!很多人喜欢从其他项目或网上闭眼co ...

  6. 安装程序未能打开日志文件_工地开工,安全日志记录这样写才规范!

    一.安全日志的理解 施工安全日志是从工程开始到竣工,由专职安全员对整个施工过程中的重要生产和技术活动的连续不断的详实记录.是项目每天安全施工的真实写照,也是工程施工安全事故原因分析的依据,施工安全日记 ...

  7. mysql启动日志指令_简单整理MySQL的日志操作命令

    1.首先确认你日志是否启用了 ?1MySQL>show variables like'log_bin'; 如果启用了,即ON那日志文件就在MySQL的安装目录的data目录下 2.怎样知道当前的 ...

  8. 交换机日志删除_如何查看交换机报警日志 并导出日志 命令是什么

    在完成上述配置后,在任意视图下执行display命令可以显示配置后信息中心的运行情况,通过查看显示信息验证配置的效果. 在用户视图下执行resetlogbuffer命令可以将日志缓冲区的统计信息清除. ...

  9. java tomcat打印日志级别_修改tomcatlog输出等级

    /usr/local/tomcat2/webapps/digital/WEB-INF/classes/log4j.properties log4j.properties配置如下(配置是转载): log ...

最新文章

  1. Python网络编程(线程通信、GIL、服务器模型)
  2. 对于写bash脚本的朋友,read命令是不可或缺的,需要实践一下就可以了解read命令的大致用途: 编写一个脚本: #!/bin/bash # hao32 test read echo -e Pl
  3. javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景
  4. LeetCode 104二叉树的最大深度-简单
  5. TomCat使用以及端口号被占用的处理方法
  6. 美团知识图谱问答技术及在商家推荐回复场景中的实践与探索
  7. php 兼容unicode文字的字符串大小写转换,php实现兼容Unicode文字的字符串大小写转换strtolower()和strtoupper()...
  8. component_春天@Component
  9. 2020年最好用的手机是哪一款_2020年千元机中性能最好的4款手机,印象最让你深刻的是哪一款?...
  10. sed,awk,grep教程
  11. 2019年上海交通大学816自动控制理论考研经验分享
  12. python写入xml文件的方法_python写入xml文件的方法
  13. 工业机器人pallet指令_工业机器人编程指令详解
  14. (PTA)数据结构(作业)7、栈和队列应用,字符串矩阵,树
  15. Trizol法提取RNA实验步骤
  16. 前端开发优秀简历_这就是如今成为优秀的前端开发人员所需要的
  17. 转载 usb_alloc_coherent 和 usb_free_coherent
  18. 安全性剖析:对比 LayerZero、Wormhole 等跨链桥优劣
  19. potplay播放器录制音频
  20. Python爬虫入门(爬取豆瓣电影信息小结)

热门文章

  1. 前端学习(3153):react-hello-react之脚手架文件介绍
  2. 前端学习(3003):vue+element今日头条管理--关于组件中的@
  3. [jQuery] jQuery.fn的init方法返回的this指的是什么对象?为什么要返回this
  4. [vue-cli]不用vue-cli,你自己有搭建过vue的开发环境吗?流程是什么?
  5. 工作270:el-dialog的open回调
  6. 前端学习(2468):echart复习电商管理通过路由加载数据
  7. 前端学习(1952)vue之电商管理系统电商系统之级联选择器
  8. 前端学习(1860)vue之电商管理系统电商系统之渲染login组件并且实现路由重定向
  9. shiro学习(19): 拦截器
  10. mysql ndb 关闭_Mysql NDB 常见问题