kraken

Форк
0
/
log.go 
166 строк · 4.9 Кб
1
// Copyright (c) 2016-2019 Uber Technologies, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
package log
15

16
// This package wraps logger functionality that is being used
17
// in kraken providing seamless migration tooling if needed
18
// and hides out some initialization details
19

20
import (
21
	"go.uber.org/zap"
22
	"go.uber.org/zap/zapcore"
23
)
24

25
var (
26
	_default *zap.SugaredLogger
27
)
28

29
// configure a default logger
30
func init() {
31
	zapConfig := zap.NewProductionConfig()
32
	zapConfig.Encoding = "console"
33
	zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
34
	zapConfig.DisableStacktrace = true
35

36
	ConfigureLogger(zapConfig)
37
}
38

39
// ConfigureLogger configures a global zap logger instance.
40
func ConfigureLogger(zapConfig zap.Config) *zap.SugaredLogger {
41
	logger, err := zapConfig.Build()
42
	if err != nil {
43
		panic(err)
44
	}
45

46
	// Skip this wrapper in a call stack.
47
	logger = logger.WithOptions(zap.AddCallerSkip(1))
48

49
	_default = logger.Sugar()
50
	return _default
51
}
52

53
// SetGlobalLogger sets the global logger.
54
func SetGlobalLogger(l *zap.SugaredLogger) {
55
	_default = l
56
}
57

58
// Default returns the default global logger.
59
func Default() *zap.SugaredLogger {
60
	return _default
61
}
62

63
// Debug uses fmt.Sprint to construct and log a message.
64
func Debug(args ...interface{}) {
65
	Default().Debug(args...)
66
}
67

68
// Info uses fmt.Sprint to construct and log a message.
69
func Info(args ...interface{}) {
70
	Default().Info(args...)
71
}
72

73
// Warn uses fmt.Sprint to construct and log a message.
74
func Warn(args ...interface{}) {
75
	Default().Warn(args...)
76
}
77

78
// Error uses fmt.Sprint to construct and log a message.
79
func Error(args ...interface{}) {
80
	Default().Error(args...)
81
}
82

83
// Panic uses fmt.Sprint to construct and log a message, then panics.
84
func Panic(args ...interface{}) {
85
	Default().Panic(args...)
86
}
87

88
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
89
func Fatal(args ...interface{}) {
90
	Default().Fatal(args...)
91
}
92

93
// Debugf uses fmt.Sprintf to log a templated message.
94
func Debugf(template string, args ...interface{}) {
95
	Default().Debugf(template, args...)
96
}
97

98
// Infof uses fmt.Sprintf to log a templated message.
99
func Infof(template string, args ...interface{}) {
100
	Default().Infof(template, args...)
101
}
102

103
// Warnf uses fmt.Sprintf to log a templated message.
104
func Warnf(template string, args ...interface{}) {
105
	Default().Warnf(template, args...)
106
}
107

108
// Errorf uses fmt.Sprintf to log a templated message.
109
func Errorf(template string, args ...interface{}) {
110
	Default().Errorf(template, args...)
111
}
112

113
// Panicf uses fmt.Sprintf to log a templated message, then panics.
114
func Panicf(template string, args ...interface{}) {
115
	Default().Panicf(template, args...)
116
}
117

118
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
119
func Fatalf(template string, args ...interface{}) {
120
	Default().Fatalf(template, args...)
121
}
122

123
// Debugw logs a message with some additional context. The variadic key-value
124
// pairs are treated as they are in With.
125
//
126
// When debug-level logging is disabled, this is much faster than
127
//  s.With(keysAndValues).Debug(msg)
128
func Debugw(msg string, keysAndValues ...interface{}) {
129
	Default().Debugw(msg, keysAndValues...)
130
}
131

132
// Infow logs a message with some additional context. The variadic key-value
133
// pairs are treated as they are in With.
134
func Infow(msg string, keysAndValues ...interface{}) {
135
	Default().Infow(msg, keysAndValues...)
136
}
137

138
// Warnw logs a message with some additional context. The variadic key-value
139
// pairs are treated as they are in With.
140
func Warnw(msg string, keysAndValues ...interface{}) {
141
	Default().Warnw(msg, keysAndValues...)
142
}
143

144
// Errorw logs a message with some additional context. The variadic key-value
145
// pairs are treated as they are in With.
146
func Errorw(msg string, keysAndValues ...interface{}) {
147
	Default().Errorw(msg, keysAndValues...)
148
}
149

150
// Panicw logs a message with some additional context, then panics. The
151
// variadic key-value pairs are treated as they are in With.
152
func Panicw(msg string, keysAndValues ...interface{}) {
153
	Default().Panicw(msg, keysAndValues...)
154
}
155

156
// Fatalw logs a message with some additional context, then calls os.Exit. The
157
// variadic key-value pairs are treated as they are in With.
158
func Fatalw(msg string, keysAndValues ...interface{}) {
159
	Default().Fatalw(msg, keysAndValues...)
160
}
161

162
// With adds a variadic number of fields to the logging context.
163
// It accepts a mix of strongly-typed zapcore.Field objects and loosely-typed key-value pairs.
164
func With(args ...interface{}) *zap.SugaredLogger {
165
	return Default().With(args...)
166
}
167

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.