feat: api stdout logger
This commit is contained in:
parent
8f4132f131
commit
7057e232e6
29
ctx.go
29
ctx.go
@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
type Ctx struct {
|
type Ctx struct {
|
||||||
// origin objects
|
// origin objects
|
||||||
Writer http.ResponseWriter
|
writer http.ResponseWriter
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
// request info
|
// request info
|
||||||
path string
|
path string
|
||||||
@ -31,10 +31,11 @@ type Ctx struct {
|
|||||||
|
|
||||||
func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
|
func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
|
||||||
return &Ctx{
|
return &Ctx{
|
||||||
Writer: writer,
|
writer: writer,
|
||||||
Request: request,
|
Request: request,
|
||||||
path: request.URL.Path,
|
path: request.URL.Path,
|
||||||
Method: request.Method,
|
Method: request.Method,
|
||||||
|
StatusCode: 200,
|
||||||
|
|
||||||
app: app,
|
app: app,
|
||||||
index: -1,
|
index: -1,
|
||||||
@ -182,16 +183,16 @@ func (c *Ctx) QueryParser(out interface{}) error {
|
|||||||
|
|
||||||
func (c *Ctx) Status(code int) *Ctx {
|
func (c *Ctx) Status(code int) *Ctx {
|
||||||
c.StatusCode = code
|
c.StatusCode = code
|
||||||
c.Writer.WriteHeader(code)
|
c.writer.WriteHeader(code)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) Set(key string, value string) {
|
func (c *Ctx) Set(key string, value string) {
|
||||||
c.Writer.Header().Set(key, value)
|
c.writer.Header().Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) SetHeader(key string, value string) {
|
func (c *Ctx) SetHeader(key string, value string) {
|
||||||
c.Writer.Header().Set(key, value)
|
c.writer.Header().Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) SendString(data string) error {
|
func (c *Ctx) SendString(data string) error {
|
||||||
@ -202,13 +203,13 @@ func (c *Ctx) SendString(data string) error {
|
|||||||
|
|
||||||
func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
|
func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
|
||||||
c.SetHeader("Content-Type", "text/plain")
|
c.SetHeader("Content-Type", "text/plain")
|
||||||
return c.Writer.Write([]byte(fmt.Sprintf(format, values...)))
|
return c.writer.Write([]byte(fmt.Sprintf(format, values...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) JSON(data interface{}) error {
|
func (c *Ctx) JSON(data interface{}) error {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
|
|
||||||
encoder := json.NewEncoder(c.Writer)
|
encoder := json.NewEncoder(c.writer)
|
||||||
|
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -217,12 +218,16 @@ func (c *Ctx) JSON(data interface{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Ctx) RawWriter() http.ResponseWriter {
|
||||||
|
return c.writer
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Ctx) Write(data []byte) (int, error) {
|
func (c *Ctx) Write(data []byte) (int, error) {
|
||||||
return c.Writer.Write(data)
|
return c.writer.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) HTML(html string) error {
|
func (c *Ctx) HTML(html string) error {
|
||||||
c.SetHeader("Content-Type", "text/html")
|
c.SetHeader("Content-Type", "text/html")
|
||||||
_, err := c.Writer.Write([]byte(html))
|
_, err := c.writer.Write([]byte(html))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@ package nf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRecover(enableStackTrace bool) HandlerFunc {
|
func NewRecover(enableStackTrace bool) HandlerFunc {
|
||||||
@ -21,3 +23,53 @@ func NewRecover(enableStackTrace bool) HandlerFunc {
|
|||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewLogger() HandlerFunc {
|
||||||
|
l := log.New(os.Stdout, "[NF] ", 0)
|
||||||
|
|
||||||
|
durationFormat := func(num int64) string {
|
||||||
|
var (
|
||||||
|
unit = "ns"
|
||||||
|
)
|
||||||
|
|
||||||
|
if num > 1000 {
|
||||||
|
num = num / 1000
|
||||||
|
unit = "µs"
|
||||||
|
}
|
||||||
|
|
||||||
|
if num > 1000 {
|
||||||
|
num = num / 1000
|
||||||
|
unit = "ms"
|
||||||
|
}
|
||||||
|
|
||||||
|
if num > 1000 {
|
||||||
|
num = num / 1000
|
||||||
|
unit = " s"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%v %s", num, unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(c *Ctx) error {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
err := c.Next()
|
||||||
|
|
||||||
|
var (
|
||||||
|
duration = time.Now().Sub(start).Nanoseconds()
|
||||||
|
status = c.StatusCode
|
||||||
|
path = c.path
|
||||||
|
method = c.Request.Method
|
||||||
|
)
|
||||||
|
|
||||||
|
l.Printf("%s | %5s | %d | %s | %s",
|
||||||
|
start.Format("06/01/02T15:04:05"),
|
||||||
|
method,
|
||||||
|
status,
|
||||||
|
durationFormat(duration),
|
||||||
|
path,
|
||||||
|
)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
4
nf.go
4
nf.go
@ -42,6 +42,10 @@ func New(config ...Config) *App {
|
|||||||
app.RouterGroup = &RouterGroup{app: app}
|
app.RouterGroup = &RouterGroup{app: app}
|
||||||
app.groups = []*RouterGroup{app.RouterGroup}
|
app.groups = []*RouterGroup{app.RouterGroup}
|
||||||
|
|
||||||
|
if !app.config.DisableLogger {
|
||||||
|
app.Use(NewLogger())
|
||||||
|
}
|
||||||
|
|
||||||
if !app.config.DisableRecover {
|
if !app.config.DisableRecover {
|
||||||
app.Use(NewRecover(true))
|
app.Use(NewRecover(true))
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/loveuer/nf"
|
"github.com/loveuer/nf"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -13,6 +14,11 @@ func main() {
|
|||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
|
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
|
||||||
})
|
})
|
||||||
|
app.Patch("/world", func(c *nf.Ctx) error {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
c.Status(404)
|
||||||
|
return c.JSON(nf.Map{"method": c.Method, "status": c.StatusCode})
|
||||||
|
})
|
||||||
|
|
||||||
ln, _ := net.Listen("tcp", ":80")
|
ln, _ := net.Listen("tcp", ":80")
|
||||||
log.Fatal(app.RunListener(ln))
|
log.Fatal(app.RunListener(ln))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user