bbbscarter / UberLogger

Replacement logging framework for Unity, with a new editor and in-game console
MIT License
475 stars 62 forks source link

Utility class for logging to channel #43

Open astruyk opened 6 years ago

astruyk commented 6 years ago

Added a utility class that wraps calls to Log*Channel(...) so that you can get code completion and don't have to worry about typing the channel name correctly each time.

My use case looks like this: 1) Make a static class with a number of UberLoggerChannel as members:

public static class Logger {
    public static readonly UberLoggerChannel Core = new UberLoggerChannel("Core");
    public static readonly UberLoggerChannel Achievements = new UberLoggerChannel("Achievements ");
    // .... More channels
}

Then usage looks like:

Logger.Core.Log("Foo");
Logger.Achievements.LogWarning("Bar");

And you get compile-time support (code completion, error checking, find-references, etc..). This is really useful when you want to standardize the usage of channels across a team because you don't have to remember capitalization and channel names.

Also you can easily turn on/off these channels without sprinkling your code with if (muteMyChannel) { Debug.Log("...."); } checks:

For example:

public static class Logger {
    public static readonly UberLoggerChannel ObscureSystem = new UberLoggerChannel("ObscureSystem");
    // ... Other channels

    static Logger() {
        ObscureSystem.Mute = true;
    }
};
Kalmalyzer commented 6 years ago

This is neat. I like the lightweight-ness of your implementation, that's quite in line with core UberLogger. One suggestion: change the "mute" flag into a filter level; thus, you can choose to mute just messages from an obscure system. I find that it is common to want to mute messages, but not warnings/errors. Example here. With filter level muting I'd vote for including this into core UberLogger.

I've made a similar but more enterprise-y thing. It has an inspector + a source code generation step. The code generation allows for removal of the call sites for muted channels -> good for projects with lots of logging. It is a bit more clunky to use; the editor is still a bit clunky, and if the user generates source code for a bad configuration it is difficult to go back without reverting all changes to the configuration asset. I think what you have made is more suitable for core UberLogger.

astruyk commented 6 years ago

Added flags to allow you to filter out messages now. You can now filter out normal .Log(...) calls (for example) by using something like ObscureSystem.Filter = UberLoggerChannel.Filters.HideLogs; or ObscureSystem.Filter = UberLoggerChannel.Filters.HideLogs | UberLoggerChannel.Filters.HideWarnings;