blob: 68bfb3295d3e0da80ac0f5b2425b8f4a373ab4d8 [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +00001/*
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
divyadesai52dc0882020-03-19 06:38:11 +000017// Package Config provides dynamic logging configuration for specific Voltha component with loglevel lookup
Rohan Agrawal00d3a412020-04-22 10:51:39 +000018// from etcd kvstore implemented using Backend.
divyadesai52dc0882020-03-19 06:38:11 +000019// Any Voltha component can start utilizing dynamic logging by starting goroutine of StartLogLevelConfigProcessing after
20// starting kvClient for the component.
divyadesai81bb7ba2020-03-11 11:45:23 +000021
22package config
23
24import (
25 "context"
26 "crypto/md5"
27 "encoding/json"
28 "errors"
Andrea Campanella18448bc2021-07-08 18:47:22 +020029 "github.com/opencord/voltha-lib-go/v5/pkg/log"
divyadesai81bb7ba2020-03-11 11:45:23 +000030 "os"
Rohan Agrawal00d3a412020-04-22 10:51:39 +000031 "sort"
divyadesai81bb7ba2020-03-11 11:45:23 +000032 "strings"
33)
34
divyadesai52dc0882020-03-19 06:38:11 +000035const (
Rohan Agrawal00d3a412020-04-22 10:51:39 +000036 defaultLogLevelKey = "default" // kvstore key containing default loglevel
37 globalConfigRootNode = "global" // Root Node in kvstore containing global config
38 initialGlobalDefaultLogLevelValue = "WARN" // Hard-coded Global Default loglevel pushed at PoD startup
39 logPackagesListKey = "log_package_list" // kvstore key containing list of allowed log packages
divyadesai52dc0882020-03-19 06:38:11 +000040)
41
divyadesai81bb7ba2020-03-11 11:45:23 +000042// ComponentLogController represents a Configuration for Logging Config of specific Voltha component type
43// It stores ComponentConfig and GlobalConfig of loglevel config of specific Voltha component type
44// For example,ComponentLogController instance will be created for rw-core component
45type ComponentLogController struct {
46 ComponentName string
47 componentNameConfig *ComponentConfig
48 GlobalConfig *ComponentConfig
49 configManager *ConfigManager
50 logHash [16]byte
divyadesai52dc0882020-03-19 06:38:11 +000051 initialLogLevel string // Initial default log level set by helm chart
divyadesai81bb7ba2020-03-11 11:45:23 +000052}
53
Rohan Agrawalc32d9932020-06-15 11:01:47 +000054func NewComponentLogController(ctx context.Context, cm *ConfigManager) (*ComponentLogController, error) {
55 logger.Debug(ctx, "creating-new-component-log-controller")
divyadesai81bb7ba2020-03-11 11:45:23 +000056 componentName := os.Getenv("COMPONENT_NAME")
57 if componentName == "" {
58 return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env")
59 }
60
divyadesai52dc0882020-03-19 06:38:11 +000061 var defaultLogLevel string
62 var err error
63 // Retrieve and save default log level; used for fallback if all loglevel config is cleared in etcd
64 if defaultLogLevel, err = log.LogLevelToString(log.GetDefaultLogLevel()); err != nil {
65 defaultLogLevel = "DEBUG"
66 }
67
divyadesai81bb7ba2020-03-11 11:45:23 +000068 return &ComponentLogController{
69 ComponentName: componentName,
70 componentNameConfig: nil,
71 GlobalConfig: nil,
72 configManager: cm,
divyadesai52dc0882020-03-19 06:38:11 +000073 initialLogLevel: defaultLogLevel,
divyadesai81bb7ba2020-03-11 11:45:23 +000074 }, nil
75
76}
77
divyadesai52dc0882020-03-19 06:38:11 +000078// StartLogLevelConfigProcessing initialize component config and global config
79// Then, it persists initial default Loglevels into Config Store before
80// starting the loading and processing of all Log Configuration
81func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000082 cc, err := NewComponentLogController(ctx, cm)
divyadesai81bb7ba2020-03-11 11:45:23 +000083 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000084 logger.Errorw(ctx, "unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000085 return
86 }
87
divyadesai52dc0882020-03-19 06:38:11 +000088 cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000089 logger.Debugw(ctx, "global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig})
divyadesai81bb7ba2020-03-11 11:45:23 +000090
91 cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000092 logger.Debugw(ctx, "component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig})
divyadesai81bb7ba2020-03-11 11:45:23 +000093
divyadesai52dc0882020-03-19 06:38:11 +000094 cc.persistInitialDefaultLogConfigs(ctx)
95
Rohan Agrawal00d3a412020-04-22 10:51:39 +000096 cc.persistRegisteredLogPackageList(ctx)
97
divyadesai81bb7ba2020-03-11 11:45:23 +000098 cc.processLogConfig(ctx)
99}
100
divyadesai52dc0882020-03-19 06:38:11 +0000101// Method to persist Global default loglevel into etcd, if not set yet
102// It also checks and set Component default loglevel into etcd with initial loglevel set from command line
103func (c *ComponentLogController) persistInitialDefaultLogConfigs(ctx context.Context) {
104
105 _, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
106 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000107 logger.Debugw(ctx, "failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err})
divyadesai52dc0882020-03-19 06:38:11 +0000108
109 err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue)
110 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000111 logger.Errorw(ctx, "failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue})
divyadesai52dc0882020-03-19 06:38:11 +0000112 }
113 }
114
115 _, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey)
116 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000117 logger.Debugw(ctx, "failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err})
divyadesai52dc0882020-03-19 06:38:11 +0000118
119 err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel)
120 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000121 logger.Errorw(ctx, "failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel})
divyadesai52dc0882020-03-19 06:38:11 +0000122 }
123 }
124}
125
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000126// Method to save list of all registered packages for component into config kvstore. A single string
127// is constructed with comma-separated package names in sorted order and persisted
128func (c *ComponentLogController) persistRegisteredLogPackageList(ctx context.Context) {
129
130 componentMetadataConfig := c.configManager.InitComponentConfig(c.ComponentName, ConfigTypeMetadata)
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000131 logger.Debugw(ctx, "component-metadata-config", log.Fields{"component-metadata-config": componentMetadataConfig})
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000132
133 packageList := log.GetPackageNames()
134 packageList = append(packageList, defaultLogLevelKey)
135 sort.Strings(packageList)
136
137 packageNames, err := json.Marshal(packageList)
138 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000139 logger.Errorw(ctx, "failed-to-marshal-log-package-list-for-storage", log.Fields{"error": err, "packageList": packageList})
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000140 return
141 }
142
143 if err := componentMetadataConfig.Save(ctx, logPackagesListKey, string(packageNames)); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000144 logger.Errorw(ctx, "failed-to-persist-component-registered-log-package-list-at-startup", log.Fields{"error": err, "packageNames": packageNames})
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000145 }
146}
147
divyadesai52dc0882020-03-19 06:38:11 +0000148// ProcessLogConfig will first load and apply log config and then start waiting on component config and global config
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000149// channels for any changes. Event channel will be recieved from Backend for valid change type
150// Then data for componentn log config and global log config will be retrieved from Backend and stored in updatedLogConfig in precedence order
divyadesai81bb7ba2020-03-11 11:45:23 +0000151// If any changes in updatedLogConfig will be applied on component
152func (c *ComponentLogController) processLogConfig(ctx context.Context) {
153
divyadesai52dc0882020-03-19 06:38:11 +0000154 // Load and apply Log Config for first time
155 initialLogConfig, err := c.buildUpdatedLogConfig(ctx)
156 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000157 logger.Warnw(ctx, "unable-to-load-log-config-at-startup", log.Fields{"error": err})
divyadesai52dc0882020-03-19 06:38:11 +0000158 } else {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000159 if err := c.loadAndApplyLogConfig(ctx, initialLogConfig); err != nil {
160 logger.Warnw(ctx, "unable-to-apply-log-config-at-startup", log.Fields{"error": err})
divyadesai52dc0882020-03-19 06:38:11 +0000161 }
162 }
163
divyadesai81bb7ba2020-03-11 11:45:23 +0000164 componentConfigEventChan := c.componentNameConfig.MonitorForConfigChange(ctx)
165
166 globalConfigEventChan := c.GlobalConfig.MonitorForConfigChange(ctx)
167
168 // process the events for componentName and global config
169 var configEvent *ConfigChangeEvent
170 for {
171 select {
172 case configEvent = <-globalConfigEventChan:
173 case configEvent = <-componentConfigEventChan:
174
175 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000176 logger.Debugw(ctx, "processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute})
divyadesai81bb7ba2020-03-11 11:45:23 +0000177
178 updatedLogConfig, err := c.buildUpdatedLogConfig(ctx)
179 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000180 logger.Warnw(ctx, "unable-to-fetch-updated-log-config", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000181 continue
182 }
183
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000184 logger.Debugw(ctx, "applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig})
divyadesai81bb7ba2020-03-11 11:45:23 +0000185
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000186 if err := c.loadAndApplyLogConfig(ctx, updatedLogConfig); err != nil {
187 logger.Warnw(ctx, "unable-to-load-and-apply-log-config", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000188 }
189 }
190
191}
192
193// get active loglevel from the zap logger
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000194func getActiveLogLevels(ctx context.Context) map[string]string {
divyadesai52dc0882020-03-19 06:38:11 +0000195 loglevels := make(map[string]string)
divyadesai81bb7ba2020-03-11 11:45:23 +0000196
197 // now do the default log level
198 if level, err := log.LogLevelToString(log.GetDefaultLogLevel()); err == nil {
divyadesai52dc0882020-03-19 06:38:11 +0000199 loglevels[defaultLogLevelKey] = level
divyadesai81bb7ba2020-03-11 11:45:23 +0000200 }
201
202 // do the per-package log levels
203 for _, packageName := range log.GetPackageNames() {
204 level, err := log.GetPackageLogLevel(packageName)
205 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000206 logger.Warnw(ctx, "unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err})
divyadesai52dc0882020-03-19 06:38:11 +0000207 continue
divyadesai81bb7ba2020-03-11 11:45:23 +0000208 }
209
divyadesai81bb7ba2020-03-11 11:45:23 +0000210 if l, err := log.LogLevelToString(level); err == nil {
divyadesai52dc0882020-03-19 06:38:11 +0000211 loglevels[packageName] = l
divyadesai81bb7ba2020-03-11 11:45:23 +0000212 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000213 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000214
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000215 logger.Debugw(ctx, "retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels})
divyadesai52dc0882020-03-19 06:38:11 +0000216
217 return loglevels
divyadesai81bb7ba2020-03-11 11:45:23 +0000218}
219
220func (c *ComponentLogController) getGlobalLogConfig(ctx context.Context) (string, error) {
221
divyadesai52dc0882020-03-19 06:38:11 +0000222 globalDefaultLogLevel, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
divyadesai81bb7ba2020-03-11 11:45:23 +0000223 if err != nil {
224 return "", err
225 }
226
divyadesai52dc0882020-03-19 06:38:11 +0000227 // Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value
228 // We should use hard-coded initial default value in such cases
229 if globalDefaultLogLevel == "" {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000230 logger.Warn(ctx, "global-default-loglevel-not-found-in-config-store")
divyadesai52dc0882020-03-19 06:38:11 +0000231 globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
divyadesai81bb7ba2020-03-11 11:45:23 +0000232 }
divyadesai52dc0882020-03-19 06:38:11 +0000233
234 if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000235 logger.Warnw(ctx, "unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel})
divyadesai52dc0882020-03-19 06:38:11 +0000236 globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
237 }
238
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000239 logger.Debugw(ctx, "retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel})
divyadesai81bb7ba2020-03-11 11:45:23 +0000240
241 return globalDefaultLogLevel, nil
242}
243
divyadesai52dc0882020-03-19 06:38:11 +0000244func (c *ComponentLogController) getComponentLogConfig(ctx context.Context, globalDefaultLogLevel string) (map[string]string, error) {
divyadesai81bb7ba2020-03-11 11:45:23 +0000245 componentLogConfig, err := c.componentNameConfig.RetrieveAll(ctx)
246 if err != nil {
247 return nil, err
248 }
249
divyadesai52dc0882020-03-19 06:38:11 +0000250 effectiveDefaultLogLevel := ""
251 for logConfigKey, logConfigValue := range componentLogConfig {
252 if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000253 logger.Warnw(ctx, "unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue})
divyadesai52dc0882020-03-19 06:38:11 +0000254 delete(componentLogConfig, logConfigKey)
divyadesai81bb7ba2020-03-11 11:45:23 +0000255 } else {
divyadesai52dc0882020-03-19 06:38:11 +0000256 if logConfigKey == defaultLogLevelKey {
257 effectiveDefaultLogLevel = componentLogConfig[defaultLogLevelKey]
divyadesai81bb7ba2020-03-11 11:45:23 +0000258 }
259 }
260 }
divyadesai52dc0882020-03-19 06:38:11 +0000261
262 // if default loglevel is not configured for the component, component should use
263 // default loglevel configured at global level
264 if effectiveDefaultLogLevel == "" {
265 effectiveDefaultLogLevel = globalDefaultLogLevel
divyadesai81bb7ba2020-03-11 11:45:23 +0000266 }
divyadesai52dc0882020-03-19 06:38:11 +0000267
268 componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel
269
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000270 logger.Debugw(ctx, "retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig})
divyadesai81bb7ba2020-03-11 11:45:23 +0000271
272 return componentLogConfig, nil
273}
274
Rohan Agrawal00d3a412020-04-22 10:51:39 +0000275// buildUpdatedLogConfig retrieve the global logConfig and component logConfig from Backend
divyadesai81bb7ba2020-03-11 11:45:23 +0000276// component logConfig stores the log config with precedence order
277// For example, If the global logConfig is set and component logConfig is set only for specific package then
278// component logConfig is stored with global logConfig and component logConfig of specific package
279// For example, If the global logConfig is set and component logConfig is set for specific package and as well as for default then
280// component logConfig is stored with component logConfig data only
281func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) {
282 globalLogLevel, err := c.getGlobalLogConfig(ctx)
283 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000284 logger.Errorw(ctx, "unable-to-retrieve-global-log-config", log.Fields{"err": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000285 }
286
divyadesai52dc0882020-03-19 06:38:11 +0000287 componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel)
divyadesai81bb7ba2020-03-11 11:45:23 +0000288 if err != nil {
289 return nil, err
290 }
291
divyadesai52dc0882020-03-19 06:38:11 +0000292 finalLogConfig := make(map[string]string)
293 for packageName, logLevel := range componentLogConfig {
294 finalLogConfig[strings.ReplaceAll(packageName, "#", "/")] = logLevel
295 }
296
297 return finalLogConfig, nil
divyadesai81bb7ba2020-03-11 11:45:23 +0000298}
299
300// load and apply the current configuration for component name
301// create hash of loaded configuration using GenerateLogConfigHash
302// if there is previous hash stored, compare the hash to stored hash
303// if there is any change will call UpdateLogLevels
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000304func (c *ComponentLogController) loadAndApplyLogConfig(ctx context.Context, logConfig map[string]string) error {
divyadesai81bb7ba2020-03-11 11:45:23 +0000305 currentLogHash, err := GenerateLogConfigHash(logConfig)
306 if err != nil {
307 return err
308 }
309
divyadesai81bb7ba2020-03-11 11:45:23 +0000310 if c.logHash != currentLogHash {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000311 updateLogLevels(ctx, logConfig)
divyadesai81bb7ba2020-03-11 11:45:23 +0000312 c.logHash = currentLogHash
divyadesai52dc0882020-03-19 06:38:11 +0000313 } else {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000314 logger.Debug(ctx, "effective-loglevel-config-same-as-currently-active")
divyadesai81bb7ba2020-03-11 11:45:23 +0000315 }
divyadesai52dc0882020-03-19 06:38:11 +0000316
divyadesai81bb7ba2020-03-11 11:45:23 +0000317 return nil
318}
319
divyadesai52dc0882020-03-19 06:38:11 +0000320// createModifiedLogLevels loops through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig
321// to identify and create map of modified Log Levels of 2 types:
322// - Packages for which log level has been changed
323// - Packages for which log level config has been cleared - set to default log level
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000324func createModifiedLogLevels(ctx context.Context, activeLogLevels, updatedLogLevels map[string]string) map[string]string {
divyadesai52dc0882020-03-19 06:38:11 +0000325 defaultLevel := updatedLogLevels[defaultLogLevelKey]
divyadesai81bb7ba2020-03-11 11:45:23 +0000326
divyadesai52dc0882020-03-19 06:38:11 +0000327 modifiedLogLevels := make(map[string]string)
divyadesai81bb7ba2020-03-11 11:45:23 +0000328 for activeKey, activeLevel := range activeLogLevels {
329 if _, exist := updatedLogLevels[activeKey]; !exist {
divyadesai52dc0882020-03-19 06:38:11 +0000330 if activeLevel != defaultLevel {
331 modifiedLogLevels[activeKey] = defaultLevel
divyadesai81bb7ba2020-03-11 11:45:23 +0000332 }
divyadesai52dc0882020-03-19 06:38:11 +0000333 } else if activeLevel != updatedLogLevels[activeKey] {
334 modifiedLogLevels[activeKey] = updatedLogLevels[activeKey]
divyadesai81bb7ba2020-03-11 11:45:23 +0000335 }
336 }
divyadesai52dc0882020-03-19 06:38:11 +0000337
338 // Log warnings for all invalid packages for which log config has been set
339 for key, value := range updatedLogLevels {
340 if _, exist := activeLogLevels[key]; !exist {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000341 logger.Warnw(ctx, "ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value})
divyadesai52dc0882020-03-19 06:38:11 +0000342 }
343 }
344
345 return modifiedLogLevels
divyadesai81bb7ba2020-03-11 11:45:23 +0000346}
347
348// updateLogLevels update the loglevels for the component
349// retrieve active confguration from logger
350// compare with entries one by one and apply
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000351func updateLogLevels(ctx context.Context, updatedLogConfig map[string]string) {
divyadesai81bb7ba2020-03-11 11:45:23 +0000352
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000353 activeLogLevels := getActiveLogLevels(ctx)
354 changedLogLevels := createModifiedLogLevels(ctx, activeLogLevels, updatedLogConfig)
divyadesai52dc0882020-03-19 06:38:11 +0000355
356 // If no changed log levels are found, just return. It may happen on configuration of a invalid package
357 if len(changedLogLevels) == 0 {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000358 logger.Debug(ctx, "no-change-in-effective-loglevel-config")
divyadesai52dc0882020-03-19 06:38:11 +0000359 return
360 }
361
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000362 logger.Debugw(ctx, "applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels})
divyadesai52dc0882020-03-19 06:38:11 +0000363 for key, level := range changedLogLevels {
364 if key == defaultLogLevelKey {
divyadesai81bb7ba2020-03-11 11:45:23 +0000365 if l, err := log.StringToLogLevel(level); err == nil {
366 log.SetDefaultLogLevel(l)
367 }
368 } else {
divyadesai81bb7ba2020-03-11 11:45:23 +0000369 if l, err := log.StringToLogLevel(level); err == nil {
divyadesai52dc0882020-03-19 06:38:11 +0000370 log.SetPackageLogLevel(key, l)
divyadesai81bb7ba2020-03-11 11:45:23 +0000371 }
372 }
373 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000374}
375
376// generate md5 hash of key value pairs appended into a single string
377// in order by key name
378func GenerateLogConfigHash(createHashLog map[string]string) ([16]byte, error) {
379 createHashLogBytes := []byte{}
380 levelData, err := json.Marshal(createHashLog)
381 if err != nil {
382 return [16]byte{}, err
383 }
384 createHashLogBytes = append(createHashLogBytes, levelData...)
385 return md5.Sum(createHashLogBytes), nil
386}