Skip to content

Commit c0b6866

Browse files
committed
Fixed time format
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 6344ddf commit c0b6866

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

db/db.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package db
2+
3+
type (
4+
// DB defines the interface for general database operations.
5+
DB interface {
6+
Logger
7+
}
8+
9+
// Logger defines the interface for logger middleware.
10+
Logger interface {
11+
Log(*Request) error
12+
}
13+
14+
// Mongo implements `DB`
15+
Mongo struct{}
16+
)

db/model.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package db
2+
3+
import "time"
4+
5+
type (
6+
// Request defines the data to be logged by logger middleware.
7+
Request struct {
8+
// ID string `json:"id,omitempty"` (Request ID - Not implemented)
9+
Time time.Time `json:"time,omitempty"`
10+
RemoteIP string `json:"remote_ip,omitempty"`
11+
URI string `json:"uri,omitempty"`
12+
Host string `json:"host,omitempty"`
13+
Method string `json:"method,omitempty"`
14+
Path string `json:"path,omitempty"`
15+
Referer string `json:"referer,omitempty"`
16+
UserAgent string `json:"user_agent,omitempty"`
17+
Status int `json:"status,omitempty"`
18+
Latency time.Duration `json:"latency,omitempty"`
19+
LatencyHuman string `json:"latency_human,omitempty"`
20+
BytesIn int64 `json:"bytes_in"`
21+
BytesOut int64 `json:"bytes_out"`
22+
Header map[string]string `json:"header,omitempty"`
23+
Form map[string]string `json:"form,omitempty"`
24+
Query map[string]string `json:"query,omitempty"`
25+
}
26+
)

log.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package echo
22

33
import (
44
"io"
5-
"time"
65

76
"github.com/labstack/gommon/log"
87
)
@@ -38,30 +37,4 @@ type (
3837
Panicj(j log.JSON)
3938
Panicf(format string, args ...interface{})
4039
}
41-
42-
// RequestLogger defines the logging interface for logging middleware.
43-
RequestLogger interface {
44-
LogRequest(*Request) error
45-
}
46-
47-
// Request defines the data to be logged by logger middleware.
48-
Request struct {
49-
// ID string `json:"id,omitempty"` (Request ID - Not implemented)
50-
Time int64 `json:"time,omitempty"`
51-
RemoteIP string `json:"remote_ip,omitempty"`
52-
URI string `json:"uri,omitempty"`
53-
Host string `json:"host,omitempty"`
54-
Method string `json:"method,omitempty"`
55-
Path string `json:"path,omitempty"`
56-
Referer string `json:"referer,omitempty"`
57-
UserAgent string `json:"user_agent,omitempty"`
58-
Status int `json:"status,omitempty"`
59-
Latency time.Duration `json:"latency,omitempty"`
60-
LatencyHuman string `json:"latency_human,omitempty"`
61-
BytesIn int64 `json:"bytes_in"`
62-
BytesOut int64 `json:"bytes_out"`
63-
Header map[string]string `json:"header,omitempty"`
64-
Form map[string]string `json:"form,omitempty"`
65-
Query map[string]string `json:"query,omitempty"`
66-
}
6740
)

middleware/logger.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111

1212
"github.com/labstack/echo"
13+
"github.com/labstack/echo/db"
1314
)
1415

1516
type (
@@ -20,7 +21,8 @@ type (
2021

2122
// Availabe logger fields:
2223
//
23-
// - time (Unix time)
24+
// - time_unix
25+
// - time_rfc3339
2426
// - id (Request ID - Not implemented)
2527
// - remote_ip
2628
// - uri
@@ -43,17 +45,17 @@ type (
4345

4446
// Output is where logs are written.
4547
// Optional. Default value &Stream{os.Stdout}.
46-
Output echo.RequestLogger
48+
Output db.Logger
4749
}
4850

49-
// Stream implements `echo.RequestLogger`.
51+
// Stream implements `db.Logger`.
5052
Stream struct {
5153
io.Writer
5254
}
5355
)
5456

55-
// LogRequest encodes `echo.Request` into a stream.
56-
func (s *Stream) LogRequest(r *echo.Request) error {
57+
// LogRequest encodes `db.Request` into a stream.
58+
func (s *Stream) Log(r *db.Request) error {
5759
enc := json.NewEncoder(s)
5860
return enc.Encode(r)
5961
}
@@ -110,7 +112,7 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
110112
c.Error(err)
111113
}
112114
stop := time.Now()
113-
request := &echo.Request{
115+
request := &db.Request{
114116
Header: make(map[string]string),
115117
Query: make(map[string]string),
116118
Form: make(map[string]string),
@@ -119,7 +121,7 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
119121
for _, f := range config.Fields {
120122
switch f {
121123
case "time":
122-
request.Time = time.Now().Unix()
124+
request.Time = time.Now()
123125
case "remote_ip":
124126
request.RemoteIP = c.RealIP()
125127
case "host":
@@ -139,17 +141,6 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
139141
case "user_agent":
140142
request.UserAgent = req.UserAgent()
141143
case "status":
142-
// n := res.Status
143-
// s := config.color.Green(n)
144-
// switch {
145-
// case n >= 500:
146-
// s = config.color.Red(n)
147-
// case n >= 400:
148-
// s = config.color.Yellow(n)
149-
// case n >= 300:
150-
// s = config.color.Cyan(n)
151-
// }
152-
// return w.Write([]byte(s))
153144
request.Status = res.Status
154145
case "latency":
155146
request.Latency = stop.Sub(start)
@@ -180,7 +171,7 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
180171
}
181172

182173
// Write
183-
return config.Output.LogRequest(request)
174+
return config.Output.Log(request)
184175
}
185176
}
186177
}

0 commit comments

Comments
 (0)