Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python env with dictionary
#1
Hello All,

I want to create an .env file which should contains the dictionary format variables for logging. Later I would like to read those variables through my code.
Below is my
Quote:mymodule.env
file
logging = {
    'module1': {
        'path': '/usr/log/module1.log',
        'level': 'DEBUG'
    },
    'module2': {
        'path': '/usr/log/module2.log',
        'level': 'DEBUG'
    },
    'module3': {
        'path': '/usr/log/module3.log',
        'level': 'DEBUG'
    }
}
And at the same I want this variable to enable and disable on the fly. For example, just by enabling this variable (logging) will be activated and it should start logging.

How to read the above env file through my code and reading, how to enable it on the fly.

Thanks for your support.

Regards,
Maiya
Reply
#2
I am not too sure what you want to do. I don't know what a .env file is. Your logging variable looks like a .json file.

import json
from pathlib import Path

# don't save in /usr/log, I will use my /home/pedro/temp/ folder
path2json = Path('/home/pedro/temp/log_dict.json')
path2log = Path('/home/pedro/temp/log/')

logging = {
    'module1': {
        'path': '/usr/log/module1.log',
        'level': 'DEBUG'
    },
    'module2': {
        'path': '/usr/log/module2.log',
        'level': 'DEBUG'
    },
    'module3': {
        'path': '/usr/log/module3.log',
        'level': 'DEBUG'
    }
}

# save the logging as json
with open(path2json, 'w', encoding='utf-8') as fp:
    json.dump(logging, fp, ensure_ascii=False, indent=4)

# open the data to inspect it
with open(path2json) as fp:
    data_dict = json.load(fp)
    type(data) # returns <class 'dict'> so we have a Python dictionary

# I won't make files in /usr that could cause problems
# create the log files in /home/pedro/temp/log/
for key in data_dict.keys():
    print(key)
    print(f'path = {data_dict[key]["path"]}')
    logfile = Path(data_dict[key]["path"])
    savename = logfile.name
    savepath = path2log / savename
    with open(savepath, 'a') as sp:
        sp.write(f'This is {logfile}')

# make a list of the files now in path2log
file_list = [filename for filename in path2log.iterdir() if filename.is_file()]

for filename in file_list:
    print(filename)
Now I have 3 log files in /home/pedro/temp/log/:

Output:
/home/pedro/temp/log/module1.log /home/pedro/temp/log/module3.log /home/pedro/temp/log/module2.log
Maybe that is something like what you want?
Reply
#3
To create an .env file with logging configurations and dynamically enable or disable logging, you can use the python-dotenv library to load the environment variables and logging.config to configure logging based on a dictionary. First, load your .env file using load_dotenv(). Define logging settings in a dictionary format and read it using os.getenv(). Then, apply the dictionary configuration with logging.config.dictConfig(). To enable or disable logging on the fly, you can check for a specific environment variable and use logging.disable(logging.CRITICAL) to disable it when needed. To get a clearer perspective and access comprehensive details, feel free to explore the link provided below.
https://mycompany.filemail.com/d/hqxztkstpvflier
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 87,407 Apr-27-2019, 03:13 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020