Skip to content

Instantly share code, notes, and snippets.

@chy168
Created June 13, 2015 18:02
Show Gist options
  • Save chy168/f19bd7a89f4dafec4748 to your computer and use it in GitHub Desktop.
Save chy168/f19bd7a89f4dafec4748 to your computer and use it in GitHub Desktop.
test see log trace depth
package main
import (
"fmt"
"io"
"net/http"
log "github.com/cihub/seelog"
)
var appLogger log.LoggerInterface
func main() {
logger, log_err := log.LoggerFromConfigAsString(
`
<seelog minlevel="trace" maxlevel="error">
<outputs formatid="main">
<console />
</outputs>
<formats>
<format id="main" format="%Date(2006/01/02 15:04:05.000) [%Level] %Func %Msg%n"/>
</formats>
</seelog>
`)
if log_err != nil {
fmt.Printf("Logger error: %v", log_err)
panic("Cannot initialize logger from config")
}
appLogger = logger
appLogger.SetAdditionalStackDepth(1)
log.ReplaceLogger(appLogger)
defer log.Flush()
http.HandleFunc("/", hello)
http.ListenAndServe(":8000", nil)
}
func hello(w http.ResponseWriter, r *http.Request) {
f := playRequest()
f()
io.WriteString(w, "Hello world!")
}
type HandlerFunc func()
func playRequest() HandlerFunc {
return func() {
request_id := "THIS_IS_UUID"
myLogger := &ApiLogger{appLogger, request_id}
myLogger.Debug("I am runnnnning!")
}
}
// Customize logger with request-id
type ApiLogger struct {
inner log.LoggerInterface
RequestId string
}
func (sw *ApiLogger) Debug(s string) {
sw.inner.Debugf(sw.RequestId + " " + s)
}
func (sw *ApiLogger) Info(s string) {
sw.inner.Info(sw.RequestId + " " + s)
}
func (sw *ApiLogger) Error(s string) {
sw.inner.Error(sw.RequestId + " " + s)
}
func (sw *ApiLogger) Debugf(format string, params ...interface{}) {
sw.inner.Debugf(sw.RequestId+" "+format, params...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment