-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (39 loc) · 1.19 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
package main
import (
"os"
"time"
"github.com/go-siris/siris"
"github.com/go-siris/siris/context"
)
// get a filename based on the date, file logs works that way the most times
// but these are just a sugar, you can directly attach a new file logger with .AttachLogger(io.Writer)
func todayFilename() string {
today := time.Now().Format("Jan 02 2006")
return today + ".txt"
}
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
}
func main() {
f := newLogFile()
app := siris.New()
// attach the file as logger, remember, siris' app logger is just an io.Writer.
app.AttachLogger(f)
app.Get("/", func(ctx context.Context) {
// for the sake of simplicity, in order see the logs at the ./_today_.txt
ctx.Application().Log("Request: %s\r\n", ctx.Path())
ctx.Writef("hello")
})
// navigate to http://localhost:8080
// and open the ./logs.txt file
if err := app.Run(siris.Addr(":8080"), siris.WithoutBanner); err != nil {
app.Log("Shutdown with error: %v\n", err)
}
f.Close()
}