Another Log implementation
Vincent
thornik at gmail.com
Sat May 28 08:08:58 PDT 2011
Hello, guys!
I see a lot of discussions about std.Log implementation, that I couldn't
pass along. I use logs a long time, but some ideas of opponents looks to
me obscure.
Of course, different commands have different requirements, but I think
that such DUMB AND SIMPLE task as logging should not transform to 'one
fits all' dump of monster classes.
Logging must conform most commonly used usages, all other 'features' is
the pain of 'offbeat' developers. Also my 'fresh look' at 'verbosity':
this is obsolete point of programmers from 70-th. Modern development has
no time for 'verbosity' like this:
log(Verbosity1, "var i greater than zero")
log(Verbosity2, "var i is "~ to!string(i))
...etc...
My second point is that libraries must benefit from used language, what
leads to the third point: logging from other languages(libraries) must be
used only as an 'idea source', since their logging based on capabilities
of _their_ languages.
In this light I made this draft for logging, may be it'll cold/delight
people with its simplicity.
Let's start from usage:
====================================================================================
version(logging) { // we benefit from D (conditional compilation)
// Different 'severity' emulated with different log objects
auto logWrn = new Log(new FileLogEngine(`c:\res.txt`), new
DBLogEngine(`server=.;database=ABC`));// log can be done to many
destinations
auto logErr = new Log();// by default log outputs to the screen.
auto logCrash = new Log(new EmailLogEngine(`smtp.myserver.com`);// log
output is not only a file!
}
try {
// some action
} catch (LightException lex) {
version(logging) logWrn(`Problem!`); // simple usage. Non-critical
messages going to its own log.
} catch (Exception ex) {
version(logging) logCrash(`Something serious happen! %s`, ex);//
logging with formatting. No necessaries for 'severity' - critical stuff
gone to critical log only.
}
version(logging) { writeln(`Log disabled`); logDbg.IsEnabled = false; }
version(logging) logDbg(`Disabled message`);// logging is disabled at
runtime
====================================================================================
Like it? Here is the code! http://pastebin.com/WwkrD02g
Note on recordTemplate field - customization for log records, which allow
even XML.
Since it's 'one hour draft', be lenient in details - my goal is to show
conception, but of course I'm happy to discuss any improvements!
Vincent.
More information about the Digitalmars-d
mailing list