blob: 23612b7c41b50113390aa93bd5a1bb96822701c3 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
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"
amit.ghosh258d14c2020-10-02 15:13:38 +020027
28 "google.golang.org/grpc/internal/grpclog"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070029)
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.
34 Info(args ...interface{})
35 // Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
36 Infoln(args ...interface{})
37 // Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
38 Infof(format string, args ...interface{})
39 // Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
40 Warning(args ...interface{})
41 // Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
42 Warningln(args ...interface{})
43 // Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
44 Warningf(format string, args ...interface{})
45 // Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
46 Error(args ...interface{})
47 // Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
48 Errorln(args ...interface{})
49 // Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
50 Errorf(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.
54 Fatal(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.
58 Fatalln(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.
62 Fatalf(format string, args ...interface{})
63 // V reports whether verbosity level l is at least the requested verbose level.
64 V(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) {
amit.ghosh258d14c2020-10-02 15:13:38 +020070 grpclog.Logger = l
71 grpclog.DepthLogger, _ = l.(grpclog.DepthLoggerV2)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070072}
73
74const (
75 // infoLog indicates Info severity.
76 infoLog int = iota
77 // warningLog indicates Warning severity.
78 warningLog
79 // errorLog indicates Error severity.
80 errorLog
81 // fatalLog indicates Fatal severity.
82 fatalLog
83)
84
85// severityName contains the string representation of each severity.
86var severityName = []string{
87 infoLog: "INFO",
88 warningLog: "WARNING",
89 errorLog: "ERROR",
90 fatalLog: "FATAL",
91}
92
93// loggerT is the default logger used by grpclog.
94type loggerT struct {
95 m []*log.Logger
96 v int
97}
98
99// NewLoggerV2 creates a loggerV2 with the provided writers.
100// Fatal logs will be written to errorW, warningW, infoW, followed by exit(1).
101// Error logs will be written to errorW, warningW and infoW.
102// Warning logs will be written to warningW and infoW.
103// Info logs will be written to infoW.
104func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
105 return NewLoggerV2WithVerbosity(infoW, warningW, errorW, 0)
106}
107
108// NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
109// verbosity level.
110func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
111 var m []*log.Logger
112 m = append(m, log.New(infoW, severityName[infoLog]+": ", log.LstdFlags))
113 m = append(m, log.New(io.MultiWriter(infoW, warningW), severityName[warningLog]+": ", log.LstdFlags))
114 ew := io.MultiWriter(infoW, warningW, errorW) // ew will be used for error and fatal.
115 m = append(m, log.New(ew, severityName[errorLog]+": ", log.LstdFlags))
116 m = append(m, log.New(ew, severityName[fatalLog]+": ", log.LstdFlags))
117 return &loggerT{m: m, v: v}
118}
119
120// newLoggerV2 creates a loggerV2 to be used as default logger.
121// All logs are written to stderr.
122func newLoggerV2() LoggerV2 {
123 errorW := ioutil.Discard
124 warningW := ioutil.Discard
125 infoW := ioutil.Discard
126
127 logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL")
128 switch logLevel {
129 case "", "ERROR", "error": // If env is unset, set level to ERROR.
130 errorW = os.Stderr
131 case "WARNING", "warning":
132 warningW = os.Stderr
133 case "INFO", "info":
134 infoW = os.Stderr
135 }
136
137 var v int
138 vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
139 if vl, err := strconv.Atoi(vLevel); err == nil {
140 v = vl
141 }
142 return NewLoggerV2WithVerbosity(infoW, warningW, errorW, v)
143}
144
145func (g *loggerT) Info(args ...interface{}) {
146 g.m[infoLog].Print(args...)
147}
148
149func (g *loggerT) Infoln(args ...interface{}) {
150 g.m[infoLog].Println(args...)
151}
152
153func (g *loggerT) Infof(format string, args ...interface{}) {
154 g.m[infoLog].Printf(format, args...)
155}
156
157func (g *loggerT) Warning(args ...interface{}) {
158 g.m[warningLog].Print(args...)
159}
160
161func (g *loggerT) Warningln(args ...interface{}) {
162 g.m[warningLog].Println(args...)
163}
164
165func (g *loggerT) Warningf(format string, args ...interface{}) {
166 g.m[warningLog].Printf(format, args...)
167}
168
169func (g *loggerT) Error(args ...interface{}) {
170 g.m[errorLog].Print(args...)
171}
172
173func (g *loggerT) Errorln(args ...interface{}) {
174 g.m[errorLog].Println(args...)
175}
176
177func (g *loggerT) Errorf(format string, args ...interface{}) {
178 g.m[errorLog].Printf(format, args...)
179}
180
181func (g *loggerT) Fatal(args ...interface{}) {
182 g.m[fatalLog].Fatal(args...)
183 // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
184}
185
186func (g *loggerT) Fatalln(args ...interface{}) {
187 g.m[fatalLog].Fatalln(args...)
188 // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
189}
190
191func (g *loggerT) Fatalf(format string, args ...interface{}) {
192 g.m[fatalLog].Fatalf(format, args...)
193 // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit().
194}
195
196func (g *loggerT) V(l int) bool {
197 return l <= g.v
198}
amit.ghosh258d14c2020-10-02 15:13:38 +0200199
200// DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements
201// DepthLoggerV2, the below functions will be called with the appropriate stack
202// depth set for trivial functions the logger may ignore.
203//
204// This API is EXPERIMENTAL.
205type DepthLoggerV2 interface {
206 // InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Print.
207 InfoDepth(depth int, args ...interface{})
208 // WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Print.
209 WarningDepth(depth int, args ...interface{})
210 // ErrorDetph logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Print.
211 ErrorDepth(depth int, args ...interface{})
212 // FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Print.
213 FatalDepth(depth int, args ...interface{})
214}