Unity自己有log系统,为什么要自己封装一个
1.不好用,只能在pc上记录log文件,移动平台是没有的
2.在开发时期的log,不想在正式版里面出现。没有一个统一的开关来控制是不是要显示log,要显示什么类型的log

那么怎么做?
我的思路是:Unity里面的所有的log,都会被 Application.logMessageReceived 捕获
只要添加一个回调来把这些log信息记录下来保存就好了
Application.logMessageReceived += ProcessExceptionReport;

代码1:log等级枚举,被用来控制是否显示log,显示哪些类型的log

namespace ShiHuanJue.Debuger
{
/// <summary>
/// log等级
/// </summary>
public enum LogLevel
{
None = ,
Debug = ,
Error = ,
Warning = ,
Exception = ,
All = LogLevel.Debug | LogLevel.Error | LogLevel.Warning | LogLevel.Exception
}
}

代码2:把log记录到文本里

using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Text; namespace ShiHuanJue.Debuger
{
public class LogWriter
{
private string m_logPath = Application.persistentDataPath + "/log/";
private string m_logFileName = "log_{0}.txt";
private string m_logFilePath = string.Empty; public LogWriter()
{
if (!Directory.Exists(m_logPath))
{
Directory.CreateDirectory(m_logPath);
}
this.m_logFilePath = this.m_logPath + string.Format(this.m_logFileName, DateTime.Today.ToString("yyyyMMdd"));
} public void ExcuteWrite(string content)
{
using (StreamWriter writer = new StreamWriter(m_logFilePath, true, Encoding.UTF8))
{
writer.WriteLine(content);
}
}
}
}

代码3:log帮助类

using UnityEngine;
using System; namespace ShiHuanJue.Debuger
{
public class LogHelper
{
static public LogLevel m_logLevel = LogLevel.All;
static LogWriter m_logWriter = new LogWriter(); static LogHelper()
{
Application.logMessageReceived += ProcessExceptionReport;
} private static void ProcessExceptionReport(string message, string stackTrace, LogType type)
{
LogLevel dEBUG = LogLevel.Debug;
switch (type)
{
case LogType.Error:
dEBUG = LogLevel.Error;
break;
case LogType.Assert:
dEBUG = LogLevel.Debug;
break;
case LogType.Warning:
dEBUG = LogLevel.Warning;
break;
case LogType.Log:
dEBUG = LogLevel.Debug;
break;
case LogType.Exception:
dEBUG = LogLevel.Exception;
break;
} if (dEBUG == (m_logLevel & dEBUG))
{
Log(string.Concat(new object[] { " [", dEBUG, "]: ", message, '\n', stackTrace }));
}
} /// <summary>
/// 加上时间戳
/// </summary>
/// <param name="message"></param>
private static void Log(string message)
{
string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff") + message;
m_logWriter.ExcuteWrite(msg);
} static public void Log(object message)
{
Log(message, null);
}
static public void Log(object message, UnityEngine.Object context)
{
if (LogLevel.Debug == (m_logLevel & LogLevel.Debug))
{
Debug.Log(message, context);
}
}
static public void LogError(object message)
{
LogError(message, null);
}
static public void LogError(object message, UnityEngine.Object context)
{
if (LogLevel.Error == (m_logLevel & LogLevel.Error))
{
Debug.LogError(message, context);
}
}
static public void LogWarning(object message)
{
LogWarning(message, null);
}
static public void LogWarning(object message, UnityEngine.Object context)
{
if (LogLevel.Warning == (m_logLevel & LogLevel.Warning))
{
Debug.LogWarning(message, context);
}
}
}
}

游戏上线之前把LogHelper.m_logLevel = LogLevel.None;Log就不显示了。
LogHelper.m_logLevel = LogLevel.Error;是显示Error。
LogHelper.m_logLevel = LogLevel.Debug | LogLevel.Error;同时显示Debug和Error。

log文件是保存在 private string m_logPath = Application.persistentDataPath + “/log/”;在手机上就是沙盒路径。
log文件是以天为单位,同一天的log会被记录到一个文件里面。

怎么用呢?

using UnityEngine;
using ShiHuanJue.Debuger; public class Test : MonoBehaviour
{
void Start()
{
LogHelper.m_logLevel = LogLevel.All;
LogHelper.Log("debug");
LogHelper.LogError("error");
LogHelper.LogWarning("warning"); GameObject go = GameObject.Find("fsdfsd");
GameObject.Instantiate(go);
}
}

这里测试了几种典型的情况,包括找不到go,实例化的时候系统报的异常
有图有真相~!

查看一下log文件

最后我封装了一个dll,直接放到工程下头就可以用了。
大家也可以在这里下载:链接: http://pan.baidu.com/s/1qWFtQOG 密码: erpx
代码都在上面,你们也可以去生成。

自己封装一个Log模块的更多相关文章

  1. 【Android自动化】编写一个log模块,输出至控制台,供程序运行查看

    # -*- coding:utf-8 -*- import logging def get_log(name): log = logging.getLogger(name) log.setLevel( ...

  2. go: 一个通用log模块的实现

    在go里面,虽然有log模块,但是该模块提供的功能并不强,譬如就没有我们常用的level log功能,但是自己实现一个log模块也并不困难. 对于log的level,我们定义如下: const ( L ...

  3. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  4. Python之自定义封装一个简单的Log类

    参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...

  5. Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》

    Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676

  6. frp源码剖析-frp中的log模块

    前言&引入 一个好的log模块可以帮助我们排错,分析,统计 一般来说log中需要有时间.栈信息(比如说文件名行号等),这些东西一般某些底层log模块已经帮我们做好了.但在业务中还有很多我们需要 ...

  7. 代码改变世界 | 如何封装一个简单的 Koa

    下面给大家带来:封装一个简单的 Koa Koa 是基于 Node.js 平台的下一代 web 开发框架 Koa 是一个新的 web 框架,可以快速而愉快地编写服务端应用程序,本文将跟大家一起学习:封装 ...

  8. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  9. Android架构分析之LOG模块

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 Andro ...

随机推荐

  1. RAID技术介绍

    RAID技术介绍 简介 RAID是一个我们经常能见到的名词.但却因为很少能在实际环境中体验,所以很难对其原理 能有很清楚的认识和掌握.本文将对RAID技术进行介绍和总结,以期能尽量阐明其概念. RAI ...

  2. asp.net中套用母版页之后的findcontrol

    套用模板页之后,如果要在内容页中查找某个控件,需要先找到模板页中的ContentPlaceHolder,在通过ContentPlaceHolder查找代码,如下: LinkButton btn = t ...

  3. cf126b(kmp好题)

    http://codeforces.com/contest/126/problem/B #include<bits/stdc++.h> using namespace std; const ...

  4. linux git安装及配置(包括更新)

    1.在终端运行命令 sudo apt-get install git 2.查看版本号 git --version  (若不是最新可更新 自选) 更新提示: sudo add-apt-repositor ...

  5. Socket通信(一)

    代码及PDF下载链接:http://download.csdn.net/detail/u010312811/9683034 例程1:实现服务器与客户端的连接与简单数据收发 参考链接:http://bl ...

  6. ABAP 数量单位转换

    CALL FUNCTION 'UNIT_CONVERSION_SIMPLE'           EXPORTING             input                = wa_ite ...

  7. yii中的cookie的发送和读取

    cookies: //新创建的cookie会从本地传到服务器上,然后从服务器获取. (1) cookie的发送 $cookies = Yii::$app->response->cookie ...

  8. Centos7设置关闭防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,要想使用iptables必须重新设置一下. 1.关闭防火墙 [root@localhost ~]# systemctl stop firew ...

  9. iOS TabeView 头视图和尾视图不滑动的实现

    因项目有需求不能让section中的头尾视图滑动,顾根据网上的一些资料和自己整理的思路,已实现了不滑动效果,上代码,啥都说了,搞了2个小时都是泪.... 1.创建一个tableview _mainTa ...

  10. NSString使用stringWithFormat拼接的相关知识

    NSString使用stringWithFormat拼接的相关知识 保留2位小数点 1 2 3 4 //.2代表小数点后面保留2位(2代表保留的数量) NSString *string = [NSSt ...