github.com/polarismesh/polaris@v1.17.8/common/log/options.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package log 19 20 import ( 21 "errors" 22 ) 23 24 const ( 25 DefaultLoggerName = "default" 26 defaultOutputLevel = InfoLevel 27 defaultStackTraceLevel = NoneLevel 28 defaultOutputPath = "stdout" 29 defaultErrorOutputPath = "stderr" 30 defaultRotationMaxAge = 7 31 defaultRotationMaxSize = 100 32 defaultRotationMaxBackups = 10 33 ) 34 35 // Level is an enumeration of all supported log levels. 36 type Level int 37 38 const ( 39 // NoneLevel disables logging 40 NoneLevel Level = iota 41 // FatalLevel enables fatal level logging 42 FatalLevel 43 // ErrorLevel enables error level logging 44 ErrorLevel 45 // WarnLevel enables warn level logging 46 WarnLevel 47 // InfoLevel enables info level logging 48 InfoLevel 49 // DebugLevel enables debug level logging 50 DebugLevel 51 ) 52 53 var levelToString = map[Level]string{ 54 DebugLevel: "debug", 55 InfoLevel: "info", 56 WarnLevel: "warn", 57 ErrorLevel: "error", 58 FatalLevel: "fatal", 59 NoneLevel: "none", 60 } 61 62 var stringToLevel = map[string]Level{ 63 "debug": DebugLevel, 64 "info": InfoLevel, 65 "warn": WarnLevel, 66 "error": ErrorLevel, 67 "fatal": FatalLevel, 68 "none": NoneLevel, 69 } 70 71 // Options defines the set of options supported by Istio's component logging package. 72 type Options struct { 73 // OutputPaths is a list of file system paths to write the log data to. 74 // The special values stdout and stderr can be used to output to the 75 // standard I/O streams. This defaults to stdout. 76 OutputPaths []string `yaml:"outputPaths"` 77 78 // ErrorOutputPaths is a list of file system paths to write logger errors to. 79 // The special values stdout and stderr can be used to output to the 80 // standard I/O streams. This defaults to stderr. 81 ErrorOutputPaths []string `yaml:"errorOutputPaths"` 82 83 // RotateOutputPath is the path to a rotating log file. This file should 84 // be automatically rotated over time, based on the rotation parameters such 85 // as RotationMaxSize and RotationMaxAge. The default is to not rotate. 86 // 87 // This path is used as a foundational path. This is where log output is normally 88 // saved. When a rotation needs to take place because the file got too big or too 89 // old, then the file is renamed by appending a timestamp to the name. Such renamed 90 // files are called backups. Once a backup has been created, 91 // output resumes to this path. 92 RotateOutputPath string `yaml:"rotateOutputPath"` 93 94 // RotateOutputPath is the path to a rotating error log file. This file should 95 // be automatically rotated over time, based on the rotation parameters such 96 // as RotationMaxSize and RotationMaxAge. The default is to not rotate. 97 // 98 // This path is used as a foundational path. This is where log output is normally 99 // saved. When a rotation needs to take place because the file got too big or too 100 // old, then the file is renamed by appending a timestamp to the name. Such renamed 101 // files are called backups. Once a backup has been created, 102 // output resumes to this path. 103 ErrorRotateOutputPath string `yaml:"errorRotateOutputPath"` 104 105 // RotationMaxSize is the maximum size in megabytes of a log file before it gets 106 // rotated. It defaults to 100 megabytes. 107 RotationMaxSize int `yaml:"rotationMaxSize"` 108 109 // RotationMaxAge is the maximum number of days to retain old log files based on the 110 // timestamp encoded in their filename. Note that a day is defined as 24 111 // hours and may not exactly correspond to calendar days due to daylight 112 // savings, leap seconds, etc. The default is to remove log files 113 // older than 30 days. 114 RotationMaxAge int `yaml:"rotationMaxAge"` 115 116 // RotationMaxBackups is the maximum number of old log files to retain. The default 117 // is to retain at most 1000 logs. 118 RotationMaxBackups int `yaml:"rotationMaxBackups"` 119 120 // JSONEncoding controls whether the log is formatted as JSON. 121 JSONEncoding bool `yaml:"jsonEncoding"` 122 123 // LogGrpc indicates that Grpc logs should be captured. The default is true. 124 // This is not exposed through the command-line flags, as this flag is mainly useful for testing: Grpc 125 // stack will hold on to the logger even though it gets closed. This causes data races. 126 LogGrpc bool 127 128 // RotationMaxDurationForHour 129 RotationMaxDurationForHour int `yaml:"rotationMaxDurationForHour"` 130 131 Compress bool `yaml:"compress"` 132 OutputLevel string `yaml:"outputLevel"` 133 StackTraceLevel string `yaml:"stackTraceLevel"` 134 DisableLogCaller bool `yaml:"disableLogCaller"` 135 OnlyContent bool `yaml:"onlyContent"` 136 } 137 138 // DefaultOptions returns a new set of options, initialized to the defaults 139 func DefaultOptions() map[string]*Options { 140 optionsMap := make(map[string]*Options) 141 for _, typeName := range allLoggerTypes() { 142 optionsMap[typeName] = &Options{ 143 OutputPaths: []string{defaultOutputPath}, 144 ErrorOutputPaths: []string{defaultErrorOutputPath}, 145 RotationMaxSize: defaultRotationMaxSize, 146 RotationMaxAge: defaultRotationMaxAge, 147 RotationMaxBackups: defaultRotationMaxBackups, 148 OutputLevel: levelToString[defaultOutputLevel], 149 StackTraceLevel: levelToString[defaultStackTraceLevel], 150 LogGrpc: false, 151 } 152 } 153 return optionsMap 154 } 155 156 // SetOutputLevel sets the minimum log output level for a given scope. 157 func (o *Options) SetOutputLevel(level string) error { 158 _, exist := stringToLevel[level] 159 if !exist { 160 return errors.New("invalid log level") 161 } 162 o.OutputLevel = level 163 return nil 164 } 165 166 // GetOutputLevel returns the minimum log output level for a given scope. 167 func (o *Options) GetOutputLevel() Level { 168 return stringToLevel[o.OutputLevel] 169 } 170 171 // SetStackTraceLevel sets the minimum stack tracing level for a given scope. 172 func (o *Options) SetStackTraceLevel(level string) error { 173 _, exist := stringToLevel[level] 174 if !exist { 175 return errors.New("invalid stack trace level") 176 } 177 o.StackTraceLevel = level 178 return nil 179 } 180 181 // GetStackTraceLevel returns the minimum stack tracing level for a given scope. 182 func (o *Options) GetStackTraceLevel() Level { 183 return stringToLevel[o.StackTraceLevel] 184 } 185 186 func (l Level) Name() string { 187 return levelToString[l] 188 }