/
main.go
134 lines (110 loc) · 3.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/golog"
)
const deleteFileOnExit = false
func main() {
app := iris.New()
logFile := newLogFile()
defer func() {
logFile.Close()
if deleteFileOnExit {
os.Remove(logFile.Name())
}
}()
// 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()
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")
// http://localhost:8080
// http://localhost:8080/1
// http://localhost:8080/2
// http://lcoalhost:8080/notfoundhere
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
var excludeExtensions = [...]string{
".js",
".css",
".jpg",
".png",
".ico",
".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 {
today := time.Now().Format("Jan 02 2006")
return today + ".json"
}
func newLogFile() *os.File {
filename := todayFilename()
// open an output file, this will append to the today's file if server restarted.
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
return f
}