- Home /
How to use Application.logMessageReceived for logging ?
Hi,
I want to implement a logging system in my app where i can see filter log according to logtype. But i didnt know how to do this, so after researching a while i found this and Application.RegisterCallbacks but it seems to have deprecated, please help me, i cant understand how i supposed to use and implement this:
http://docs.unity3d.com/ScriptReference/Application-logMessageReceived.html
I am not a pro programmer, just a rookie willing to gain more knowledge.
hello,do you solve the problem?,hello,do you solve the problem now?
I used my own custom wrapper class which encapsulated Debug.Log, Debug.LogWarning and Debug.LogError, Debug.LogAssertion. You also check out this [Logger][1] by Unity for more handling. [1]: https://docs.unity3d.com/ScriptReference/Debug-logger.html
Answer by Gekon · Aug 11, 2015 at 04:50 AM
You have to wrap the code from the link into a component and attach it to the scene:
public class ExternalLoggerComponent : MonoBehaviour
{
public void Awake()
{
Debug.Log("ExternalLoggerComponent -> Awake");
}
public void OnEnable()
{
Debug.Log("ExternalLoggerComponent -> OnEnable");
Application.logMessageReceivedThreaded += HandleLog;
}
public void OnDisable()
{
Debug.Log("ExternalLoggerComponent -> OnDisable");
Application.logMessageReceivedThreaded -= HandleLog;
}
public void HandleLog(string logString, string stackTrace, LogType type)
{
// do some stuff
}
}
Answer by Mr-lean · Jan 02, 2017 at 10:15 AM
wo have a sctipt for the logMessageReceived .Just set the script into your MainCamera. I found it in a blog(http://www.cnblogs.com/tianyajuanke/p/4867249.html)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class LogHandler : MonoBehaviour
{
//是否打开日志所在文件夹,默认不打开
public bool openLogDir = false;
//最多显示多少条日志到屏幕
public int showLogSize = 3;
//设置过滤显示到屏幕的关键字,多个关键字用 | 隔开
public string filterString = "";
//日志存储路径
private string logSavePath;
//存储显示到屏幕上的日志
private List<string> logList;
void Start () {
logList = new List<string>();
logSavePath = Application.persistentDataPath + "/log";
if(openLogDir)
{
Application.OpenURL (Application.persistentDataPath);
}
//将本次日志与上次启动时的日志分离开
using(StreamWriter writer = new StreamWriter(logSavePath, true, Encoding.UTF8))
{
writer.WriteLine("\n\n----------------------------- 日志分隔线 -----------------------------");
writer.WriteLine("----------------------------- "+ System.DateTime.Now +" -----------------------------");
}
//注册日志处理函数
Application.logMessageReceived += HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type)
{
//将所有日志写入到日志文件
using(StreamWriter writer = new StreamWriter(logSavePath, true, Encoding.UTF8))
{
writer.WriteLine(logString + "\n\t\t" + type + ": " + stackTrace.Replace("\n","\n\t\t"));
}
//设置过滤条件,将指定类型、包含某些字符串的日志保存到屏幕日志窗器中
bool show = false;
//置过滤条件:指定类型
if (type == LogType.Error || type == LogType.Exception || type == LogType.Warning)
{
show = true;
}
//置过滤条件:包含指定关键字,多个关键字用 | 隔开
foreach (string str in filterString.Split('|'))
{
if(logString.Contains(str))
{
show = true;
break;
}
}
if(show)
{
logList.Add (logString);
if (logList.Count > showLogSize)
{
logList.RemoveAt(0);
}
}
}
void OnGUI()
{
GUI.color = Color.red;
for (int i = 0; i < logList.Count; ++i)
{
GUILayout.Label(logList[i]);
}
}
}
You can learn it for yourself.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Let Debug.LogError lead to custom file 0 Answers
Spatial Mapping 0 Answers
Why am I seeing: error CS0117: `Debug' does not contain a definition for `LogWarning' 1 Answer
Distribute terrain in zones 3 Answers