Last active
February 28, 2025 10:14
-
-
Save juanpabloaj/3e6a41f683c1767c17824811db01165b to your computer and use it in GitHub Desktop.
python logging, log level with environment variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import logging | |
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper() | |
logging.basicConfig(level=LOGLEVEL, format="%(asctime)s %(message)s") |
Have you tried `setLevel`?
https://docs.python.org/3/library/logging.html#logging.Logger.setLevel
…On Thu, Mar 2, 2023, 6:05 AM Stev Leibelt ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Damn, it doesn't work but the chance is high that I made the issue on
myself.
import logging
logging.config.fileConfig(fname='logging.conf')logging.basicConfig(level=logging.INFO)
logging.debug('debug logging test')logging.info('info logging test')logging.info('error logging test')
In my logging.conf, I've defined the loglevel with DEBUG.
I hoped that I can configure the loglevel with debug in the logging.conf
*but* configure the real applied log level via an .env file to keep the
"moving parts" in one configuration file.
Sadly my concept with python 3.11 does not work. I still get the debug
logging message.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/3e6a41f683c1767c17824811db01165b#gistcomment-4489082>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGEQ3IPNI24RDDSBUDESN3W2BPFPBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4TEMRWGQZTANNHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Issue
Initial Question
Damn, it doesn't work but the chance is high that I made the issue on myself.
import logging logging.config.fileConfig(fname='logging.conf') logging.basicConfig(level=logging.INFO) logging.debug('debug logging test') logging.info('info logging test') logging.info('error logging test')In my
logging.conf
, I've defined the loglevel with DEBUG.I hoped that I can configure the loglevel with debug in the logging.conf but configure the real applied log level via an
.env
file to keep the "moving parts" in one configuration file.Sadly my concept with python 3.11 does not work. I still get the debug logging message.
Update - 20230302T10:08:30
Following LOC is doing the trick for me.
# replace this line logging.basicConfig(level=logging.INFO) # with that line logging.getLogger().setLevel(level=os.getenv('MY_LOGGER_LEVEL', 'INFO').upper())
I like the usage of environment variables with default values. Very useful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issue
Initial Question
Damn, it doesn't work but the chance is high that I made the issue on myself.
In my
logging.conf
, I've defined the loglevel with DEBUG.I hoped that I can configure the loglevel with debug in the logging.conf but configure the real applied log level via an
.env
file to keep the "moving parts" in one configuration file.Sadly my concept with python 3.11 does not work. I still get the debug logging message.
Update - 20230302T10:08:30
Following LOC is doing the trick for me.