-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_log.go
68 lines (57 loc) · 1.89 KB
/
json_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package logger
import (
"context"
"fmt"
"time"
"github.com/a-aslani/golang_agency_clean_architecture/pkg/framework"
)
func NewSimpleJSONLogger(appData framework.ApplicationData) Logger {
return &simpleJSONLoggerImpl{AppData: appData}
}
type jsonLogModel struct {
AppName string `json:"appName"`
AppInstID string `json:"appInstID"`
Start string `json:"start"`
Severity string `json:"severity"`
Message string `json:"message"`
Location string `json:"location"`
Time string `json:"time"`
}
func newJSONLogModel(lg *simpleJSONLoggerImpl, flag, loc string, msg, trid any) string {
if flag == "ERROR" {
return toJsonString(jsonLogModel{
AppName: lg.AppData.AppName,
AppInstID: lg.AppData.AppInstanceID,
Start: lg.AppData.StartTime,
Severity: flag,
Message: fmt.Sprintf("%v %v %v", trid, loc, msg),
Location: loc,
Time: time.Now().Format("2006-01-02 15:04:05"),
})
}
return toJsonString(jsonLogModel{
AppName: lg.AppData.AppName,
AppInstID: lg.AppData.AppInstanceID,
Start: lg.AppData.StartTime,
Severity: flag,
Message: fmt.Sprintf("%v %v", trid, msg),
Location: loc,
Time: time.Now().Format("2006-01-02 15:04:05"),
})
}
type simpleJSONLoggerImpl struct {
AppData framework.ApplicationData
}
func (l simpleJSONLoggerImpl) Info(ctx context.Context, message string, args ...any) {
messageWithArgs := fmt.Sprintf(message, args...)
l.printLog(ctx, "INFO", messageWithArgs)
}
func (l simpleJSONLoggerImpl) Error(ctx context.Context, message string, args ...any) {
messageWithArgs := fmt.Sprintf(message, args...)
l.printLog(ctx, "ERROR", messageWithArgs)
}
func (l simpleJSONLoggerImpl) printLog(ctx context.Context, flag string, data any) {
traceID := GetTraceID(ctx)
fmt.Printf("%-5s %s %-60v %s\n", flag, traceID, data, getFileLocationInfo(3))
// fmt.Println(newJSONLogModel(&l, flag, getFileLocationInfo(3), data, traceID))
}