divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 17 | // Package Config provides dynamic logging configuration for specific Voltha component with loglevel lookup |
| 18 | // from etcd kvstore implemented using backend. |
| 19 | // Any Voltha component can start utilizing dynamic logging by starting goroutine of StartLogLevelConfigProcessing after |
| 20 | // starting kvClient for the component. |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 21 | |
| 22 | package config |
| 23 | |
| 24 | import ( |
| 25 | "context" |
| 26 | "crypto/md5" |
| 27 | "encoding/json" |
| 28 | "errors" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 30 | "os" |
| 31 | "strings" |
| 32 | ) |
| 33 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 34 | const ( |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 35 | defaultLogLevelKey = "default" // kvstore key containing default loglevel |
| 36 | globalConfigRootNode = "global" // Root Node in kvstore containing global config |
| 37 | initialGlobalDefaultLogLevelValue = "WARN" // Hard-coded Global Default loglevel pushed at PoD startup |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 38 | ) |
| 39 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 40 | // ComponentLogController represents a Configuration for Logging Config of specific Voltha component type |
| 41 | // It stores ComponentConfig and GlobalConfig of loglevel config of specific Voltha component type |
| 42 | // For example,ComponentLogController instance will be created for rw-core component |
| 43 | type ComponentLogController struct { |
| 44 | ComponentName string |
| 45 | componentNameConfig *ComponentConfig |
| 46 | GlobalConfig *ComponentConfig |
| 47 | configManager *ConfigManager |
| 48 | logHash [16]byte |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 49 | initialLogLevel string // Initial default log level set by helm chart |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func NewComponentLogController(cm *ConfigManager) (*ComponentLogController, error) { |
| 53 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 54 | logger.Debug("creating-new-component-log-controller") |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 55 | componentName := os.Getenv("COMPONENT_NAME") |
| 56 | if componentName == "" { |
| 57 | return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env") |
| 58 | } |
| 59 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 60 | var defaultLogLevel string |
| 61 | var err error |
| 62 | // Retrieve and save default log level; used for fallback if all loglevel config is cleared in etcd |
| 63 | if defaultLogLevel, err = log.LogLevelToString(log.GetDefaultLogLevel()); err != nil { |
| 64 | defaultLogLevel = "DEBUG" |
| 65 | } |
| 66 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 67 | return &ComponentLogController{ |
| 68 | ComponentName: componentName, |
| 69 | componentNameConfig: nil, |
| 70 | GlobalConfig: nil, |
| 71 | configManager: cm, |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 72 | initialLogLevel: defaultLogLevel, |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 73 | }, nil |
| 74 | |
| 75 | } |
| 76 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 77 | // StartLogLevelConfigProcessing initialize component config and global config |
| 78 | // Then, it persists initial default Loglevels into Config Store before |
| 79 | // starting the loading and processing of all Log Configuration |
| 80 | func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 81 | cc, err := NewComponentLogController(cm) |
| 82 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 83 | logger.Errorw("unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 84 | return |
| 85 | } |
| 86 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 87 | cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 88 | logger.Debugw("global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 89 | |
| 90 | cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 91 | logger.Debugw("component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 92 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 93 | cc.persistInitialDefaultLogConfigs(ctx) |
| 94 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 95 | cc.processLogConfig(ctx) |
| 96 | } |
| 97 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 98 | // Method to persist Global default loglevel into etcd, if not set yet |
| 99 | // It also checks and set Component default loglevel into etcd with initial loglevel set from command line |
| 100 | func (c *ComponentLogController) persistInitialDefaultLogConfigs(ctx context.Context) { |
| 101 | |
| 102 | _, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey) |
| 103 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 104 | logger.Debugw("failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 105 | |
| 106 | err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue) |
| 107 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 108 | logger.Errorw("failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | _, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey) |
| 113 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 114 | logger.Debugw("failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 115 | |
| 116 | err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel) |
| 117 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 118 | logger.Errorw("failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 123 | // ProcessLogConfig will first load and apply log config and then start waiting on component config and global config |
| 124 | // channels for any changes. Event channel will be recieved from backend for valid change type |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 125 | // Then data for componentn log config and global log config will be retrieved from backend and stored in updatedLogConfig in precedence order |
| 126 | // If any changes in updatedLogConfig will be applied on component |
| 127 | func (c *ComponentLogController) processLogConfig(ctx context.Context) { |
| 128 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 129 | // Load and apply Log Config for first time |
| 130 | initialLogConfig, err := c.buildUpdatedLogConfig(ctx) |
| 131 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 132 | logger.Warnw("unable-to-load-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 133 | } else { |
| 134 | if err := c.loadAndApplyLogConfig(initialLogConfig); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 135 | logger.Warnw("unable-to-apply-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 139 | componentConfigEventChan := c.componentNameConfig.MonitorForConfigChange(ctx) |
| 140 | |
| 141 | globalConfigEventChan := c.GlobalConfig.MonitorForConfigChange(ctx) |
| 142 | |
| 143 | // process the events for componentName and global config |
| 144 | var configEvent *ConfigChangeEvent |
| 145 | for { |
| 146 | select { |
| 147 | case configEvent = <-globalConfigEventChan: |
| 148 | case configEvent = <-componentConfigEventChan: |
| 149 | |
| 150 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 151 | logger.Debugw("processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 152 | |
| 153 | updatedLogConfig, err := c.buildUpdatedLogConfig(ctx) |
| 154 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 155 | logger.Warnw("unable-to-fetch-updated-log-config", log.Fields{"error": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 156 | continue |
| 157 | } |
| 158 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 159 | logger.Debugw("applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 160 | |
| 161 | if err := c.loadAndApplyLogConfig(updatedLogConfig); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 162 | logger.Warnw("unable-to-load-and-apply-log-config", log.Fields{"error": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | // get active loglevel from the zap logger |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 169 | func getActiveLogLevels() map[string]string { |
| 170 | loglevels := make(map[string]string) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 171 | |
| 172 | // now do the default log level |
| 173 | if level, err := log.LogLevelToString(log.GetDefaultLogLevel()); err == nil { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 174 | loglevels[defaultLogLevelKey] = level |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // do the per-package log levels |
| 178 | for _, packageName := range log.GetPackageNames() { |
| 179 | level, err := log.GetPackageLogLevel(packageName) |
| 180 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 181 | logger.Warnw("unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 182 | continue |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 183 | } |
| 184 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 185 | if l, err := log.LogLevelToString(level); err == nil { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 186 | loglevels[packageName] = l |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 187 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 188 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 189 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 190 | logger.Debugw("retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 191 | |
| 192 | return loglevels |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | func (c *ComponentLogController) getGlobalLogConfig(ctx context.Context) (string, error) { |
| 196 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 197 | globalDefaultLogLevel, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 198 | if err != nil { |
| 199 | return "", err |
| 200 | } |
| 201 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 202 | // Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value |
| 203 | // We should use hard-coded initial default value in such cases |
| 204 | if globalDefaultLogLevel == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 205 | logger.Warn("global-default-loglevel-not-found-in-config-store") |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 206 | globalDefaultLogLevel = initialGlobalDefaultLogLevelValue |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 207 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 208 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 209 | if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 210 | logger.Warnw("unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 211 | globalDefaultLogLevel = initialGlobalDefaultLogLevelValue |
| 212 | } |
| 213 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 214 | logger.Debugw("retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 215 | |
| 216 | return globalDefaultLogLevel, nil |
| 217 | } |
| 218 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 219 | func (c *ComponentLogController) getComponentLogConfig(ctx context.Context, globalDefaultLogLevel string) (map[string]string, error) { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 220 | componentLogConfig, err := c.componentNameConfig.RetrieveAll(ctx) |
| 221 | if err != nil { |
| 222 | return nil, err |
| 223 | } |
| 224 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 225 | effectiveDefaultLogLevel := "" |
| 226 | for logConfigKey, logConfigValue := range componentLogConfig { |
| 227 | if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 228 | logger.Warnw("unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 229 | delete(componentLogConfig, logConfigKey) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 230 | } else { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 231 | if logConfigKey == defaultLogLevelKey { |
| 232 | effectiveDefaultLogLevel = componentLogConfig[defaultLogLevelKey] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 236 | |
| 237 | // if default loglevel is not configured for the component, component should use |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 238 | // default loglevel configured at global level |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 239 | if effectiveDefaultLogLevel == "" { |
| 240 | effectiveDefaultLogLevel = globalDefaultLogLevel |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 241 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 242 | |
| 243 | componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel |
| 244 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 245 | logger.Debugw("retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 246 | |
| 247 | return componentLogConfig, nil |
| 248 | } |
| 249 | |
| 250 | // buildUpdatedLogConfig retrieve the global logConfig and component logConfig from backend |
| 251 | // component logConfig stores the log config with precedence order |
| 252 | // For example, If the global logConfig is set and component logConfig is set only for specific package then |
| 253 | // component logConfig is stored with global logConfig and component logConfig of specific package |
| 254 | // For example, If the global logConfig is set and component logConfig is set for specific package and as well as for default then |
| 255 | // component logConfig is stored with component logConfig data only |
| 256 | func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) { |
| 257 | globalLogLevel, err := c.getGlobalLogConfig(ctx) |
| 258 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 259 | logger.Errorw("unable-to-retrieve-global-log-config", log.Fields{"err": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 262 | componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 263 | if err != nil { |
| 264 | return nil, err |
| 265 | } |
| 266 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 267 | finalLogConfig := make(map[string]string) |
| 268 | for packageName, logLevel := range componentLogConfig { |
| 269 | finalLogConfig[strings.ReplaceAll(packageName, "#", "/")] = logLevel |
| 270 | } |
| 271 | |
| 272 | return finalLogConfig, nil |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // load and apply the current configuration for component name |
| 276 | // create hash of loaded configuration using GenerateLogConfigHash |
| 277 | // if there is previous hash stored, compare the hash to stored hash |
| 278 | // if there is any change will call UpdateLogLevels |
| 279 | func (c *ComponentLogController) loadAndApplyLogConfig(logConfig map[string]string) error { |
| 280 | currentLogHash, err := GenerateLogConfigHash(logConfig) |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 285 | if c.logHash != currentLogHash { |
| 286 | UpdateLogLevels(logConfig) |
| 287 | c.logHash = currentLogHash |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 288 | } else { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 289 | logger.Debug("effective-loglevel-config-same-as-currently-active") |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 290 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 291 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 292 | return nil |
| 293 | } |
| 294 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 295 | // createModifiedLogLevels loops through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig |
| 296 | // to identify and create map of modified Log Levels of 2 types: |
| 297 | // - Packages for which log level has been changed |
| 298 | // - Packages for which log level config has been cleared - set to default log level |
| 299 | func createModifiedLogLevels(activeLogLevels, updatedLogLevels map[string]string) map[string]string { |
| 300 | defaultLevel := updatedLogLevels[defaultLogLevelKey] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 301 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 302 | modifiedLogLevels := make(map[string]string) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 303 | for activeKey, activeLevel := range activeLogLevels { |
| 304 | if _, exist := updatedLogLevels[activeKey]; !exist { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 305 | if activeLevel != defaultLevel { |
| 306 | modifiedLogLevels[activeKey] = defaultLevel |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 307 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 308 | } else if activeLevel != updatedLogLevels[activeKey] { |
| 309 | modifiedLogLevels[activeKey] = updatedLogLevels[activeKey] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 312 | |
| 313 | // Log warnings for all invalid packages for which log config has been set |
| 314 | for key, value := range updatedLogLevels { |
| 315 | if _, exist := activeLogLevels[key]; !exist { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 316 | logger.Warnw("ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | return modifiedLogLevels |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // updateLogLevels update the loglevels for the component |
| 324 | // retrieve active confguration from logger |
| 325 | // compare with entries one by one and apply |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 326 | func UpdateLogLevels(updatedLogConfig map[string]string) { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 327 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 328 | activeLogLevels := getActiveLogLevels() |
| 329 | changedLogLevels := createModifiedLogLevels(activeLogLevels, updatedLogConfig) |
| 330 | |
| 331 | // If no changed log levels are found, just return. It may happen on configuration of a invalid package |
| 332 | if len(changedLogLevels) == 0 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 333 | logger.Debug("no-change-in-effective-loglevel-config") |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 334 | return |
| 335 | } |
| 336 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 337 | logger.Debugw("applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 338 | for key, level := range changedLogLevels { |
| 339 | if key == defaultLogLevelKey { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 340 | if l, err := log.StringToLogLevel(level); err == nil { |
| 341 | log.SetDefaultLogLevel(l) |
| 342 | } |
| 343 | } else { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 344 | if l, err := log.StringToLogLevel(level); err == nil { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 345 | log.SetPackageLogLevel(key, l) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // generate md5 hash of key value pairs appended into a single string |
| 352 | // in order by key name |
| 353 | func GenerateLogConfigHash(createHashLog map[string]string) ([16]byte, error) { |
| 354 | createHashLogBytes := []byte{} |
| 355 | levelData, err := json.Marshal(createHashLog) |
| 356 | if err != nil { |
| 357 | return [16]byte{}, err |
| 358 | } |
| 359 | createHashLogBytes = append(createHashLogBytes, levelData...) |
| 360 | return md5.Sum(createHashLogBytes), nil |
| 361 | } |