iris/_examples/beginner/file-logger/main.go
kataras 74989ad0a1 Remove internal/cmd/gen, it has no use for the end-developer.
Former-commit-id: 12bead9d379c6644e391c482fe71b2e88263376c
2017-06-11 23:58:34 +03:00

50 lines
1.2 KiB
Go

package main
import (
"os"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/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()
defer f.Close()
app := iris.New()
// attach the file as logger, remember, iris' 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(iris.Addr(":8080"), iris.WithoutBanner); err != nil {
app.Log("Shutdown with error: %v\n", err)
}
}