-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
135 lines (115 loc) · 5.07 KB
/
task.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package task
import (
"context"
"github.com/davidwartell/go-logger-facade/logger"
"runtime/debug"
"strings"
)
type Task interface {
StartTask()
StopTask()
}
type BaseTask struct{}
// LogTraceStruct deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogTraceStruct(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Trace(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
// LogDebugStruct deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogDebugStruct(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Debug(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
// LogInfoStruct deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogInfoStruct(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Info(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
// LogWarnStruct deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogWarnStruct(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Warn(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
// LogWarnStructIgnoreCancel deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogWarnStructIgnoreCancel(ctx context.Context, taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().WarnIgnoreCancel(ctx, getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
// LogErrorStruct deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogErrorStruct(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Error(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
// LogErrorStructIgnoreCancel deprecated
//
//goland:noinspection GoUnusedExportedFunction
func LogErrorStructIgnoreCancel(ctx context.Context, taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().ErrorIgnoreCancel(ctx, getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func HandlePanic(taskName string) {
if err := recover(); err != nil {
LogErrorStruct(taskName, "panic occurred", logger.Any("err", err), logger.String("stacktrace", string(debug.Stack())))
}
}
//goland:noinspection GoUnusedExportedFunction
func LogTrace(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Trace(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogDebug(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Debug(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogInfo(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Info(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogInfoIgnoreCancel(ctx context.Context, taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().InfoIgnoreCancel(ctx, getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogWarn(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Warn(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogWarnIgnoreCancel(ctx context.Context, taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().WarnIgnoreCancel(ctx, getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogError(taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().Error(getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
//goland:noinspection GoUnusedExportedFunction
func LogErrorIgnoreCancel(ctx context.Context, taskName string, msg string, fields ...logger.Field) {
fieldsWithService := append(fields, logger.String("task", taskName))
logger.Instance().ErrorIgnoreCancel(ctx, getTaskLogPrefix(taskName, msg), fieldsWithService...)
}
func getTaskLogPrefix(taskName string, format string) string {
var sb strings.Builder
sb.WriteString("[")
sb.WriteString(taskName)
sb.WriteString("] ")
sb.WriteString(format)
return sb.String()
}