log.Stderr(2, f(), g())
then f and g would be called even if the logging level was only 1. For many cases that's not important, but I've seen cases in other languages where similar issues have dominated performance in some applications.
As a result, the log package is less nice to use - you must say
if logLevel >= 2 {
log.Stderr(f(), g())
}
which is indeed less convenient but will only call f and g when their values are needed.
(This is one area where macros would actually help, by hiding the if statement for you but I'd still rather not have macros.)
I'm happy to see your package available but I would prefer that the standard library not provide a level feature.
-rob
You could create an init function in the package that sets up a global
logger instance, rather than (or as another option to) passing a
*Logger around.
From a cursory glance at the code, it looks like you have various data
structures (maps, vectors) that might be updated simultaneously if a
message is logged from two goroutines at once. This will cause data
corruption and other weirdness.
One approach to solving this problem would be to create a buffered
channel of LogRecords as part of your Logger structure, and have a
call to Log simply perform a message send to that channel. Then you
can have a goroutine (launched upon calling NewLogger) that reads from
that channel and writes out log messages (and whatever else is
currently in Log).
> Do you guys like this sort of logging? Have you used it in Java and
> (dis)liked it? I'm sure other similar libraries exist as well.
I've used logging a bit like this with Python's logger package. I
never found it as useful as it appeared to be. It must suit some
people's programming style, though, as I believe libraries like this
are quite popular.
> Should I see about merging (what I hesitantly call) my improvements into the
> standard log package? As a subpackage of log? Should I submit this for
> inclusion as a separate package to log?
This package doesn't seem to offer improvements over the standard log
package that justify its complexity. We're trying to keep the standard
library as simple and universal as possible.
> Should I maintain this as a third party package? Do you think enough people
> would actually use it to make maintaining it as a third party package
> worthwhile? Logging doesn't exactly seem like something people look around
> for in the external package list.
Definitely maintain this as a third-party package! Push it to a
repository at github/bitbucket/googlecode, and then people can easily
install it with goinstall:
http://golang.org/cmd/goinstall
If people have a use for it, they will find it. As our user-base
grows, we hope the external library ecosystem will become more visible
and accessible. (We also have some plans to improve the godashboard
package list.)
Cheers,
Andrew