Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Created May 17, 2018 14:56
Show Gist options
  • Save paulhayes/aab2098c4aa93c2fd6842d7d87e9f82b to your computer and use it in GitHub Desktop.
Save paulhayes/aab2098c4aa93c2fd6842d7d87e9f82b to your computer and use it in GitHub Desktop.
Simple Logging System
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class Logger : ScriptableObject
{
public const int Verbose = (int)Levels.Verbose;
public const int Debug = (int)Levels.Debug;
public const int Info = (int)Levels.Info;
public const int Warn = (int)Levels.Warn;
public const int Error = (int)Levels.Error;
public Levels maxLevel = Levels.Warn;
public Logger parent;
public System.Action<int,string> LogHandler;
public enum Levels : int
{
Verbose = 9,
Debug = 7,
Info = 5,
Warn = 3,
Error = 1
}
public void Log(int level, object msg)
{
if( (int)maxLevel <= level ){
if( LogHandler != null ){
LogHandler(level, msg.ToString());
}
else if( parent != null ){
parent.Log(level, msg);
}
else {
DebugLog(level, msg.ToString());
}
}
}
public void LogFormat(int level, string format, params object[] objs)
{
Log( level, string.Format( format, objs ) );
}
void DebugLog(int level, string msg)
{
if (level == Error)
{
UnityEngine.Debug.LogError(msg);
}
else if (level == Warn)
{
UnityEngine.Debug.LogWarning(msg);
}
else {
UnityEngine.Debug.Log(msg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment