Select your preferred scripting language. All code snippets will be displayed in this language.
interface in UnityEngine
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseInterface for custom log handler implementation.
ILogHandler interface to ease unit-testing and mocking of loggers.
#pragma strict public class MyFileLogHandler implements ILogHandler { private var m_FileStream: FileStream; private var m_StreamWriter: StreamWriter; private var m_DefaultLogHandler: ILogHandler = Debug.unityLogger.logHandler; MyFileLogHandler { var filePath: String = Application.persistentDataPath + "/MyLogs.txt"; m_FileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); m_StreamWriter = new StreamWriter(m_FileStream); // Replace the default debug log handler Debug.unityLogger.logHandler = this; } public function LogFormat(logType: LogType, context: UnityEngine.Object, format: String, args: Object[]) { m_StreamWriter.WriteLine(String.Format(format, args)); m_StreamWriter.Flush(); m_DefaultLogHandler.LogFormat(logType, context, format, args); } public function LogException(exception: Exception, context: UnityEngine.Object) { m_DefaultLogHandler.LogException(exception, context); } } public class MyGameClass extends MonoBehaviour { private static var logger: ILogger = Debug.unityLogger; private static var kTAG: String = "MyGameTag"; private var myFileLogHandler: MyFileLogHandler; function Start() { myFileLogHandler = new MyFileLogHandler(); logger.Log(kTAG, "MyGameClass Start."); } }
using UnityEngine; using System.Collections; using System.IO; using System;
public class MyFileLogHandler : ILogHandler { private FileStream m_FileStream; private StreamWriter m_StreamWriter; private ILogHandler m_DefaultLogHandler = Debug.unityLogger.logHandler;
public MyFileLogHandler() { string filePath = Application.persistentDataPath + "/MyLogs.txt";
m_FileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); m_StreamWriter = new StreamWriter(m_FileStream);
// Replace the default debug log handler Debug.unityLogger.logHandler = this; }
public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args) { m_StreamWriter.WriteLine(String.Format(format, args)); m_StreamWriter.Flush(); m_DefaultLogHandler.LogFormat(logType, context, format, args); }
public void LogException(Exception exception, UnityEngine.Object context) { m_DefaultLogHandler.LogException(exception, context); } }
public class MyGameClass : MonoBehaviour { private static ILogger logger = Debug.unityLogger; private static string kTAG = "MyGameTag"; private MyFileLogHandler myFileLogHandler;
void Start() { myFileLogHandler = new MyFileLogHandler();
logger.Log(kTAG, "MyGameClass Start."); } }
LogException | A variant of ILogHandler.LogFormat that logs an exception message. |
LogFormat | Logs a formatted message. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information