iris
iris copied to clipboard
question about iris logger install
here is my code
main.go
app := iris.New()
l := logrus.New()
l.SetReportCaller(true)
l.SetFormatter(&logrus.JSONFormatter{})
app.Logger().Install(l)
app.Logger().Println(123)
i got this
{"file":"/Users/sado/go/pkg/mod/github.com/kataras/[email protected]/integration.go:28","func":"github.com/kataras/golog.integrateExternalLogger.func1","level":"info","msg":"123","time":"2020-12-05T11:41:27+08:00"}
when i use this in my router controller, i got this
{"file":"/Users/sado/go/pkg/mod/github.com/kataras/[email protected]/integration.go:28","func":"github.com/kataras/golog.integrateExternalLogger.func1","level":"info","msg":"rrrrrrrr","time":"2020-12-05T11:41:47+08:00"}
{"file":"/Users/sado/go/pkg/mod/github.com/kataras/[email protected]/integration.go:28","func":"github.com/kataras/golog.integrateExternalLogger.func1","level":"info","msg":"200 8.373308ms 127.0.0.1 GET /api/a/b/xxx","time":"2020-12-05T11:41:47+08:00"}
my questions:
- i want {file: "main.go:123"}, not
integration.go:28
, how can i get this? - when i use in controller, why this log show twice? how can i avoid this ?
thank u
Hello @sado0823
-
This is NOT golog or iris issue. Why you use
SetReportCaller
on logrus and not in golog? Iris logger has a stacktrace option which is visible on "debug" level and JSON format. Indeed, you don't need to install the logrus, Iris logger has Fields and formatters too, please read some examples of the iris logger here. -
Use the mvcApp.SetControllersNoLog(true) method.
Thanks, Gerasimos Maropoulos
here are the packages i used now github.com/kataras/golog v0.0.18 github.com/kataras/iris/v12 v12.1.8 and i updated
package main
import (
"fmt"
"github.com/kataras/golog"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
)
func main() {
app := iris.New()
log := golog.New()
// Default Output is `os.Stderr`,
// but you can change it:
// log.SetOutput(os.Stdout)
// Level defaults to "info",
// but you can change it:
log.SetLevel("debug")
log.SetFormat("json", " ")
log.SetStacktraceLimit(100)
log.SetChildPrefix(":::")
log.Println("This is a raw message, no levels, no colors.")
log.Info("This is an info message, with colors (if the output is terminal)")
log.Warn("This is a warning message")
log.Error("This is an error message")
log.Debug("This is a debug message")
app.Get("/", func(context *context.Context) {
fmt.Println("begin")
log.Debugf("This is a %s with data (debug prints the stacktrace too)", "message", golog.Fields{
"username": "kataras",
})
})
err := app.Listen(":8987")
if err != nil {
log.Fatal("fail to startup")
}
}
when i enter :8987, i got this log
{
"timestamp": 1607244296,
"level": "debug",
"message": "This is a message with data (debug prints the stacktrace too)",
"fields": {
"username": "kataras"
},
"stacktrace": [
{
"function": "Context.Do",
"source": "/Users/sado/go/pkg/mod/github.com/kataras/iris/[email protected]/context/context.go:476"
},
{
"function": "routerHandler.HandleRequest",
"source": "/Users/sado/go/pkg/mod/github.com/kataras/iris/[email protected]/core/router/handler.go:440"
},
{
"function": "Router.BuildRouter.func1",
"source": "/Users/sado/go/pkg/mod/github.com/kataras/iris/[email protected]/core/router/router.go:222"
},
{
"function": "Router.ServeHTTP",
"source": "/Users/sado/go/pkg/mod/github.com/kataras/iris/[email protected]/core/router/router.go:322"
}
]
}
actually, i just want
"stacktrace": [
{
"function": "main.main",
"source": "/usr/xxxx/main.go:36"
}
]
and i want my log instance is a singletone
can golog do this ?
Hello @sado0823,
I get the xxx/main.go
as expected, take a look:
Please note that you are using [email protected] while its latest version is 0.1.6.
To get just the handler position, you just SetStackTraceLimit(1)
instead of 100:
log.SetStacktraceLimit(1)
You can also enable JSON just for the debug level and keep the default format for Info/Warn messages with:
log.SetLevelFormat("debug", "json", " ")
and i want my log instance is a singletone
You mean "fields" and "stacktrace" to be included in the root schema? This is wrong, no, we don't do that.
here are my codes in main.go
actually, i updated my packages before
github.com/kataras/golog v0.1.6
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201204104853-3e86301b7a75
package main
import (
"fmt"
"github.com/kataras/golog"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
)
func main() {
app := iris.New()
log := golog.New()
// Default Output is `os.Stderr`,
// but you can change it:
// log.SetOutput(os.Stdout)
// Level defaults to "info",
// but you can change it:
log.SetLevel("debug")
log.SetFormat("json", " ")
log.SetStacktraceLimit(1)
log.Println("This is a raw message, no levels, no colors.")
log.Info("This is an info message, with colors (if the output is terminal)")
log.Warn("This is a warning message")
log.Error("This is an error message")
log.Debug("This is a debug message",golog.Fields{
"username": "kataras",
})
app.Get("/", func(context *context.Context) {
index()
})
err := app.Listen(":8987")
if err != nil {
log.Fatal("fail to startup")
}
}
func index(){
fmt.Println("begin")
log := golog.New()
// Default Output is `os.Stderr`,
// but you can change it:
// log.SetOutput(os.Stdout)
// Level defaults to "info",
// but you can change it:
log.SetLevel("debug")
log.SetFormat("json", " ")
log.SetStacktraceLimit(1)
log.Debugf("This is a %s with data (debug prints the stacktrace too)", "message", golog.Fields{
"username": "kataras",
})
}
i got the full log:
go run main2.go
{
"timestamp": 1607494281,
"level": "disable",
"message": "This is a raw message, no levels, no colors."
}
{
"timestamp": 1607494281,
"level": "info",
"message": "This is an info message, with colors (if the output is terminal)"
}
{
"timestamp": 1607494281,
"level": "warn",
"message": "This is a warning message"
}
{
"timestamp": 1607494281,
"level": "error",
"message": "This is an error message"
}
{
"timestamp": 1607494281,
"level": "debug",
"message": "This is a debug message",
"fields": {
"username": "kataras"
}
}
Now listening on: http://localhost:8987
Application started. Press CMD+C to shut down.
and then
begin
{
"timestamp": 1607494310,
"level": "debug",
"message": "This is a message with data (debug prints the stacktrace too)",
"fields": {
"username": "kataras"
},
"stacktrace": [
{
"function": "Context.Do",
"source": "/Users/sado/go/pkg/mod/github.com/kataras/iris/[email protected]/context/context.go:476"
}
]
}
#1 Question now i am confused
- why the log in
main.go
doesn't have trace - why my trace in func
Index
, not fromxxx/main.go
is that something wrong with my code ? please tell me , thank u ❤️ !!!! @kataras
@sado0823 Because Context.Do
is the caller of your handler.index()
, why you do that?
app.Get("/", func(context *context.Context) {
index()
})
OK listen, you have access to the current handler name and file:line with ctx.HandlerName()
and ctx.HandlerFileLine()
, therefore, you can just add it as golog.Fields{"handler": ...}
based on that information. Make a function and use it inside your handlers:
func HandlerDebugf(ctx iris.Context, format string, args ...interface{}) {
file, line := ctx.HandlerFileLine()
args = append(args, golog.Fields{
"handler": fmt.Sprintf("%s | %s:%d", ctx.HandlerName(), file,line),
})
ctx.Application().Logger().Debugf(format, args...)
}
Now you can disable the stacktrace (if you want so) with app.Logger().SetStacktraceLimit(0)
before app.Listen
. However, the stacktrace is used for full debug, so you may want to know all the information, even the Iris' internals.
Thank you for your patience !!!