Skip to content

Commit c98235b

Browse files
authored
grpclog: refactor to move implementation to grpclog/internal (#7465)
1 parent 7ec3fd2 commit c98235b

File tree

11 files changed

+365
-325
lines changed

11 files changed

+365
-325
lines changed

grpclog/component.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package grpclog
2020

2121
import (
2222
"fmt"
23-
24-
"google.golang.org/grpc/internal/grpclog"
2523
)
2624

2725
// componentData records the settings for a component.
@@ -33,22 +31,22 @@ var cache = map[string]*componentData{}
3331

3432
func (c *componentData) InfoDepth(depth int, args ...any) {
3533
args = append([]any{"[" + string(c.name) + "]"}, args...)
36-
grpclog.InfoDepth(depth+1, args...)
34+
InfoDepth(depth+1, args...)
3735
}
3836

3937
func (c *componentData) WarningDepth(depth int, args ...any) {
4038
args = append([]any{"[" + string(c.name) + "]"}, args...)
41-
grpclog.WarningDepth(depth+1, args...)
39+
WarningDepth(depth+1, args...)
4240
}
4341

4442
func (c *componentData) ErrorDepth(depth int, args ...any) {
4543
args = append([]any{"[" + string(c.name) + "]"}, args...)
46-
grpclog.ErrorDepth(depth+1, args...)
44+
ErrorDepth(depth+1, args...)
4745
}
4846

4947
func (c *componentData) FatalDepth(depth int, args ...any) {
5048
args = append([]any{"[" + string(c.name) + "]"}, args...)
51-
grpclog.FatalDepth(depth+1, args...)
49+
FatalDepth(depth+1, args...)
5250
}
5351

5452
func (c *componentData) Info(args ...any) {

grpclog/grpclog.go

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@
1818

1919
// Package grpclog defines logging for grpc.
2020
//
21-
// All logs in transport and grpclb packages only go to verbose level 2.
22-
// All logs in other packages in grpc are logged in spite of the verbosity level.
23-
//
24-
// In the default logger,
25-
// severity level can be set by environment variable GRPC_GO_LOG_SEVERITY_LEVEL,
26-
// verbosity level can be set by GRPC_GO_LOG_VERBOSITY_LEVEL.
27-
package grpclog // import "google.golang.org/grpc/grpclog"
21+
// In the default logger, severity level can be set by environment variable
22+
// GRPC_GO_LOG_SEVERITY_LEVEL, verbosity level can be set by
23+
// GRPC_GO_LOG_VERBOSITY_LEVEL.
24+
package grpclog
2825

2926
import (
3027
"os"
3128

32-
"google.golang.org/grpc/internal/grpclog"
29+
"google.golang.org/grpc/grpclog/internal"
3330
)
3431

3532
func init() {
@@ -38,74 +35,74 @@ func init() {
3835

3936
// V reports whether verbosity level l is at least the requested verbose level.
4037
func V(l int) bool {
41-
return grpclog.Logger.V(l)
38+
return internal.LoggerV2Impl.V(l)
4239
}
4340

4441
// Info logs to the INFO log.
4542
func Info(args ...any) {
46-
grpclog.Logger.Info(args...)
43+
internal.LoggerV2Impl.Info(args...)
4744
}
4845

4946
// Infof logs to the INFO log. Arguments are handled in the manner of fmt.Printf.
5047
func Infof(format string, args ...any) {
51-
grpclog.Logger.Infof(format, args...)
48+
internal.LoggerV2Impl.Infof(format, args...)
5249
}
5350

5451
// Infoln logs to the INFO log. Arguments are handled in the manner of fmt.Println.
5552
func Infoln(args ...any) {
56-
grpclog.Logger.Infoln(args...)
53+
internal.LoggerV2Impl.Infoln(args...)
5754
}
5855

5956
// Warning logs to the WARNING log.
6057
func Warning(args ...any) {
61-
grpclog.Logger.Warning(args...)
58+
internal.LoggerV2Impl.Warning(args...)
6259
}
6360

6461
// Warningf logs to the WARNING log. Arguments are handled in the manner of fmt.Printf.
6562
func Warningf(format string, args ...any) {
66-
grpclog.Logger.Warningf(format, args...)
63+
internal.LoggerV2Impl.Warningf(format, args...)
6764
}
6865

6966
// Warningln logs to the WARNING log. Arguments are handled in the manner of fmt.Println.
7067
func Warningln(args ...any) {
71-
grpclog.Logger.Warningln(args...)
68+
internal.LoggerV2Impl.Warningln(args...)
7269
}
7370

7471
// Error logs to the ERROR log.
7572
func Error(args ...any) {
76-
grpclog.Logger.Error(args...)
73+
internal.LoggerV2Impl.Error(args...)
7774
}
7875

7976
// Errorf logs to the ERROR log. Arguments are handled in the manner of fmt.Printf.
8077
func Errorf(format string, args ...any) {
81-
grpclog.Logger.Errorf(format, args...)
78+
internal.LoggerV2Impl.Errorf(format, args...)
8279
}
8380

8481
// Errorln logs to the ERROR log. Arguments are handled in the manner of fmt.Println.
8582
func Errorln(args ...any) {
86-
grpclog.Logger.Errorln(args...)
83+
internal.LoggerV2Impl.Errorln(args...)
8784
}
8885

8986
// Fatal logs to the FATAL log. Arguments are handled in the manner of fmt.Print.
9087
// It calls os.Exit() with exit code 1.
9188
func Fatal(args ...any) {
92-
grpclog.Logger.Fatal(args...)
89+
internal.LoggerV2Impl.Fatal(args...)
9390
// Make sure fatal logs will exit.
9491
os.Exit(1)
9592
}
9693

9794
// Fatalf logs to the FATAL log. Arguments are handled in the manner of fmt.Printf.
9895
// It calls os.Exit() with exit code 1.
9996
func Fatalf(format string, args ...any) {
100-
grpclog.Logger.Fatalf(format, args...)
97+
internal.LoggerV2Impl.Fatalf(format, args...)
10198
// Make sure fatal logs will exit.
10299
os.Exit(1)
103100
}
104101

105102
// Fatalln logs to the FATAL log. Arguments are handled in the manner of fmt.Println.
106103
// It calls os.Exit() with exit code 1.
107104
func Fatalln(args ...any) {
108-
grpclog.Logger.Fatalln(args...)
105+
internal.LoggerV2Impl.Fatalln(args...)
109106
// Make sure fatal logs will exit.
110107
os.Exit(1)
111108
}
@@ -114,19 +111,76 @@ func Fatalln(args ...any) {
114111
//
115112
// Deprecated: use Info.
116113
func Print(args ...any) {
117-
grpclog.Logger.Info(args...)
114+
internal.LoggerV2Impl.Info(args...)
118115
}
119116

120117
// Printf prints to the logger. Arguments are handled in the manner of fmt.Printf.
121118
//
122119
// Deprecated: use Infof.
123120
func Printf(format string, args ...any) {
124-
grpclog.Logger.Infof(format, args...)
121+
internal.LoggerV2Impl.Infof(format, args...)
125122
}
126123

127124
// Println prints to the logger. Arguments are handled in the manner of fmt.Println.
128125
//
129126
// Deprecated: use Infoln.
130127
func Println(args ...any) {
131-
grpclog.Logger.Infoln(args...)
128+
internal.LoggerV2Impl.Infoln(args...)
129+
}
130+
131+
// InfoDepth logs to the INFO log at the specified depth.
132+
//
133+
// # Experimental
134+
//
135+
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
136+
// later release.
137+
func InfoDepth(depth int, args ...any) {
138+
if internal.DepthLoggerV2Impl != nil {
139+
internal.DepthLoggerV2Impl.InfoDepth(depth, args...)
140+
} else {
141+
internal.LoggerV2Impl.Infoln(args...)
142+
}
143+
}
144+
145+
// WarningDepth logs to the WARNING log at the specified depth.
146+
//
147+
// # Experimental
148+
//
149+
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
150+
// later release.
151+
func WarningDepth(depth int, args ...any) {
152+
if internal.DepthLoggerV2Impl != nil {
153+
internal.DepthLoggerV2Impl.WarningDepth(depth, args...)
154+
} else {
155+
internal.LoggerV2Impl.Warningln(args...)
156+
}
157+
}
158+
159+
// ErrorDepth logs to the ERROR log at the specified depth.
160+
//
161+
// # Experimental
162+
//
163+
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
164+
// later release.
165+
func ErrorDepth(depth int, args ...any) {
166+
if internal.DepthLoggerV2Impl != nil {
167+
internal.DepthLoggerV2Impl.ErrorDepth(depth, args...)
168+
} else {
169+
internal.LoggerV2Impl.Errorln(args...)
170+
}
171+
}
172+
173+
// FatalDepth logs to the FATAL log at the specified depth.
174+
//
175+
// # Experimental
176+
//
177+
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
178+
// later release.
179+
func FatalDepth(depth int, args ...any) {
180+
if internal.DepthLoggerV2Impl != nil {
181+
internal.DepthLoggerV2Impl.FatalDepth(depth, args...)
182+
} else {
183+
internal.LoggerV2Impl.Fatalln(args...)
184+
}
185+
os.Exit(1)
132186
}

grpclog/internal/grpclog.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* Copyright 2024 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+
19+
// Package internal contains functionality internal to the grpclog package.
20+
package internal
21+
22+
// LoggerV2Impl is the logger used for the non-depth log functions.
23+
var LoggerV2Impl LoggerV2
24+
25+
// DepthLoggerV2Impl is the logger used for the depth log functions.
26+
var DepthLoggerV2Impl DepthLoggerV2

grpclog/internal/logger.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
*
3+
* Copyright 2024 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+
19+
package internal
20+
21+
// Logger mimics golang's standard Logger as an interface.
22+
//
23+
// Deprecated: use LoggerV2.
24+
type Logger interface {
25+
Fatal(args ...any)
26+
Fatalf(format string, args ...any)
27+
Fatalln(args ...any)
28+
Print(args ...any)
29+
Printf(format string, args ...any)
30+
Println(args ...any)
31+
}
32+
33+
// LoggerWrapper wraps Logger into a LoggerV2.
34+
type LoggerWrapper struct {
35+
Logger
36+
}
37+
38+
// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
39+
func (l *LoggerWrapper) Info(args ...any) {
40+
l.Logger.Print(args...)
41+
}
42+
43+
// Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
44+
func (l *LoggerWrapper) Infoln(args ...any) {
45+
l.Logger.Println(args...)
46+
}
47+
48+
// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
49+
func (l *LoggerWrapper) Infof(format string, args ...any) {
50+
l.Logger.Printf(format, args...)
51+
}
52+
53+
// Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
54+
func (l *LoggerWrapper) Warning(args ...any) {
55+
l.Logger.Print(args...)
56+
}
57+
58+
// Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
59+
func (l *LoggerWrapper) Warningln(args ...any) {
60+
l.Logger.Println(args...)
61+
}
62+
63+
// Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
64+
func (l *LoggerWrapper) Warningf(format string, args ...any) {
65+
l.Logger.Printf(format, args...)
66+
}
67+
68+
// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
69+
func (l *LoggerWrapper) Error(args ...any) {
70+
l.Logger.Print(args...)
71+
}
72+
73+
// Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
74+
func (l *LoggerWrapper) Errorln(args ...any) {
75+
l.Logger.Println(args...)
76+
}
77+
78+
// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
79+
func (l *LoggerWrapper) Errorf(format string, args ...any) {
80+
l.Logger.Printf(format, args...)
81+
}
82+
83+
// V reports whether verbosity level l is at least the requested verbose level.
84+
func (*LoggerWrapper) V(l int) bool {
85+
// Returns true for all verbose level.
86+
return true
87+
}

0 commit comments

Comments
 (0)