alexcesaro / log Goto Github PK
View Code? Open in Web Editor NEWLogging packages for Go
License: MIT License
Logging packages for Go
License: MIT License
I am working on a command line application and am using stdlog. Currently, the only way I can see to set the log level is to use GetFromFlags(). Although this works, by default the logging level is set to "Info". What I would like to be able to do is define my own flag and pass the value of that flag to a function such as setLoggingLevel. This would allow me to have the set the default value for the flag as "none" (or I suppose any other non-info level) and have the logger function otherwise the same.
I'd like to propose adding a NullLogger
instance to the log
package which implements a no-op version of log.Logger
.
This is obviously a minor improvement, but I run into it often enough that I figure it's worth asking. :) I'd be happy to do a PR for this if you'd like.
In all of my subpackages, I like to use a package-global log.Logger instance, it typically looks like this:
package foo
import (
"io/ioutil"
"github.com/alexcesaro/log"
"github.com/alexcesaro/log/golog"
)
var logger log.Logger
func SetLogger(l log.Logger) {
logger = l
}
func init() {
// Set a default null logger
SetLogger(golog.New(ioutil.Discard, log.None))
}
Ideally, I'd like for this boilerplate file to be more like:
package foo
import "github.com/alexcesaro/log"
var logger log.Logger = log.NullLogger
func SetLogger(l log.Logger) {
logger = l
}
Hi there, this may be out of scope for your module but you may like to know that there is a race condition warning for when this module is used with goroutines.
Exhibited by this test case (in go v1.2.2):
package main
import (
"os"
"github.com/alexcesaro/log"
"github.com/alexcesaro/log/golog"
)
var logger *golog.Logger
func main() {
logger = golog.New(os.Stderr, log.Debug)
done := make(chan struct{})
logger.Info("Test.")
go func() {
logger.Info("Test.")
done <- struct{}{}
}()
logger.Info("Test.")
<-done
}
This yields warnings like this...
$ go run -race main.go
2014-05-20 15:43:44.294 INFO Test.
2014-05-20 15:43:44.298 INFO Test.
==================
WARNING: DATA RACE
Write by goroutine 3:
runtime.copy()
/usr/local/Cellar/go/1.2.2/libexec/src/pkg/runtime/slice.c:120 +0x0
bytes.(*Buffer).Write()
/usr/local/Cellar/go/1.2.2/libexec/src/pkg/bytes/buffer.go:128 +0x11d
github.com/alexcesaro/log/golog.addTimestamp()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:252 +0x394
github.com/alexcesaro/log/golog.func·001()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:225 +0x34
github.com/alexcesaro/log/golog.(*Logger).output()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:215 +0xec
github.com/alexcesaro/log/golog.(*Logger).Info()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:129 +0x57
main.func·001()
/Users/shazow/tmp/go/main.go:19 +0xf1
Previous read by main goroutine:
syscall.raceReadRange()
/usr/local/Cellar/go/1.2.2/libexec/src/pkg/syscall/race.go:25 +0x3f
syscall.Write()
/usr/local/Cellar/go/1.2.2/libexec/src/pkg/syscall/syscall_unix.go:153 +0xb9
os.(*File).write()
/usr/local/Cellar/go/1.2.2/libexec/src/pkg/os/file_unix.go:194 +0x9d
os.(*File).Write()
/usr/local/Cellar/go/1.2.2/libexec/src/pkg/os/file.go:139 +0xbb
github.com/alexcesaro/log/golog.func·002()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:285 +0x58
github.com/alexcesaro/log/golog.(*Logger).output()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:220 +0x236
github.com/alexcesaro/log/golog.(*Logger).Info()
/Users/shazow/local/go/src/github.com/alexcesaro/log/golog/golog.go:129 +0x57
main.main()
/Users/shazow/tmp/go/main.go:23 +0x3a5
Goroutine 3 (running) created at:
main.main()
/Users/shazow/tmp/go/main.go:21 +0x2dc
For comparison, using the stdlib "log"
module works fine:
package main
import (
"log"
"os"
)
var logger *log.Logger
func main() {
logger = log.New(os.Stderr, "", log.LstdFlags)
done := make(chan struct{})
logger.Print("Test.")
go func() {
logger.Print("Test.")
done <- struct{}{}
}()
logger.Print("Test.")
<-done
}
$ go run -race main.go
2014/05/20 15:47:27 Test.
2014/05/20 15:47:27 Test.
2014/05/20 15:47:27 Test.
By the way, thank you for building this otherwise-excellent logging module. :) It's exactly what I needed.
Making a proper release helps packaging your software by 3rd-party vendors like linux distros or BSDs.
It's super easy on github, please tag a release.
Many thanks,
Alex
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.