-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_log.go
70 lines (61 loc) · 1.75 KB
/
function_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
69
70
package blaze
import (
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// NewFuncLog will create a new func log
func NewFuncLog(name string, log *zap.Logger, fields ...zap.Field) *FuncLog {
return &FuncLog{
name,
log,
fields,
}
}
// FuncLog is used to easily log golang function statuses
type FuncLog struct {
name string
Log *zap.Logger
fields []zap.Field
}
// getFields This will concat fields with fields provided when struct was initialized
func (funcLog FuncLog) getFields(fields ...zap.Field) []zap.Field {
newFields := funcLog.fields
if fields != nil {
newFields = append(newFields, fields...)
}
return newFields
}
// Panic will log the panic at PANIC level
func (funcLog FuncLog) Panic(message interface{}, fields ...zap.Field) {
fieldsToAdd := append(
funcLog.getFields(fields...), zap.Any("Panic Message", message))
funcLog.Log.Panic(
fmt.Sprintf("Function %s Ran Into A Panic", funcLog.name),
fieldsToAdd...,
)
}
// ErrorWrap will log the error at ERROR level and wrap it with information
func (funcLog FuncLog) Error(err error, fields ...zap.Field) error {
msg := fmt.Sprintf("Function %s Ran Into An Error", funcLog.name)
fieldsToAdd := append(funcLog.getFields(fields...), zap.Error(err))
funcLog.Log.Error(
msg,
fieldsToAdd...,
)
return errors.Wrap(err, msg)
}
// Completed will that function completed at INFO level
func (funcLog FuncLog) Completed(fields ...zap.Field) {
funcLog.Log.Info(
fmt.Sprintf("Function %s Executed Successfully", funcLog.name),
funcLog.getFields(fields...)...,
)
}
// Started will log that function started at DEBUG level
func (funcLog FuncLog) Started(fields ...zap.Field) {
funcLog.Log.Debug(
fmt.Sprintf("Function %s Started", funcLog.name),
funcLog.getFields(fields...)...,
)
}