Skip to content

Instantly share code, notes, and snippets.

@nin-jat
Last active October 12, 2019 13:50
Show Gist options
  • Save nin-jat/0009abf8e028c0c573c16872b3500d00 to your computer and use it in GitHub Desktop.
Save nin-jat/0009abf8e028c0c573c16872b3500d00 to your computer and use it in GitHub Desktop.
A Unity logger that writes to files instead of your editor console.

How to use

Read about unity's loggers here, https://docs.unity3d.com/ScriptReference/Logger.html

//create a logger.
var logger = new Logger(new FileLogger("my-log-file"));

// Log something to a log file.
logger.Log(LogType.Log, "Hello World!");

What it looks like

12/10/2019 9:27:39 PM  Log        Resourcepacks/default/assets/minecraft/blockstates/acacia_trapdoor.json
12/10/2019 9:27:39 PM  Log        Inserted acacia_trapdoor with 16 variants.
12/10/2019 9:27:39 PM  Log        Resourcepacks/default/assets/minecraft/blockstates/acacia_wood.json
12/10/2019 9:27:39 PM  Log        Inserted acacia_wood with 3 variants.
12/10/2019 9:27:39 PM  Log        Resourcepacks/default/assets/minecraft/blockstates/activator_rail.json
12/10/2019 9:27:39 PM  Log        Inserted activator_rail with 12 variants.
12/10/2019 9:27:39 PM  Log        Resourcepacks/default/assets/minecraft/blockstates/air.json
12/10/2019 9:27:39 PM  Exception  Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: t. Path '', line 6, position 0.
  at Newtonsoft.Json.JsonTextReader.Read () [0x000c7] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <6e2e10d078be42bcb47e6374224b596a>:0 
  at Pack.ResourcePack.loadAllStates () [0x00051] in /home/tom/Documents/git-repos/Minecraft-Unity3D/Minecraft Client/Assets/_Project/Scripts/ResourcePacks/ResourcePack.cs:107 
12/10/2019 9:27:39 PM  Log        Resourcepacks/default/assets/minecraft/blockstates/allium.json
12/10/2019 9:27:39 PM  Log        Inserted allium with 1 variants.
12/10/2019 9:27:39 PM  Log        Resourcepacks/default/assets/minecraft/blockstates/andesite.json
12/10/2019 9:27:39 PM  Log        Inserted andesite with 1 variants.
// Created by Those45Ninjas.
// https://gist.github.com/Those45Ninjas/0009abf8e028c0c573c16872b3500d00
using UnityEngine;
using System.IO;
using System;
public class FileLogger : ILogHandler
{
public TextWriter Writer;
public bool NeedsFlush = false;
public readonly string LogFile;
public FileLogger(string file)
{
// Create the Logs directory if needed.
if(!Directory.Exists("Logs"))
Directory.CreateDirectory("Logs");
// Create or overwrite the log file.
LogFile = Path.Combine("Logs", file + ".log");
Writer = File.CreateText(LogFile);
}
// Log an exception by calling Log format with the exception as the message.
public void LogException(Exception exception, UnityEngine.Object context)
{
LogFormat(LogType.Exception, context, exception.ToString());
// Just in case the game crashes, flush on every exception that comes in.
Writer.Flush();
}
public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
{
// Log the date, type and message.
Writer.Write(DateTime.Now.ToString().PadRight(23));
Writer.Write(logType.ToString().PadRight(11));
Writer.WriteLine(format, args);
// Update the needs flush flag.
NeedsFlush = true;
}
// Call this every fixed update to keep the log file up to date without incomplete lines.
public void Flush()
{
// Only flush if something was changed.
if(!NeedsFlush)
return;
Writer.Flush();
NeedsFlush = false;
}
// Close the file when this object is cleared.
~FileLogger()
{
Writer.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment