Skip to content

Commit 5106691

Browse files
committed
refactor source
1 parent 69d8127 commit 5106691

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

echo_adapter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func NewEchoLoggerMiddleware() echo.MiddlewareFunc {
294294
"latency_ms": latency.Milliseconds(),
295295
"referer": req.Referer(),
296296
"type": LogTypeAPI,
297-
"request_id": GetCorrelationID(ctx),
297+
"request_id": getCorrelationID(ctx),
298298
"error": errStr,
299299
}
300300

@@ -326,7 +326,7 @@ func LogWithEchoContext(c echo.Context, content ...interface{}) {
326326

327327
logField := logrus.Fields{
328328
"type": logType,
329-
"request_id": GetCorrelationID(c.Request().Context()),
329+
"request_id": getCorrelationID(c.Request().Context()),
330330
}
331331

332332
if len(content) > 2 {

main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@ const (
2121

2222
// custom constants
2323
const (
24-
DefaultTimestampFormat = "2006-01-02T15:04:05.00000Z07:00"
24+
DefaultTimestampFormat = "2006-01-02 15:04:05.00000"
2525
DefaultGormSourceField = "source"
2626
)
2727

28+
// Key is key context type
29+
type Key string
30+
31+
// Exported constanst
32+
const (
33+
CorrelationIDKey Key = "X-User-Correlation-Id"
34+
RequestIDKey Key = "X-Request-ID"
35+
)
36+
2837
// LogFormat log format
2938
type LogFormat uint32
3039

@@ -86,6 +95,7 @@ type Logger struct {
8695

8796
// SetFormatter logger formatter
8897
func (l *Logger) SetFormatter(logFormat LogFormat) {
98+
l.logFormat = logFormat
8999
switch logFormat {
90100
case JSONFormat:
91101
l.Logger.SetFormatter(&log.JSONFormatter{TimestampFormat: DefaultTimestampFormat})
@@ -142,7 +152,7 @@ func (l *Logger) SetLevel(level Level) {
142152

143153
func getDefaultFileConfig() *FileConfig {
144154
return &FileConfig{
145-
Filename: GetLogFile(),
155+
Filename: getLogFile(),
146156
MaxSize: 10,
147157
MaxBackups: 3,
148158
MaxAge: 30,

util.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,22 @@ import (
99
"time"
1010
)
1111

12-
// Key is key context type
13-
type Key string
14-
15-
// Exported constanst
16-
const (
17-
CorrelationIDKey Key = "X-User-Correlation-Id"
18-
RequestIDKey Key = "X-Request-ID"
19-
)
20-
2112
// withCorrelationID sets correlation id to context
2213
func withCorrelationID(parent context.Context, correlationID string) context.Context {
2314
return context.WithValue(parent, CorrelationIDKey, correlationID)
2415
}
2516

26-
// GetCorrelationID return correlation id
27-
func GetCorrelationID(ctx context.Context) string {
17+
// getCorrelationID return correlation id
18+
func getCorrelationID(ctx context.Context) string {
2819
id := ctx.Value(CorrelationIDKey)
2920
if id != nil {
3021
return id.(string)
3122
}
3223
return ""
3324
}
3425

35-
// GetLogFile get file log
36-
func GetLogFile() string {
26+
// getLogFile get file log
27+
func getLogFile() string {
3728
path := ""
3829
dir, err := os.Getwd()
3930
if err != nil {

0 commit comments

Comments
 (0)