mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 10:57:05 +00:00
create the new FileServer and HandleDir, deprecate the rest APIBuilder/Party static methods and more
relative: https://github.com/kataras/iris/issues/1283 and removing pongo2 from vendor: https://github.com/kataras/iris/issues/1284 Former-commit-id: 3ec57b349f99faca2b8e36d9f7252db0b6ea080d
This commit is contained in:
@@ -2,19 +2,56 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
const deleteFileOnExit = false
|
||||
|
||||
func newRequestLogger(newWriter io.Writer) iris.Handler {
|
||||
c := logger.Config{}
|
||||
|
||||
// we don't want to use the logger
|
||||
// to log requests to assets and etc
|
||||
c.AddSkipper(func(ctx iris.Context) bool {
|
||||
path := ctx.Path()
|
||||
for _, ext := range excludeExtensions {
|
||||
if strings.HasSuffix(path, ext) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
c.LogFuncCtx = func(ctx iris.Context, latency time.Duration) {
|
||||
datetime := time.Now().Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
|
||||
customHandlerMessage := ctx.Values().GetString("log_message")
|
||||
|
||||
file, line := ctx.HandlerFileLine()
|
||||
source := fmt.Sprintf("%s:%d", file, line)
|
||||
|
||||
// this will just append a line without an array of javascript objects, readers of this file should read one line per log javascript object,
|
||||
// however, you can improve it even more, this is just a simple example on how to use the `LogFuncCtx`.
|
||||
jsonStr := fmt.Sprintf(`{"datetime":"%s","level":"%s","source":"%s","latency": "%s","status": %d,"method":"%s","path":"%s","message":"%s"}`,
|
||||
datetime, "INFO", source, latency.String(), ctx.GetStatusCode(), ctx.Method(), ctx.Path(), customHandlerMessage)
|
||||
|
||||
fmt.Fprintln(newWriter, jsonStr)
|
||||
}
|
||||
|
||||
return logger.New(c)
|
||||
}
|
||||
|
||||
func h(ctx iris.Context) {
|
||||
ctx.Values().Set("log_message", "something to give more info to the request logger")
|
||||
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
@@ -26,56 +63,21 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// Handle the logs by yourself using the `app.Logger#Handle` method.
|
||||
// Return true if that handled, otherwise will print to the screen.
|
||||
// You can also use the `app.Logger#SetOutput/AddOutput` to change or add
|
||||
// multi (io.Writer) outputs if you just want to print the message
|
||||
// somewhere else than the terminal screen.
|
||||
app.Logger().Handle(func(l *golog.Log) bool {
|
||||
_, fn, line, _ := runtime.Caller(5)
|
||||
|
||||
var (
|
||||
// formatted date string based on the `golog#TimeFormat`, which can be customized.
|
||||
// Or use the golog.Log#Time field to get the exact time.Time instance.
|
||||
datetime = l.FormatTime()
|
||||
// the log's message level.
|
||||
level = golog.GetTextForLevel(l.Level, false)
|
||||
// the log's message.
|
||||
message = l.Message
|
||||
// the source code line of where it is called,
|
||||
// this can differ on your app, see runtime.Caller(%d).
|
||||
source = fmt.Sprintf("%s#%d", fn, line)
|
||||
)
|
||||
|
||||
// You can always use a custom json structure and json.Marshal and logFile.Write(its result)
|
||||
// but it is faster to just build your JSON string by yourself as we do below.
|
||||
jsonStr := fmt.Sprintf(`{"datetime":"%s","level":"%s","message":"%s","source":"%s"}`, datetime, level, message, source)
|
||||
fmt.Fprintln(logFile, jsonStr)
|
||||
|
||||
/* Example output:
|
||||
{"datetime":"2018/10/31 13:13","level":"[INFO]","message":"My server started","source":"c:/mygopath/src/github.com/kataras/iris/_examples/http_request/request-logger/request-logger-file-json/main.go#71"}
|
||||
*/
|
||||
return true
|
||||
})
|
||||
|
||||
r := newRequestLogger()
|
||||
r := newRequestLogger(logFile)
|
||||
|
||||
app.Use(r)
|
||||
app.OnAnyErrorCode(r, func(ctx iris.Context) {
|
||||
ctx.HTML("<h1> Error: Please try <a href ='/'> this </a> instead.</h1>")
|
||||
})
|
||||
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
|
||||
app.Get("/", h)
|
||||
|
||||
app.Get("/1", h)
|
||||
|
||||
app.Get("/2", h)
|
||||
|
||||
app.Logger().Info("My server started")
|
||||
app.Get("/", h)
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/1
|
||||
// http://localhost:8080/2
|
||||
@@ -92,29 +94,6 @@ var excludeExtensions = [...]string{
|
||||
".svg",
|
||||
}
|
||||
|
||||
func newRequestLogger() iris.Handler {
|
||||
c := logger.Config{
|
||||
Status: true,
|
||||
IP: true,
|
||||
Method: true,
|
||||
Path: true,
|
||||
}
|
||||
|
||||
// we don't want to use the logger
|
||||
// to log requests to assets and etc
|
||||
c.AddSkipper(func(ctx iris.Context) bool {
|
||||
path := ctx.Path()
|
||||
for _, ext := range excludeExtensions {
|
||||
if strings.HasSuffix(path, ext) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return logger.New(c)
|
||||
}
|
||||
|
||||
// get a filename based on the date, file logs works that way the most times
|
||||
// but these are just a sugar.
|
||||
func todayFilename() string {
|
||||
|
||||
Reference in New Issue
Block a user