cubefs
221 строка · 7.4 Кб
1/*
2*
3* Copyright 2017 gRPC authors.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*
17*/
18
19package grpclog
20
21import (
22"io"
23"io/ioutil"
24"log"
25"os"
26"strconv"
27
28"google.golang.org/grpc/internal/grpclog"
29)
30
31// LoggerV2 does underlying logging work for grpclog.
32type LoggerV2 interface {
33// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
34Info(args ...interface{})
35// Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
36Infoln(args ...interface{})
37// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
38Infof(format string, args ...interface{})
39// Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
40Warning(args ...interface{})
41// Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
42Warningln(args ...interface{})
43// Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
44Warningf(format string, args ...interface{})
45// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
46Error(args ...interface{})
47// Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
48Errorln(args ...interface{})
49// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
50Errorf(format string, args ...interface{})
51// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
52// gRPC ensures that all Fatal logs will exit with os.Exit(1).
53// Implementations may also call os.Exit() with a non-zero exit code.
54Fatal(args ...interface{})
55// Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
56// gRPC ensures that all Fatal logs will exit with os.Exit(1).
57// Implementations may also call os.Exit() with a non-zero exit code.
58Fatalln(args ...interface{})
59// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
60// gRPC ensures that all Fatal logs will exit with os.Exit(1).
61// Implementations may also call os.Exit() with a non-zero exit code.
62Fatalf(format string, args ...interface{})
63// V reports whether verbosity level l is at least the requested verbose level.
64V(l int) bool
65}
66
67// SetLoggerV2 sets logger that is used in grpc to a V2 logger.
68// Not mutex-protected, should be called before any gRPC functions.
69func SetLoggerV2(l LoggerV2) {
70if _, ok := l.(*componentData); ok {
71panic("cannot use component logger as grpclog logger")
72}
73grpclog.Logger = l
74grpclog.DepthLogger, _ = l.(grpclog.DepthLoggerV2)
75}
76
77const (
78// infoLog indicates Info severity.
79infoLog int = iota
80// warningLog indicates Warning severity.
81warningLog
82// errorLog indicates Error severity.
83errorLog
84// fatalLog indicates Fatal severity.
85fatalLog
86)
87
88// severityName contains the string representation of each severity.
89var severityName = []string{
90infoLog: "INFO",
91warningLog: "WARNING",
92errorLog: "ERROR",
93fatalLog: "FATAL",
94}
95
96// loggerT is the default logger used by grpclog.
97type loggerT struct {
98m []*log.Logger
99v int
100}
101
102// NewLoggerV2 creates a loggerV2 with the provided writers.
103// Fatal logs will be written to errorW, warningW, infoW, followed by exit(1).
104// Error logs will be written to errorW, warningW and infoW.
105// Warning logs will be written to warningW and infoW.
106// Info logs will be written to infoW.
107func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
108return NewLoggerV2WithVerbosity(infoW, warningW, errorW, 0)
109}
110
111// NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
112// verbosity level.
113func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
114var m []*log.Logger
115m = append(m, log.New(infoW, severityName[infoLog]+": ", log.LstdFlags))
116m = append(m, log.New(io.MultiWriter(infoW, warningW), severityName[warningLog]+": ", log.LstdFlags))
117ew := io.MultiWriter(infoW, warningW, errorW) // ew will be used for error and fatal.
118m = append(m, log.New(ew, severityName[errorLog]+": ", log.LstdFlags))
119m = append(m, log.New(ew, severityName[fatalLog]+": ", log.LstdFlags))
120return &loggerT{m: m, v: v}
121}
122
123// newLoggerV2 creates a loggerV2 to be used as default logger.
124// All logs are written to stderr.
125func newLoggerV2() LoggerV2 {
126errorW := ioutil.Discard
127warningW := ioutil.Discard
128infoW := ioutil.Discard
129
130logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL")
131switch logLevel {
132case "", "ERROR", "error": // If env is unset, set level to ERROR.
133errorW = os.Stderr
134case "WARNING", "warning":
135warningW = os.Stderr
136case "INFO", "info":
137infoW = os.Stderr
138}
139
140var v int
141vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
142if vl, err := strconv.Atoi(vLevel); err == nil {
143v = vl
144}
145return NewLoggerV2WithVerbosity(infoW, warningW, errorW, v)
146}
147
148func (g *loggerT) Info(args ...interface{}) {
149g.m[infoLog].Print(args...)
150}
151
152func (g *loggerT) Infoln(args ...interface{}) {
153g.m[infoLog].Println(args...)
154}
155
156func (g *loggerT) Infof(format string, args ...interface{}) {
157g.m[infoLog].Printf(format, args...)
158}
159
160func (g *loggerT) Warning(args ...interface{}) {
161g.m[warningLog].Print(args...)
162}
163
164func (g *loggerT) Warningln(args ...interface{}) {
165g.m[warningLog].Println(args...)
166}
167
168func (g *loggerT) Warningf(format string, args ...interface{}) {
169g.m[warningLog].Printf(format, args...)
170}
171
172func (g *loggerT) Error(args ...interface{}) {
173g.m[errorLog].Print(args...)
174}
175
176func (g *loggerT) Errorln(args ...interface{}) {
177g.m[errorLog].Println(args...)
178}
179
180func (g *loggerT) Errorf(format string, args ...interface{}) {
181g.m[errorLog].Printf(format, args...)
182}
183
184func (g *loggerT) Fatal(args ...interface{}) {
185g.m[fatalLog].Fatal(args...)
186// No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
187}
188
189func (g *loggerT) Fatalln(args ...interface{}) {
190g.m[fatalLog].Fatalln(args...)
191// No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
192}
193
194func (g *loggerT) Fatalf(format string, args ...interface{}) {
195g.m[fatalLog].Fatalf(format, args...)
196// No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
197}
198
199func (g *loggerT) V(l int) bool {
200return l <= g.v
201}
202
203// DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements
204// DepthLoggerV2, the below functions will be called with the appropriate stack
205// depth set for trivial functions the logger may ignore.
206//
207// Experimental
208//
209// Notice: This type is EXPERIMENTAL and may be changed or removed in a
210// later release.
211type DepthLoggerV2 interface {
212LoggerV2
213// InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Print.
214InfoDepth(depth int, args ...interface{})
215// WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Print.
216WarningDepth(depth int, args ...interface{})
217// ErrorDetph logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Print.
218ErrorDepth(depth int, args ...interface{})
219// FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Print.
220FatalDepth(depth int, args ...interface{})
221}
222