mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
48
_examples/logging/file-logger/main.go
Normal file
48
_examples/logging/file-logger/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f := newLogFile()
|
||||
defer f.Close()
|
||||
|
||||
app := iris.New()
|
||||
// Attach the file as logger, remember, iris' app logger is just an io.Writer.
|
||||
// Use the following code if you need to write the logs to file and console at the same time.
|
||||
// app.Logger().SetOutput(io.MultiWriter(f, os.Stdout))
|
||||
app.Logger().SetOutput(f)
|
||||
|
||||
app.Get("/ping", func(ctx iris.Context) {
|
||||
// for the sake of simplicity, in order see the logs at the ./_today_.txt
|
||||
ctx.Application().Logger().Infof("Request path: %s", ctx.Path())
|
||||
ctx.WriteString("pong")
|
||||
})
|
||||
|
||||
// Navigate to http://localhost:8080/ping
|
||||
// and open the ./logs{TODAY}.txt file.
|
||||
if err := app.Listen(":8080", iris.WithoutBanner); err != nil {
|
||||
app.Logger().Warn("Shutdown with error: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Get a filename based on the date, just for the sugar.
|
||||
func todayFilename() string {
|
||||
today := time.Now().Format("Jan 02 2006")
|
||||
return today + ".txt"
|
||||
}
|
||||
|
||||
func newLogFile() *os.File {
|
||||
filename := todayFilename()
|
||||
// Open the 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
|
||||
}
|
||||
Reference in New Issue
Block a user