blob: 8cc4e52318203c03ebd1cd7168c919dac175d5c9 [file] [log] [blame]
divyadesai8bf96862020-02-07 12:24:26 +00001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
divyadesai8bf96862020-02-07 12:24:26 +00003
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 Kumar78cdb4f2020-03-16 13:39:42 +000017// Package Config provides dynamic logging configuration for specific Voltha component with loglevel lookup
Matteo Scandolo2e677482020-04-09 11:56:27 -070018// from etcd kvstore implemented using Backend.
Girish Kumar78cdb4f2020-03-16 13:39:42 +000019// Any Voltha component can start utilizing dynamic logging by starting goroutine of StartLogLevelConfigProcessing after
20// starting kvClient for the component.
divyadesai8bf96862020-02-07 12:24:26 +000021
22package config
23
24import (
25 "context"
26 "crypto/md5"
27 "encoding/json"
28 "errors"
divyadesai8bf96862020-02-07 12:24:26 +000029 "os"
Girish Kumar472a5c92020-04-14 09:14:18 +000030 "sort"
divyadesai8bf96862020-02-07 12:24:26 +000031 "strings"
khenaidoo26721882021-08-11 17:42:52 -040032
33 "github.com/opencord/voltha-lib-go/v7/pkg/log"
divyadesai8bf96862020-02-07 12:24:26 +000034)
35
Girish Kumar21972fb2020-03-13 13:27:47 +000036const (
Girish Kumar472a5c92020-04-14 09:14:18 +000037 defaultLogLevelKey = "default" // kvstore key containing default loglevel
38 globalConfigRootNode = "global" // Root Node in kvstore containing global config
39 initialGlobalDefaultLogLevelValue = "WARN" // Hard-coded Global Default loglevel pushed at PoD startup
40 logPackagesListKey = "log_package_list" // kvstore key containing list of allowed log packages
Girish Kumar21972fb2020-03-13 13:27:47 +000041)
42
divyadesai8bf96862020-02-07 12:24:26 +000043// ComponentLogController represents a Configuration for Logging Config of specific Voltha component type
44// It stores ComponentConfig and GlobalConfig of loglevel config of specific Voltha component type
45// For example,ComponentLogController instance will be created for rw-core component
46type ComponentLogController struct {
47 ComponentName string
48 componentNameConfig *ComponentConfig
49 GlobalConfig *ComponentConfig
50 configManager *ConfigManager
51 logHash [16]byte
Girish Kumar21972fb2020-03-13 13:27:47 +000052 initialLogLevel string // Initial default log level set by helm chart
divyadesai8bf96862020-02-07 12:24:26 +000053}
54
Neha Sharma94f16a92020-06-26 04:17:55 +000055func NewComponentLogController(ctx context.Context, cm *ConfigManager) (*ComponentLogController, error) {
56 logger.Debug(ctx, "creating-new-component-log-controller")
divyadesai8bf96862020-02-07 12:24:26 +000057 componentName := os.Getenv("COMPONENT_NAME")
58 if componentName == "" {
59 return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env")
60 }
61
Girish Kumar21972fb2020-03-13 13:27:47 +000062 var defaultLogLevel string
63 var err error
64 // Retrieve and save default log level; used for fallback if all loglevel config is cleared in etcd
65 if defaultLogLevel, err = log.LogLevelToString(log.GetDefaultLogLevel()); err != nil {
66 defaultLogLevel = "DEBUG"
67 }
68
divyadesai8bf96862020-02-07 12:24:26 +000069 return &ComponentLogController{
70 ComponentName: componentName,
71 componentNameConfig: nil,
72 GlobalConfig: nil,
73 configManager: cm,
Girish Kumar21972fb2020-03-13 13:27:47 +000074 initialLogLevel: defaultLogLevel,
divyadesai8bf96862020-02-07 12:24:26 +000075 }, nil
76
77}
78
Girish Kumar78cdb4f2020-03-16 13:39:42 +000079// StartLogLevelConfigProcessing initialize component config and global config
80// Then, it persists initial default Loglevels into Config Store before
81// starting the loading and processing of all Log Configuration
82func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) {
Neha Sharma94f16a92020-06-26 04:17:55 +000083 cc, err := NewComponentLogController(ctx, cm)
divyadesai8bf96862020-02-07 12:24:26 +000084 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000085 logger.Errorw(ctx, "unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err})
divyadesai8bf96862020-02-07 12:24:26 +000086 return
87 }
88
Girish Kumar21972fb2020-03-13 13:27:47 +000089 cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel)
Neha Sharma94f16a92020-06-26 04:17:55 +000090 logger.Debugw(ctx, "global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig})
divyadesai8bf96862020-02-07 12:24:26 +000091
92 cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel)
Neha Sharma94f16a92020-06-26 04:17:55 +000093 logger.Debugw(ctx, "component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig})
divyadesai8bf96862020-02-07 12:24:26 +000094
Girish Kumar78cdb4f2020-03-16 13:39:42 +000095 cc.persistInitialDefaultLogConfigs(ctx)
96
Girish Kumar472a5c92020-04-14 09:14:18 +000097 cc.persistRegisteredLogPackageList(ctx)
98
divyadesai8bf96862020-02-07 12:24:26 +000099 cc.processLogConfig(ctx)
100}
101
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000102// Method to persist Global default loglevel into etcd, if not set yet
103// It also checks and set Component default loglevel into etcd with initial loglevel set from command line
104func (c *ComponentLogController) persistInitialDefaultLogConfigs(ctx context.Context) {
105
106 _, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
107 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000108 logger.Debugw(ctx, "failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000109
110 err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue)
111 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000112 logger.Errorw(ctx, "failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000113 }
114 }
115
116 _, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey)
117 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000118 logger.Debugw(ctx, "failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000119
120 err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel)
121 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000122 logger.Errorw(ctx, "failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000123 }
124 }
125}
126
Girish Kumar472a5c92020-04-14 09:14:18 +0000127// Method to save list of all registered packages for component into config kvstore. A single string
128// is constructed with comma-separated package names in sorted order and persisted
129func (c *ComponentLogController) persistRegisteredLogPackageList(ctx context.Context) {
130
131 componentMetadataConfig := c.configManager.InitComponentConfig(c.ComponentName, ConfigTypeMetadata)
Neha Sharma94f16a92020-06-26 04:17:55 +0000132 logger.Debugw(ctx, "component-metadata-config", log.Fields{"component-metadata-config": componentMetadataConfig})
Girish Kumar472a5c92020-04-14 09:14:18 +0000133
134 packageList := log.GetPackageNames()
135 packageList = append(packageList, defaultLogLevelKey)
136 sort.Strings(packageList)
137
138 packageNames, err := json.Marshal(packageList)
139 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000140 logger.Errorw(ctx, "failed-to-marshal-log-package-list-for-storage", log.Fields{"error": err, "packageList": packageList})
Girish Kumar472a5c92020-04-14 09:14:18 +0000141 return
142 }
143
144 if err := componentMetadataConfig.Save(ctx, logPackagesListKey, string(packageNames)); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000145 logger.Errorw(ctx, "failed-to-persist-component-registered-log-package-list-at-startup", log.Fields{"error": err, "packageNames": packageNames})
Girish Kumar472a5c92020-04-14 09:14:18 +0000146 }
147}
148
Girish Kumar21972fb2020-03-13 13:27:47 +0000149// ProcessLogConfig will first load and apply log config and then start waiting on component config and global config
Matteo Scandolo2e677482020-04-09 11:56:27 -0700150// channels for any changes. Event channel will be recieved from Backend for valid change type
151// Then data for componentn log config and global log config will be retrieved from Backend and stored in updatedLogConfig in precedence order
divyadesai8bf96862020-02-07 12:24:26 +0000152// If any changes in updatedLogConfig will be applied on component
153func (c *ComponentLogController) processLogConfig(ctx context.Context) {
154
Girish Kumar21972fb2020-03-13 13:27:47 +0000155 // Load and apply Log Config for first time
156 initialLogConfig, err := c.buildUpdatedLogConfig(ctx)
157 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000158 logger.Warnw(ctx, "unable-to-load-log-config-at-startup", log.Fields{"error": err})
Girish Kumar21972fb2020-03-13 13:27:47 +0000159 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000160 if err := c.loadAndApplyLogConfig(ctx, initialLogConfig); err != nil {
161 logger.Warnw(ctx, "unable-to-apply-log-config-at-startup", log.Fields{"error": err})
Girish Kumar21972fb2020-03-13 13:27:47 +0000162 }
163 }
164
divyadesai8bf96862020-02-07 12:24:26 +0000165 componentConfigEventChan := c.componentNameConfig.MonitorForConfigChange(ctx)
166
167 globalConfigEventChan := c.GlobalConfig.MonitorForConfigChange(ctx)
168
169 // process the events for componentName and global config
170 var configEvent *ConfigChangeEvent
171 for {
172 select {
173 case configEvent = <-globalConfigEventChan:
174 case configEvent = <-componentConfigEventChan:
175
176 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000177 logger.Debugw(ctx, "processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute})
divyadesai8bf96862020-02-07 12:24:26 +0000178
179 updatedLogConfig, err := c.buildUpdatedLogConfig(ctx)
180 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000181 logger.Warnw(ctx, "unable-to-fetch-updated-log-config", log.Fields{"error": err})
divyadesai8bf96862020-02-07 12:24:26 +0000182 continue
183 }
184
Neha Sharma94f16a92020-06-26 04:17:55 +0000185 logger.Debugw(ctx, "applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig})
divyadesai8bf96862020-02-07 12:24:26 +0000186
Neha Sharma94f16a92020-06-26 04:17:55 +0000187 if err := c.loadAndApplyLogConfig(ctx, updatedLogConfig); err != nil {
188 logger.Warnw(ctx, "unable-to-load-and-apply-log-config", log.Fields{"error": err})
divyadesai8bf96862020-02-07 12:24:26 +0000189 }
190 }
191
192}
193
194// get active loglevel from the zap logger
Neha Sharma94f16a92020-06-26 04:17:55 +0000195func getActiveLogLevels(ctx context.Context) map[string]string {
Girish Kumar21972fb2020-03-13 13:27:47 +0000196 loglevels := make(map[string]string)
divyadesai8bf96862020-02-07 12:24:26 +0000197
198 // now do the default log level
199 if level, err := log.LogLevelToString(log.GetDefaultLogLevel()); err == nil {
Girish Kumar21972fb2020-03-13 13:27:47 +0000200 loglevels[defaultLogLevelKey] = level
divyadesai8bf96862020-02-07 12:24:26 +0000201 }
202
203 // do the per-package log levels
204 for _, packageName := range log.GetPackageNames() {
205 level, err := log.GetPackageLogLevel(packageName)
206 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000207 logger.Warnw(ctx, "unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err})
Girish Kumar21972fb2020-03-13 13:27:47 +0000208 continue
divyadesai8bf96862020-02-07 12:24:26 +0000209 }
210
divyadesai8bf96862020-02-07 12:24:26 +0000211 if l, err := log.LogLevelToString(level); err == nil {
Girish Kumar21972fb2020-03-13 13:27:47 +0000212 loglevels[packageName] = l
divyadesai8bf96862020-02-07 12:24:26 +0000213 }
divyadesai8bf96862020-02-07 12:24:26 +0000214 }
divyadesai8bf96862020-02-07 12:24:26 +0000215
Neha Sharma94f16a92020-06-26 04:17:55 +0000216 logger.Debugw(ctx, "retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels})
Girish Kumar21972fb2020-03-13 13:27:47 +0000217
218 return loglevels
divyadesai8bf96862020-02-07 12:24:26 +0000219}
220
221func (c *ComponentLogController) getGlobalLogConfig(ctx context.Context) (string, error) {
222
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000223 globalDefaultLogLevel, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
divyadesai8bf96862020-02-07 12:24:26 +0000224 if err != nil {
225 return "", err
226 }
227
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000228 // Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value
229 // We should use hard-coded initial default value in such cases
230 if globalDefaultLogLevel == "" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000231 logger.Warn(ctx, "global-default-loglevel-not-found-in-config-store")
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000232 globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
divyadesai8bf96862020-02-07 12:24:26 +0000233 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000234
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000235 if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000236 logger.Warnw(ctx, "unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000237 globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
238 }
239
Neha Sharma94f16a92020-06-26 04:17:55 +0000240 logger.Debugw(ctx, "retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel})
divyadesai8bf96862020-02-07 12:24:26 +0000241
242 return globalDefaultLogLevel, nil
243}
244
Girish Kumar21972fb2020-03-13 13:27:47 +0000245func (c *ComponentLogController) getComponentLogConfig(ctx context.Context, globalDefaultLogLevel string) (map[string]string, error) {
divyadesai8bf96862020-02-07 12:24:26 +0000246 componentLogConfig, err := c.componentNameConfig.RetrieveAll(ctx)
247 if err != nil {
248 return nil, err
249 }
250
Girish Kumar21972fb2020-03-13 13:27:47 +0000251 effectiveDefaultLogLevel := ""
252 for logConfigKey, logConfigValue := range componentLogConfig {
253 if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000254 logger.Warnw(ctx, "unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue})
Girish Kumar21972fb2020-03-13 13:27:47 +0000255 delete(componentLogConfig, logConfigKey)
divyadesai8bf96862020-02-07 12:24:26 +0000256 } else {
Girish Kumar21972fb2020-03-13 13:27:47 +0000257 if logConfigKey == defaultLogLevelKey {
258 effectiveDefaultLogLevel = componentLogConfig[defaultLogLevelKey]
divyadesai8bf96862020-02-07 12:24:26 +0000259 }
260 }
261 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000262
263 // if default loglevel is not configured for the component, component should use
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000264 // default loglevel configured at global level
Girish Kumar21972fb2020-03-13 13:27:47 +0000265 if effectiveDefaultLogLevel == "" {
266 effectiveDefaultLogLevel = globalDefaultLogLevel
divyadesai8bf96862020-02-07 12:24:26 +0000267 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000268
269 componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel
270
Neha Sharma94f16a92020-06-26 04:17:55 +0000271 logger.Debugw(ctx, "retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig})
divyadesai8bf96862020-02-07 12:24:26 +0000272
273 return componentLogConfig, nil
274}
275
Matteo Scandolo2e677482020-04-09 11:56:27 -0700276// buildUpdatedLogConfig retrieve the global logConfig and component logConfig from Backend
divyadesai8bf96862020-02-07 12:24:26 +0000277// component logConfig stores the log config with precedence order
278// For example, If the global logConfig is set and component logConfig is set only for specific package then
279// component logConfig is stored with global logConfig and component logConfig of specific package
280// For example, If the global logConfig is set and component logConfig is set for specific package and as well as for default then
281// component logConfig is stored with component logConfig data only
282func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) {
283 globalLogLevel, err := c.getGlobalLogConfig(ctx)
284 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000285 logger.Errorw(ctx, "unable-to-retrieve-global-log-config", log.Fields{"err": err})
divyadesai8bf96862020-02-07 12:24:26 +0000286 }
287
Girish Kumar21972fb2020-03-13 13:27:47 +0000288 componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel)
divyadesai8bf96862020-02-07 12:24:26 +0000289 if err != nil {
290 return nil, err
291 }
292
Girish Kumar21972fb2020-03-13 13:27:47 +0000293 finalLogConfig := make(map[string]string)
294 for packageName, logLevel := range componentLogConfig {
295 finalLogConfig[strings.ReplaceAll(packageName, "#", "/")] = logLevel
296 }
297
298 return finalLogConfig, nil
divyadesai8bf96862020-02-07 12:24:26 +0000299}
300
301// load and apply the current configuration for component name
302// create hash of loaded configuration using GenerateLogConfigHash
303// if there is previous hash stored, compare the hash to stored hash
304// if there is any change will call UpdateLogLevels
Neha Sharma94f16a92020-06-26 04:17:55 +0000305func (c *ComponentLogController) loadAndApplyLogConfig(ctx context.Context, logConfig map[string]string) error {
divyadesai8bf96862020-02-07 12:24:26 +0000306 currentLogHash, err := GenerateLogConfigHash(logConfig)
307 if err != nil {
308 return err
309 }
310
divyadesai8bf96862020-02-07 12:24:26 +0000311 if c.logHash != currentLogHash {
Neha Sharma94f16a92020-06-26 04:17:55 +0000312 updateLogLevels(ctx, logConfig)
divyadesai8bf96862020-02-07 12:24:26 +0000313 c.logHash = currentLogHash
Girish Kumar21972fb2020-03-13 13:27:47 +0000314 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000315 logger.Debug(ctx, "effective-loglevel-config-same-as-currently-active")
divyadesai8bf96862020-02-07 12:24:26 +0000316 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000317
divyadesai8bf96862020-02-07 12:24:26 +0000318 return nil
319}
320
Girish Kumar21972fb2020-03-13 13:27:47 +0000321// createModifiedLogLevels loops through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig
322// to identify and create map of modified Log Levels of 2 types:
323// - Packages for which log level has been changed
324// - Packages for which log level config has been cleared - set to default log level
Neha Sharma94f16a92020-06-26 04:17:55 +0000325func createModifiedLogLevels(ctx context.Context, activeLogLevels, updatedLogLevels map[string]string) map[string]string {
Girish Kumar21972fb2020-03-13 13:27:47 +0000326 defaultLevel := updatedLogLevels[defaultLogLevelKey]
divyadesai8bf96862020-02-07 12:24:26 +0000327
Girish Kumar21972fb2020-03-13 13:27:47 +0000328 modifiedLogLevels := make(map[string]string)
divyadesai8bf96862020-02-07 12:24:26 +0000329 for activeKey, activeLevel := range activeLogLevels {
330 if _, exist := updatedLogLevels[activeKey]; !exist {
Girish Kumar21972fb2020-03-13 13:27:47 +0000331 if activeLevel != defaultLevel {
332 modifiedLogLevels[activeKey] = defaultLevel
divyadesai8bf96862020-02-07 12:24:26 +0000333 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000334 } else if activeLevel != updatedLogLevels[activeKey] {
335 modifiedLogLevels[activeKey] = updatedLogLevels[activeKey]
divyadesai8bf96862020-02-07 12:24:26 +0000336 }
337 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000338
339 // Log warnings for all invalid packages for which log config has been set
340 for key, value := range updatedLogLevels {
341 if _, exist := activeLogLevels[key]; !exist {
Neha Sharma94f16a92020-06-26 04:17:55 +0000342 logger.Warnw(ctx, "ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value})
Girish Kumar21972fb2020-03-13 13:27:47 +0000343 }
344 }
345
346 return modifiedLogLevels
divyadesai8bf96862020-02-07 12:24:26 +0000347}
348
349// updateLogLevels update the loglevels for the component
350// retrieve active confguration from logger
351// compare with entries one by one and apply
Neha Sharma94f16a92020-06-26 04:17:55 +0000352func updateLogLevels(ctx context.Context, updatedLogConfig map[string]string) {
divyadesai8bf96862020-02-07 12:24:26 +0000353
Neha Sharma94f16a92020-06-26 04:17:55 +0000354 activeLogLevels := getActiveLogLevels(ctx)
355 changedLogLevels := createModifiedLogLevels(ctx, activeLogLevels, updatedLogConfig)
Girish Kumar21972fb2020-03-13 13:27:47 +0000356
357 // If no changed log levels are found, just return. It may happen on configuration of a invalid package
358 if len(changedLogLevels) == 0 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000359 logger.Debug(ctx, "no-change-in-effective-loglevel-config")
Girish Kumar21972fb2020-03-13 13:27:47 +0000360 return
361 }
362
Neha Sharma94f16a92020-06-26 04:17:55 +0000363 logger.Debugw(ctx, "applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels})
Girish Kumar21972fb2020-03-13 13:27:47 +0000364 for key, level := range changedLogLevels {
365 if key == defaultLogLevelKey {
divyadesai8bf96862020-02-07 12:24:26 +0000366 if l, err := log.StringToLogLevel(level); err == nil {
367 log.SetDefaultLogLevel(l)
368 }
369 } else {
divyadesai8bf96862020-02-07 12:24:26 +0000370 if l, err := log.StringToLogLevel(level); err == nil {
Girish Kumar21972fb2020-03-13 13:27:47 +0000371 log.SetPackageLogLevel(key, l)
divyadesai8bf96862020-02-07 12:24:26 +0000372 }
373 }
374 }
divyadesai8bf96862020-02-07 12:24:26 +0000375}
376
377// generate md5 hash of key value pairs appended into a single string
378// in order by key name
379func GenerateLogConfigHash(createHashLog map[string]string) ([16]byte, error) {
380 createHashLogBytes := []byte{}
381 levelData, err := json.Marshal(createHashLog)
382 if err != nil {
383 return [16]byte{}, err
384 }
385 createHashLogBytes = append(createHashLogBytes, levelData...)
386 return md5.Sum(createHashLogBytes), nil
387}