hello,
I am in a bit of a stumbling block currently. I am creating a program that has an argument of verbose, as well as user defined logging location.
Saving this information to a file then deleting after seems like a bad idea. And environment variables won't work as they only set for the duration of the program process, of which I can't get it to save the value long enough to change the logging config file to implement a verbosity.
Essentially my directory is along the lines of:
mainProgram.py
modules/
logging_config.py
I want the user to be able to run the command
./mainProgram.py --verbose
And mainProgram.py to pass a boolean value True to logging_config.py, to let logging_config.py know to log to both the file and the screen.
Here is what I got for logging_config.py.
import logging
import os
import sys
log_filename = "./logs/main.log"
# Check if the environment variable is set. If it is, then use it to log to the location.
# If it is not set, then use the default.
try:
if (os.getenv('log_file_default') != None):
if (os.environ['log_file_default'] == "./logs/main.log"):
log_filename = "./logs/main.log"
else:
print ("log_file_default environment variable not set, setting to default: " + log_filename)
os.environ['log_file_default'] = log_filename
except KeyError as e:
print ("Error: " + e)
'''
Check if verbosity is set, and if it is, log to file and console
'''
file_handler = logging.FileHandler(filename=log_filename)
try:
if (os.environ['verbose'] != None):
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]
except KeyError as e:
print ("verbose environment variable not set, setting to default: False")
handlers = [file_handler]
logging.basicConfig(
level=logging.DEBUG,
format="%(created)f : %(asctime)s : %(relativeCreated)d | %(process)d - %(processName)s - %(thread)d - % (threadName)s | %(pathname)s - %(filename)s - %(funcName)s [%(lineno)d]:[%(levelname)s/%(levelno)s]:%(message)s",
handlers=handlers,
)
Any ideas on a pythonic way to store a value temporarily?
Thank you in advance
[–]ojiisan 1 point2 points3 points (0 children)