1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package logprinter
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"go.uber.org/zap"
)
var (
outputFmt = DisplayModeDefault // global output format of logger
stdout io.Writer = os.Stdout
stderr io.Writer = os.Stderr
)
// DisplayMode control the output format
type DisplayMode int
// display modes
const (
DisplayModeDefault DisplayMode = iota // default is the interactive output
DisplayModePlain // plain text
DisplayModeJSON // JSON
)
func fmtDisplayMode(m string) DisplayMode {
var dp DisplayMode
switch strings.ToLower(m) {
case "json":
dp = DisplayModeJSON
case "plain", "text":
dp = DisplayModePlain
default:
dp = DisplayModeDefault
}
return dp
}
func printLog(w io.Writer, mode DisplayMode, level, format string, args ...any) {
switch mode {
case DisplayModeJSON:
obj := struct {
Level string `json:"level"`
Msg string `json:"message"`
}{
Level: level,
Msg: fmt.Sprintf(format, args...),
}
data, err := json.Marshal(obj)
if err != nil {
_, _ = fmt.Fprintf(w, "{\"error\":\"%s\"}", err)
return
}
_, _ = fmt.Fprint(w, string(data)+"\n")
default:
_, _ = fmt.Fprintf(w, format+"\n", args...)
}
}
// SetDisplayMode changes the global output format of logger
func SetDisplayMode(m DisplayMode) {
outputFmt = m
}
// GetDisplayMode returns the current global output format
func GetDisplayMode() DisplayMode {
return outputFmt
}
// SetDisplayModeFromString changes the global output format of logger
func SetDisplayModeFromString(m string) {
outputFmt = fmtDisplayMode(m)
}
// Debugf output the debug message to console
func Debugf(format string, args ...any) {
zap.L().Debug(fmt.Sprintf(format, args...))
}
// Infof output the log message to console
// Deprecated: Use zap.L().Info() instead
func Infof(format string, args ...any) {
zap.L().Info(fmt.Sprintf(format, args...))
printLog(stdout, outputFmt, "info", format, args...)
}
// Warnf output the warning message to console
// Deprecated: Use zap.L().Warn() instead
func Warnf(format string, args ...any) {
zap.L().Warn(fmt.Sprintf(format, args...))
printLog(stderr, outputFmt, "warn", format, args...)
}
// Errorf output the error message to console
// Deprecated: Use zap.L().Error() instead
func Errorf(format string, args ...any) {
zap.L().Error(fmt.Sprintf(format, args...))
printLog(stderr, outputFmt, "error", format, args...)
}
// SetStdout redirect stdout to a custom writer
func SetStdout(w io.Writer) {
stdout = w
}
// SetStderr redirect stderr to a custom writer
func SetStderr(w io.Writer) {
stderr = w
}
|