GoLog adds level based logging to logger(s) from standard library.
GoLog's API is designed to be expressive with sensible configuration defaults and to be easy to use.
go get -u github.com/codemedic/go-log
To get started, import the library and use one of the constructor functions, wrapped with log.Must. Make sure the
logger is closed, once you are done with it, using defer l.Close(). Now you are all set to start logging.
package main
import golog "github.com/codemedic/go-log"
func main() {
// create syslog logger
l := golog.Must(golog.NewSyslog())
// make sure resources are freed up when we are done
defer l.Close()
// hello to the world
l.Print("hello world!")
}NOTE
Functionslog.Printandlog.Printflogs toDebuglevel by default. It is preferable to use a method that log to a specific level. The level logged to bylog.Printandlog.Printfcan be changed usingWithPrintLevel.
You can find more examples here.
The methods below provides leveled logging. They follow the same pattern as fmt.Print and fmt.Printf and uses the
same format specification.
// Log string message at specific levels
Debug(value ...interface{})
Info(value ...interface{})
Warning(value ...interface{})
Error(value ...interface{})
// Log formatted string message at specific levels, similar to log.Printf from standard library
Debugf(format string, value ...interface{})
Infof(format string, value ...interface{})
Warningf(format string, value ...interface{})
Errorf(format string, value ...interface{})package main
import (
"errors"
golog "github.com/codemedic/go-log"
)
func main() {
l := golog.Must(golog.NewSyslog())
defer l.Close()
l.Debug("debug message")
l.Debugf("formatted %s message", "debug")
l.Info("informational message")
l.Infof("formatted %s message", "informational")
l.Warning("warning message")
l.Warningf("formatted %s message", "warning")
l.Error("error message")
l.Errorf("formatted %v message", errors.New("error"))
}See documentation for all available Options.
package main
import golog "github.com/codemedic/go-log"
func main() {
l := golog.Must(golog.NewSyslog(
golog.OptionsMust(golog.Options(
golog.WithLevelFromEnv("LOG_THRESHOLD", golog.Info),
golog.WithSourceLocationFromEnv("LOG_CALLER_LOCATION", "short"),
golog.WithSyslogTag("my-test-app"),
))))
defer l.Close()
l.Info("hello world!")
}Logging via standard logger is handled by default. This is meant for cases where the logging via the standard library is
outside your control; a library used in your project for example. Those will be logged at Info level, but this
behaviour can be customised using WithStdlogSorter.
package main
import (
"bytes"
golog "github.com/codemedic/go-log"
)
func sortStdlog(b []byte) golog.Level {
switch {
case bytes.HasPrefix(b, []byte("WARNING")):
fallthrough
case bytes.HasPrefix(b, []byte("ERROR")):
return golog.Warning
case bytes.HasPrefix(b, []byte("DEBUG")):
return golog.Disabled
default:
return golog.Info
}
}
func main() {
l, _ := golog.NewSyslog(golog.WithStdlogSorter(sortStdlog))
defer l.Close()
l.Info("hello world!")
}