loggerv2.go

 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	"os"
24	"strconv"
25	"strings"
26
27	"google.golang.org/grpc/grpclog/internal"
28)
29
30// LoggerV2 does underlying logging work for grpclog.
31type LoggerV2 internal.LoggerV2
32
33// SetLoggerV2 sets logger that is used in grpc to a V2 logger.
34// Not mutex-protected, should be called before any gRPC functions.
35func SetLoggerV2(l LoggerV2) {
36	if _, ok := l.(*componentData); ok {
37		panic("cannot use component logger as grpclog logger")
38	}
39	internal.LoggerV2Impl = l
40	internal.DepthLoggerV2Impl, _ = l.(internal.DepthLoggerV2)
41}
42
43// NewLoggerV2 creates a loggerV2 with the provided writers.
44// Fatal logs will be written to errorW, warningW, infoW, followed by exit(1).
45// Error logs will be written to errorW, warningW and infoW.
46// Warning logs will be written to warningW and infoW.
47// Info logs will be written to infoW.
48func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
49	return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{})
50}
51
52// NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
53// verbosity level.
54func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
55	return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{Verbosity: v})
56}
57
58// newLoggerV2 creates a loggerV2 to be used as default logger.
59// All logs are written to stderr.
60func newLoggerV2() LoggerV2 {
61	errorW := io.Discard
62	warningW := io.Discard
63	infoW := io.Discard
64
65	logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL")
66	switch logLevel {
67	case "", "ERROR", "error": // If env is unset, set level to ERROR.
68		errorW = os.Stderr
69	case "WARNING", "warning":
70		warningW = os.Stderr
71	case "INFO", "info":
72		infoW = os.Stderr
73	}
74
75	var v int
76	vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
77	if vl, err := strconv.Atoi(vLevel); err == nil {
78		v = vl
79	}
80
81	jsonFormat := strings.EqualFold(os.Getenv("GRPC_GO_LOG_FORMATTER"), "json")
82
83	return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{
84		Verbosity:  v,
85		FormatJSON: jsonFormat,
86	})
87}
88
89// DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements
90// DepthLoggerV2, the below functions will be called with the appropriate stack
91// depth set for trivial functions the logger may ignore.
92//
93// # Experimental
94//
95// Notice: This type is EXPERIMENTAL and may be changed or removed in a
96// later release.
97type DepthLoggerV2 internal.DepthLoggerV2