Skip to content

Instantly share code, notes, and snippets.

@erayerdin
Last active April 11, 2020 12:29
Show Gist options
  • Save erayerdin/8bc4033b4441adfb313053db62458b45 to your computer and use it in GitHub Desktop.
Save erayerdin/8bc4033b4441adfb313053db62458b45 to your computer and use it in GitHub Desktop.
Godot C# Logger Implementation Example
MIT License
Copyright (c) 2020 Eray Erdin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Godot C# Logger Implementation Example

Yes, I know it probably is the worst logging implementation you've ever seen. I'm kinda new to C#. When I write in C#, I usually trust my Java knowledge.

I can already see issues here. Like, what about file logging? I don't really know but I will update the code when I require more.

How to Use

After you put Logger.cs into your codebase, you can initialize Logger in almost anywhere. It's usually good to do it as early as possible.

class MyNode: Node2D {
  private Logger logger;  // in order to use it anywhere
  
  public override void _Ready()
  {
    logger = new Logger(this);
    
    logger.log(LogLevel.TRACE, "This is a trace log.");
    // [TRACE][node_name@MyNode: Node2D] This is a trace log.
    logger.trace("This is a trace log.");
    // [TRACE][node_name@MyNode: Node2D] This is a trace log.
    logger.debug("This is a debug log.");
    // [DEBUG][node_name@MyNode: Node2D] This is a debug log.
    logger.info("This is an info log.");
    // [INFO][node_name@MyNode: Node2D] This is an info log.
    logger.warning("This is a warning log.");
    // [WARNING][node_name@MyNode: Node2D] This is a warning log.
    logger.error("This is an error log.");
    // [ERROR][node_name@MyNode: Node2D] This is an error log.
  }
}
using System;
using static Godot.GD;
using Godot;
public enum LogLevel
{
TRACE,
DEBUG,
INFO,
WARNING,
ERROR
}
public class Logger
{
private static char[] DELIMITERS = { '[', ']' };
private string name;
private string klass;
private string type;
public Logger(Node loggee)
{
name = loggee.Name;
type = loggee.GetClass();
klass = loggee.GetType().Name;
}
private static string GetLevelName(LogLevel level)
{
switch (level)
{
case LogLevel.TRACE:
return "TRACE";
case LogLevel.DEBUG:
return "DEBUG";
case LogLevel.INFO:
return "INFO";
case LogLevel.WARNING:
return "WARNING";
case LogLevel.ERROR:
return "ERROR";
default:
return "UNKNOWN";
}
}
private string GetHeader(LogLevel level)
{
var level_name = GetLevelName(level);
return $"{DELIMITERS[0]}{level_name}{DELIMITERS[1]}" +
$"{DELIMITERS[0]}{name}@{klass}: {type}{DELIMITERS[1]}";
}
public void log(LogLevel level, string message)
{
Print($"{GetHeader(level)} {message}");
}
public void trace(string message)
{
log(LogLevel.TRACE, message);
}
public void debug(string message)
{
log(LogLevel.DEBUG, message);
}
public void info(string message)
{
log(LogLevel.INFO, message);
}
public void warning(string message)
{
log(LogLevel.WARNING, message);
}
public void error(string message)
{
log(LogLevel.ERROR, message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment