Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpclog: refactor to move implementation to grpclog/internal #7465

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions grpclog/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package grpclog

import (
"fmt"

"google.golang.org/grpc/internal/grpclog"
)

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

func (c *componentData) InfoDepth(depth int, args ...any) {
args = append([]any{"[" + string(c.name) + "]"}, args...)
grpclog.InfoDepth(depth+1, args...)
InfoDepth(depth+1, args...)
}

func (c *componentData) WarningDepth(depth int, args ...any) {
args = append([]any{"[" + string(c.name) + "]"}, args...)
grpclog.WarningDepth(depth+1, args...)
WarningDepth(depth+1, args...)
}

func (c *componentData) ErrorDepth(depth int, args ...any) {
args = append([]any{"[" + string(c.name) + "]"}, args...)
grpclog.ErrorDepth(depth+1, args...)
ErrorDepth(depth+1, args...)
}

func (c *componentData) FatalDepth(depth int, args ...any) {
args = append([]any{"[" + string(c.name) + "]"}, args...)
grpclog.FatalDepth(depth+1, args...)
FatalDepth(depth+1, args...)
}

func (c *componentData) Info(args ...any) {
Expand Down
102 changes: 78 additions & 24 deletions grpclog/grpclog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@

// Package grpclog defines logging for grpc.
//
// All logs in transport and grpclb packages only go to verbose level 2.
// All logs in other packages in grpc are logged in spite of the verbosity level.
//
// In the default logger,
// severity level can be set by environment variable GRPC_GO_LOG_SEVERITY_LEVEL,
// verbosity level can be set by GRPC_GO_LOG_VERBOSITY_LEVEL.
package grpclog // import "google.golang.org/grpc/grpclog"
// In the default logger, severity level can be set by environment variable
// GRPC_GO_LOG_SEVERITY_LEVEL, verbosity level can be set by
// GRPC_GO_LOG_VERBOSITY_LEVEL.
package grpclog

import (
"os"

"google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/grpclog/internal"
)

func init() {
Expand All @@ -38,74 +35,74 @@

// V reports whether verbosity level l is at least the requested verbose level.
func V(l int) bool {
return grpclog.Logger.V(l)
return internal.LoggerV2Impl.V(l)
}

// Info logs to the INFO log.
func Info(args ...any) {
grpclog.Logger.Info(args...)
internal.LoggerV2Impl.Info(args...)
}

// Infof logs to the INFO log. Arguments are handled in the manner of fmt.Printf.
func Infof(format string, args ...any) {
grpclog.Logger.Infof(format, args...)
internal.LoggerV2Impl.Infof(format, args...)
}

// Infoln logs to the INFO log. Arguments are handled in the manner of fmt.Println.
func Infoln(args ...any) {
grpclog.Logger.Infoln(args...)
internal.LoggerV2Impl.Infoln(args...)
}

// Warning logs to the WARNING log.
func Warning(args ...any) {
grpclog.Logger.Warning(args...)
internal.LoggerV2Impl.Warning(args...)
}

// Warningf logs to the WARNING log. Arguments are handled in the manner of fmt.Printf.
func Warningf(format string, args ...any) {
grpclog.Logger.Warningf(format, args...)
internal.LoggerV2Impl.Warningf(format, args...)
}

// Warningln logs to the WARNING log. Arguments are handled in the manner of fmt.Println.
func Warningln(args ...any) {
grpclog.Logger.Warningln(args...)
internal.LoggerV2Impl.Warningln(args...)
}

// Error logs to the ERROR log.
func Error(args ...any) {
grpclog.Logger.Error(args...)
internal.LoggerV2Impl.Error(args...)
}

// Errorf logs to the ERROR log. Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, args ...any) {
grpclog.Logger.Errorf(format, args...)
internal.LoggerV2Impl.Errorf(format, args...)
}

// Errorln logs to the ERROR log. Arguments are handled in the manner of fmt.Println.
func Errorln(args ...any) {
grpclog.Logger.Errorln(args...)
internal.LoggerV2Impl.Errorln(args...)
}

// Fatal logs to the FATAL log. Arguments are handled in the manner of fmt.Print.
// It calls os.Exit() with exit code 1.
func Fatal(args ...any) {
grpclog.Logger.Fatal(args...)
internal.LoggerV2Impl.Fatal(args...)

Check warning on line 89 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L89

Added line #L89 was not covered by tests
// Make sure fatal logs will exit.
os.Exit(1)
}

// Fatalf logs to the FATAL log. Arguments are handled in the manner of fmt.Printf.
// It calls os.Exit() with exit code 1.
func Fatalf(format string, args ...any) {
grpclog.Logger.Fatalf(format, args...)
internal.LoggerV2Impl.Fatalf(format, args...)

Check warning on line 97 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L97

Added line #L97 was not covered by tests
// Make sure fatal logs will exit.
os.Exit(1)
}

// Fatalln logs to the FATAL log. Arguments are handled in the manner of fmt.Println.
// It calls os.Exit() with exit code 1.
func Fatalln(args ...any) {
grpclog.Logger.Fatalln(args...)
internal.LoggerV2Impl.Fatalln(args...)

Check warning on line 105 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L105

Added line #L105 was not covered by tests
// Make sure fatal logs will exit.
os.Exit(1)
}
Expand All @@ -114,19 +111,76 @@
//
// Deprecated: use Info.
func Print(args ...any) {
grpclog.Logger.Info(args...)
internal.LoggerV2Impl.Info(args...)

Check warning on line 114 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L114

Added line #L114 was not covered by tests
}

// Printf prints to the logger. Arguments are handled in the manner of fmt.Printf.
//
// Deprecated: use Infof.
func Printf(format string, args ...any) {
grpclog.Logger.Infof(format, args...)
internal.LoggerV2Impl.Infof(format, args...)

Check warning on line 121 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L121

Added line #L121 was not covered by tests
}

// Println prints to the logger. Arguments are handled in the manner of fmt.Println.
//
// Deprecated: use Infoln.
func Println(args ...any) {
grpclog.Logger.Infoln(args...)
internal.LoggerV2Impl.Infoln(args...)

Check warning on line 128 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L128

Added line #L128 was not covered by tests
}

// InfoDepth logs to the INFO log at the specified depth.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func InfoDepth(depth int, args ...any) {
if internal.DepthLoggerV2Impl != nil {
internal.DepthLoggerV2Impl.InfoDepth(depth, args...)
} else {
internal.LoggerV2Impl.Infoln(args...)
}
}

// WarningDepth logs to the WARNING log at the specified depth.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func WarningDepth(depth int, args ...any) {
if internal.DepthLoggerV2Impl != nil {
internal.DepthLoggerV2Impl.WarningDepth(depth, args...)
} else {
internal.LoggerV2Impl.Warningln(args...)
}
}

// ErrorDepth logs to the ERROR log at the specified depth.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ErrorDepth(depth int, args ...any) {
if internal.DepthLoggerV2Impl != nil {
internal.DepthLoggerV2Impl.ErrorDepth(depth, args...)
} else {
internal.LoggerV2Impl.Errorln(args...)

Check warning on line 169 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L169

Added line #L169 was not covered by tests
}
}

// FatalDepth logs to the FATAL log at the specified depth.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func FatalDepth(depth int, args ...any) {
if internal.DepthLoggerV2Impl != nil {
internal.DepthLoggerV2Impl.FatalDepth(depth, args...)
} else {
internal.LoggerV2Impl.Fatalln(args...)

Check warning on line 183 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L183

Added line #L183 was not covered by tests
}
os.Exit(1)

Check warning on line 185 in grpclog/grpclog.go

View check run for this annotation

Codecov / codecov/patch

grpclog/grpclog.go#L185

Added line #L185 was not covered by tests
}
26 changes: 26 additions & 0 deletions grpclog/internal/grpclog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Package internal contains functionality internal to the grpclog package.
package internal

// LoggerV2Impl is the logger used for the non-depth log functions.
var LoggerV2Impl LoggerV2

// DepthLoggerV2Impl is the logger used for the depth log functions.
var DepthLoggerV2Impl DepthLoggerV2
87 changes: 87 additions & 0 deletions grpclog/internal/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package internal

// Logger mimics golang's standard Logger as an interface.
//
// Deprecated: use LoggerV2.
type Logger interface {
Fatal(args ...any)
Fatalf(format string, args ...any)
Fatalln(args ...any)
Print(args ...any)
Printf(format string, args ...any)
Println(args ...any)
}

// LoggerWrapper wraps Logger into a LoggerV2.
type LoggerWrapper struct {
Logger
}

// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
func (l *LoggerWrapper) Info(args ...any) {
l.Logger.Print(args...)

Check warning on line 40 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}

// Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
func (l *LoggerWrapper) Infoln(args ...any) {
l.Logger.Println(args...)

Check warning on line 45 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func (l *LoggerWrapper) Infof(format string, args ...any) {
l.Logger.Printf(format, args...)

Check warning on line 50 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}

// Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func (l *LoggerWrapper) Warning(args ...any) {
l.Logger.Print(args...)

Check warning on line 55 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}

// Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
func (l *LoggerWrapper) Warningln(args ...any) {
l.Logger.Println(args...)

Check warning on line 60 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

// Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func (l *LoggerWrapper) Warningf(format string, args ...any) {
l.Logger.Printf(format, args...)

Check warning on line 65 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func (l *LoggerWrapper) Error(args ...any) {
l.Logger.Print(args...)

Check warning on line 70 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

// Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
func (l *LoggerWrapper) Errorln(args ...any) {
l.Logger.Println(args...)

Check warning on line 75 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L74-L75

Added lines #L74 - L75 were not covered by tests
}

// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func (l *LoggerWrapper) Errorf(format string, args ...any) {
l.Logger.Printf(format, args...)

Check warning on line 80 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L79-L80

Added lines #L79 - L80 were not covered by tests
}

// V reports whether verbosity level l is at least the requested verbose level.
func (*LoggerWrapper) V(l int) bool {

Check warning on line 84 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L84

Added line #L84 was not covered by tests
// Returns true for all verbose level.
return true

Check warning on line 86 in grpclog/internal/logger.go

View check run for this annotation

Codecov / codecov/patch

grpclog/internal/logger.go#L86

Added line #L86 was not covered by tests
}
Loading
Loading