mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 03:01:03 +01:00
42b123975c
Former-commit-id: 90c05e743052bc3722e7fefaa0cbb0ed5153a1fb
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/kataras/iris"
|
|
)
|
|
|
|
// 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 + ".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()
|
|
defer f.Close()
|
|
|
|
app := iris.New()
|
|
// attach the file as logger, remember, iris' app logger is just an io.Writer.
|
|
app.Logger().SetOutput(newLogFile())
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
// for the sake of simplicity, in order see the logs at the ./_today_.txt
|
|
ctx.Application().Logger().Info("Request path: " + ctx.Path())
|
|
ctx.Writef("hello")
|
|
})
|
|
|
|
// navigate to http://localhost:8080
|
|
// and open the ./logs.txt file
|
|
if err := app.Run(iris.Addr(":8080"), iris.WithoutBanner); err != nil {
|
|
if err != iris.ErrServerClosed {
|
|
app.Logger().Warn("Shutdown with error: " + err.Error())
|
|
}
|
|
}
|
|
}
|