Skip to content

Commit

Permalink
adding support for env variable expansion in log config. see #337
Browse files Browse the repository at this point in the history
Signed-off-by: Syed Nihal <[email protected]>
  • Loading branch information
wasim-nihal committed Mar 25, 2024
1 parent fd64485 commit 686e6db
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,32 @@ If the filename ends with `.url` suffix, the content will be processed as a URL
| `LOG_FORMAT` | Set a log format. (JSON or LOGFMT) | false | `JSON` | string |
| `LOG_TZ` | Set the log timezone. (LOCAL or UTC) | false | `LOCAL` | string |
| `LOG_CONFIG` | Log configuration file path. If not configured, uses the default log config for backward compatibility support. When not configured `LOG_LEVEL, LOG_FORMAT and LOG_TZ` would be used. Refer to [Python logging](https://docs.python.org/3/library/logging.config.html) for log configuration. For sample configuration file refer to file examples/example_logconfig.yaml | false | - | string |

# Environment variable expansion in LOG_CONFIG
Kiwigrid k8s-sidecar supports expansion fo environment variables expansion in the log config.

This comment has been minimized.

Copy link
@chalapat

chalapat Mar 25, 2024

Contributor

expansion fo -> expansion for

This can be done by wrapping the name of environment variable in the regex placeholder `$(<env_var_name>)` in the log config.
At the startup, the k8s container will look for the regex wrapper and replace all the matched occurrences with the content of the environment variables.

For instance the below snippet from the log config,

```commandline
version: 1
disable_existing_loggers: false
root:
level: $(LV_DBG)

This comment has been minimized.

Copy link
@chalapat

chalapat Mar 25, 2024

Contributor

You have to write some tests for the same

handlers: [console]
...
```

would be read as below, replacing the content of `$(LV_DBG)` with the value of environment variable `LV_DBG`.

```commandline
version: 1
disable_existing_loggers: false
root:
level: DEBUG
handlers: [console]
...
```
19 changes: 18 additions & 1 deletion src/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import sys
import re
import yaml
from datetime import datetime
from typing import Optional
Expand Down Expand Up @@ -107,11 +108,27 @@ def add_fields(self, log_record, record, message_dict):
}
}

def expand_env(data):
placeholder_pattern = r'\$\((.*?)\)'

This comment has been minimized.

Copy link
@chalapat

chalapat Mar 25, 2024

Contributor

add {} patter also. r'${([^\s}]+)}'

def replace_placeholder(s):
env = s.group(1)
env_value = os.getenv(env)
if env_value is None:
print(f'unable to expand environment variable {env} in LOG_CONFIG. reason: env variable not set')
sys.exit(1)
else:
return env_value

processed_data = re.sub(placeholder_pattern, replace_placeholder, data)
return yaml.load(processed_data, Loader=yaml.FullLoader)

def get_log_config():
if log_conf_file != "" :
try:
with open(log_conf_file, 'r') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
data = stream.read()
config = expand_env(data)
return config
except FileNotFoundError:
msg = "Config file: "+ log_conf_file + " Not Found"
Expand Down

0 comments on commit 686e6db

Please sign in to comment.