RejectedSoftware Forums

Sign up

Custom Console Logger

I wanted to create a custom console logger to extend the functionality provided. I found that I couldn't deregister the standard logger because I couldn't get a reference to the logger instances.
I ended up having to edit the core files...

source/vibe/core/log.d

private shared(Logger)[] getLoggers() nothrow @trusted { return ss_loggers; }

became:

shared(Logger)[] getLoggers() nothrow @trusted { return ss_loggers; }

source/app.d

    auto loggers = getLoggers();
    foreach(i; loggers) {
        deregisterLogger(i);
    }

Seems to me that the getLoggers function shouldn't be private, or there should be a way to deregister all loggers.

Re: Custom Console Logger

While I was in there, I also added a "textLine" attribute to LogLine and rawLog so I could customize the first line of a multiline console output.

Re: Custom Console Logger

And in case someone wants my ConsoleLogger... Here is the log function I changed:

    override void log(ref LogLine msg)
        @trusted // FILE isn't @safe (as of DMD 2.065)
    {
        string pref;
        string color;
        File file;
        string colorNormal = "\x1B[0m";
        string colorRed = "\x1B[31m";
        string colorGreen = "\x1B[32m";
        string colorYellow = "\x1B[33m";
        string colorBlue = "\x1B[34m";
        string colorMagenta = "\x1B[35m";
        string colorCyan = "\x1B[36m";
        string colorWhite = "\x1B[37m";
        string colorReset = "\033[0m";

        color = colorGreen;

        final switch (msg.level) {
            case LogLevel.trace: pref = "trc"; file = m_diagFile; break;
            case LogLevel.debugV: pref = "dbv"; file = m_diagFile; break;
            case LogLevel.debug_: pref = "dbg"; file = m_diagFile; break;
            case LogLevel.diagnostic: pref = "dia"; file = m_diagFile; break;
            case LogLevel.info: pref = "INFO"; file = m_infoFile; break;
            case LogLevel.warn: pref = "WARN"; file = m_diagFile; color = colorYellow; break;
            case LogLevel.error: pref = "ERROR"; file = m_diagFile; color = colorRed; break;
            case LogLevel.critical: pref = "CRITICAL"; file = m_diagFile; color = colorRed; break;
            case LogLevel.fatal: pref = "FATAL"; file = m_diagFile; color = colorRed; break;
            case LogLevel.none: assert(false);
        }

        auto fmt = (msg.textLine == 1) ? this.format : this.infoFormat;

        final switch (fmt) {
            case Format.plain: file.writeln(msg.text); break;
            case Format.thread: file.writefln("[%08X:%08X %s] %s", msg.threadID, msg.fiberID, pref, msg.text); break;
            case Format.threadTime:
                auto tm = msg.time;
                file.writefln("[%08X:%08X %d.%02d.%02d %02d:%02d:%02d.%03d %s] %s",
                    msg.threadID, msg.fiberID,
                    tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second, tm.fracSec.msecs,
                    pref, msg.text);
                break;
            case Format.timeColor:
                auto tm = msg.time;
                file.writefln("[%d-%02d-%02d %02d:%02d:%02d.%03d %s%s%s] %s%s%s",
                    tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second, tm.fracSec.msecs,
                    color, pref, colorReset, color, msg.text, colorReset);
                break;
        }
        file.flush();
    }

I'm sure someone else would do better... But I'm just doing this for fun....

Re: Custom Console Logger

Am 26.02.2015 um 22:42 schrieb CanadianNomad:

I wanted to create a custom console logger to extend the functionality provided. I found that I couldn't deregister the standard logger because I couldn't get a reference to the logger instances.
I ended up having to edit the core files...

source/vibe/core/log.d

private shared(Logger)[] getLoggers() nothrow @trusted { return ss_loggers; }

became:

shared(Logger)[] getLoggers() nothrow @trusted { return ss_loggers; }

source/app.d

     auto loggers = getLoggers();
     foreach(i; loggers) {
         deregisterLogger(i);
     }

Seems to me that the getLoggers function shouldn't be private, or there should be a way to deregister all loggers.

You can disable the default logger using setLogLevel(LogLevel.none);.
But I think it might me interesting to actually integrate the color
support into the default FileLogger.