This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
155 lines (133 loc) · 4.55 KB
/
logger.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package log
import (
"sync"
"github.com/hyperledger/aries-framework-go/pkg/internal/common/logging/metadata"
)
//nolint:lll
const (
// loggerNotInitializedMsg is used when a logger is not initialized before logging
loggerNotInitializedMsg = "Default logger initialized (please call log.Initialize() if you wish to use a custom logger)"
loggerModule = "aries-framework/common"
)
// Log is an implementation of Logger interface.
// It encapsulates default or custom logger to provide module and level based logging.
type Log struct {
instance Logger
module string
once sync.Once
}
// New creates and returns a Logger implementation based on given module name.
// note: the underlying logger instance is lazy initialized on first use.
// To use your own logger implementation provide logger provider in 'Initialize()' before logging any line.
// If 'Initialize()' is not called before logging any line then default logging implementation will be used.
func New(module string) *Log {
return &Log{module: module}
}
// Fatalf calls Fatalf function of underlying logger
// should possibly cause system shutdown based on implementation
func (l *Log) Fatalf(msg string, args ...interface{}) {
l.logger().Fatalf(msg, args...)
}
// Panicf calls Panic function of underlying logger
// should possibly cause panic based on implementation
func (l *Log) Panicf(msg string, args ...interface{}) {
l.logger().Panicf(msg, args...)
}
// Debugf calls Debugf function of underlying logger
func (l *Log) Debugf(msg string, args ...interface{}) {
l.logger().Debugf(msg, args...)
}
// Infof calls Infof function of underlying logger
func (l *Log) Infof(msg string, args ...interface{}) {
l.logger().Infof(msg, args...)
}
// Warnf calls Warnf function of underlying logger
func (l *Log) Warnf(msg string, args ...interface{}) {
l.logger().Warnf(msg, args...)
}
// Errorf calls Errorf function of underlying logger
func (l *Log) Errorf(msg string, args ...interface{}) {
l.logger().Errorf(msg, args...)
}
func (l *Log) logger() Logger {
l.once.Do(func() {
l.instance = loggerProvider().GetLogger(l.module)
})
return l.instance
}
// SetLevel - setting log level for given module
// Parameters:
// module is module name
// level is logging level
//
// If not set default logging level is info
func SetLevel(module string, level Level) {
metadata.SetLevel(module, metadata.Level(level))
}
// GetLevel - getting log level for given module
// Parameters:
// module is module name
//
// Returns:
// logging level
//
// If not set default logging level is info
func GetLevel(module string) Level {
return Level(metadata.GetLevel(module))
}
// IsEnabledFor - Check if given log level is enabled for given module
// Parameters:
// module is module name
// level is logging level
//
// Returns:
// is logging enabled for this module and level
//
// If not set default logging level is info
func IsEnabledFor(module string, level Level) bool {
return metadata.IsEnabledFor(module, metadata.Level(level))
}
// ParseLevel returns the log level from a string representation.
// Parameters:
// level is logging level in string representation
//
// Returns:
// logging level
func ParseLevel(level string) (Level, error) {
l, err := metadata.ParseLevel(level)
return Level(l), err
}
// ShowCallerInfo - Show caller info in log lines for given log level and module
// Parameters:
// module is module name
// level is logging level
//
// note: based on implementation of custom logger, callerinfo info may not be available for custom logging provider
func ShowCallerInfo(module string, level Level) {
metadata.ShowCallerInfo(module, metadata.Level(level))
}
// HideCallerInfo - Do not show caller info in log lines for given log level and module
// Parameters:
// module is module name
// level is logging level
//
// note: based on implementation of custom logger, callerinfo info may not be available for custom logging provider
func HideCallerInfo(module string, level Level) {
metadata.HideCallerInfo(module, metadata.Level(level))
}
// IsCallerInfoEnabled - returns if caller info enabled for given log level and module
// Parameters:
// module is module name
// level is logging level
//
// Returns:
// is caller info enabled for this module and level
//
// note: based on implementation of custom logger, callerinfo info may not be available for custom logging provider
func IsCallerInfoEnabled(module string, level Level) bool {
return metadata.IsCallerInfoEnabled(module, metadata.Level(level))
}