Skip to content

Instantly share code, notes, and snippets.

@griv
Created April 29, 2015 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griv/6093449a4c059289da2f to your computer and use it in GitHub Desktop.
Save griv/6093449a4c059289da2f to your computer and use it in GitHub Desktop.
Simple log to file for Unity3D apps. Designed for long running standalone application, logs a file per run in year / month folders.
using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
// Taken from here: http://www.thegameengineer.com/blog/2014/02/09/unity-11-logging/
// and adapted to my needs - per run log files, in year / month folders
// Steve Berrick <steve@berrick.net>
public class Dbg : MonoBehaviour
{
//-------------------------------------------------------------------------------------------------------------------------
public string LogFile = "log.txt";
public bool EchoToConsole = true;
public bool AddTimeStamp = true;
//-------------------------------------------------------------------------------------------------------------------------
private StreamWriter OutputStream;
//-------------------------------------------------------------------------------------------------------------------------
static Dbg Singleton = null;
//-------------------------------------------------------------------------------------------------------------------------
public static Dbg Instance {
get { return Singleton; }
}
//-------------------------------------------------------------------------------------------------------------------------
void Awake() {
if (Singleton != null) {
UnityEngine.Debug.LogError("Multiple Dbg Singletons exist!");
return;
}
Singleton = this;
// create year / month directories in the persistant data path
string LogDirectory = Application.persistentDataPath + "/logs/" + System.DateTime.Now.ToString("yyyy") + "/" + System.DateTime.Now.ToString("MM");
// actually create the directory if it doesnt exist
Directory.CreateDirectory(LogDirectory);
// set logfile name to be datestamped = new file for each run
LogFile = LogDirectory + "/log_" + System.DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";
// open stream
OutputStream = new StreamWriter( LogFile, true );
}
//-------------------------------------------------------------------------------------------------------------------------
void OnDestroy() {
if ( OutputStream != null ) {
OutputStream.Close();
OutputStream = null;
}
}
//-------------------------------------------------------------------------------------------------------------------------
private void Write( string message ) {
if ( AddTimeStamp ) {
DateTime now = DateTime.Now;
message = string.Format("[{0:H:mm:ss:ffff}] {1}", now, message );
}
if ( OutputStream != null ) {
OutputStream.WriteLine( message );
OutputStream.Flush();
}
if ( EchoToConsole ){
UnityEngine.Debug.Log( message );
}
}
//-------------------------------------------------------------------------------------------------------------------------
public static void Log( string Message) {
if ( Dbg.Instance != null ) {
Dbg.Instance.Write( Message );
} else {
UnityEngine.Debug.Log( Message );
}
}
}
@bradzoob
Copy link

Nice work! perhaps an addition to this would be to check disk free and purge oldest logs when free disk is under a certain threshold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment