forked from Xzya/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (51 loc) · 1.36 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
package main
import (
"log"
"os"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
)
var myLogFile *os.File
func init() {
// open an output file
f, err := os.Create("logs.txt")
if err != nil {
panic(err)
}
myLogFile = f
}
func myFileLogger() iris.LoggerPolicy {
// you can use a *File or an io.Writer,
// we want to log with timestamps so we use the log.New.
myLogger := log.New(myLogFile, "", log.LstdFlags)
// the logger is just a func,
// will be used in runtime
return func(mode iris.LogMode, message string) {
// optionally, check for production or development log message mode
// two modes: iris.ProdMode and iris.DevMode
if mode == iris.ProdMode {
// log only production-mode log messages
myLogger.Println(message)
}
}
}
func main() {
// close the log file on exit application
// when panic or iris exited by interupt event or manually by Shutdown.
defer func() {
if err := myLogFile.Close(); err != nil {
panic(err)
}
}()
app := iris.New()
app.Adapt(myFileLogger())
app.Adapt(httprouter.New())
app.Get("/", func(ctx *iris.Context) {
// for the sake of simplicity, in order see the logs at the ./logs.txt:
app.Log(iris.ProdMode, "You have requested: http://localhost/8080"+ctx.Path())
ctx.Writef("hello")
})
// open http://localhost:8080
// and watch the ./logs.txt file
app.Listen(":8080")
}