Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add environment variables to a python logger ini file configuration?

I want to add a customized log file directory using an environment variable in my file handler through a log.ini file used in a logging.fileConfig() function.

I had tried adding an environment variable in the following:

My logging.ini looks like this:

[loggers]
keys=root

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=fileFormatter,consoleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler, fileHandler
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=consoleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('${LOG_DIRECTORY_ENV_VARIABLE}/logname.log',)

And I configure it using:

from logging.config import fileConfig

fileConfig(f"{BASE_PATH}/resources/logging.ini")

My code results that the directory to be: path/to/file/${LOG_DIRECTORY_ENV_VARIABLE}/logname.log

like image 888
Hesham Kadry Avatar asked Jan 25 '26 20:01

Hesham Kadry


1 Answers

I figured that there's a walk around to my question using an answer on another thread

like image 131
Hesham Kadry Avatar answered Jan 28 '26 20:01

Hesham Kadry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!