Logger for D (was: Using glog's design for Phobos?)

Denis Koroskin 2korden at gmail.com
Fri Aug 27 19:29:53 PDT 2010


On Sat, 28 Aug 2010 05:54:40 +0400, so <so at so.do> wrote:

> On Sat, 28 Aug 2010 04:18:52 +0300, sybrandy <sybrandy at gmail.com> wrote:
>
>> Here's my current version of the logger.  All of the documentation
>> should be in the logger.d file.  I also provided a sample config file
>> (logger.conf) and a sample test file that I was using for benchmarking
>> (main.d).  Let me know if you have any questions.  Feedback on improving
>> this is also welcome.
>>
>> The one thing I forgot to note in here is that the function that writes
>> to the log is passed into the second version of the constructor, which
>> needs to be used only once in the code.  A simple log writer is included
>> at the bottom of logger.d that does work.  The reason I did this was
>> when I initially asked for feedback, I got several ideas on how the
>> logger can be improved.  Also, I discussed this with a friend of mine
>> who stated that people may want to log to something other than a file,
>> such as syslog.  So, instead of trying to make the one "end all be all"
>> logger, I felt that by allowing it to accept a function as a parameter
>> to the constructor, the person can write to any type of log file,
>> database, whatever they want and the logger interface works the same no
>> matter what.
>>
>> There currently is a bug somewhere that I don't understand.  Even though
>> I have "OwnerTerminated" as part of my receive statement, I still see
>> exceptions when the program closes.  However, it doesn't seem to affect
>> the execution of the logger.
>>
>> Enjoy.
>>
>> Casey
>
> Hello?
> Couldn't it be just like this?
>
> enum log_level {
>    note, // or info whatever
>    warning,
>    error,
>    fatal,
> }
>
> string log_direct(string filename); // returns last log file directed.
> void log(A...)(A a)
> {
>    if(typof(a[0]) == log_level)
>      do_level_thingy();
>
>    ... // rest.
> }
>
> version(debug)  void debug_log(A...)(A a) { log(a); }
> else            void debug_log(A...)(A a) {}
>
> void main()
> {
>    std::string old_file = log_direct(new_file); // if you like to change  
> it that is
>    log("log me pretty", arg1, ....);
>    log(error, "log me pretty error", arg1, ...);
>    log(warning, "log me pretty warning", arg1, ...);
> }
>
> Isn't that good/beautiful/flexible enough?
> Please don't start with instantiated loggers, they are ugly so ugly and  
> unnecessary!
>
> Thanks.
>

Why wouldn't you just keep using writefln for your logging?

I'm afraid you misunderstand why people use logging libraries. I'll try to  
explain: once your project grows big, the amount of log it starts to  
generate may grow *HUGE*. Once there, you understand the importance of  
configuring every bit of the logger. Some logs needs to be disabled, some  
logs need to go to console, some logs need to file A, some to file B etc.

That's why it is a standard practice to have *independent* loggers for  
most of your classes. I refuse to use anything that dumps all the logs  
into the same bin - it's a joke and not a logging framework.


More information about the Digitalmars-d mailing list