Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active March 1, 2024 13:09
Show Gist options
  • Save juanpabloaj/3e6a41f683c1767c17824811db01165b to your computer and use it in GitHub Desktop.
Save juanpabloaj/3e6a41f683c1767c17824811db01165b to your computer and use it in GitHub Desktop.
python logging, log level with environment variable
import os
import logging
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
logging.basicConfig(level=LOGLEVEL)
@mendhak
Copy link

mendhak commented Mar 19, 2021

Thanks, I was using LOGLEVEL.INFO (an int) and hadn't realized you could just pass a string.

@shadowimmage
Copy link

just what I needed :)

@edn01
Copy link

edn01 commented Sep 23, 2021

👍

@alete89
Copy link

alete89 commented Jan 26, 2022

I think this should be the default behavior of the package logging. It should support setting the LOGLEVEL through env-var natively IMO.

@tompurl
Copy link

tompurl commented May 18, 2022

Thank you for this. It is surprisingly difficult to find.

@hydrocat
Copy link

should we open a PEP ?

@nguaman
Copy link

nguaman commented Jun 15, 2022

thanks!!!

@roman4ello
Copy link

👍

@asomov
Copy link

asomov commented Sep 21, 2022

thank you !

@hagai-arad
Copy link

THANKS!

@recklessop
Copy link

THANKS!

@teodoryantcheff
Copy link

👍

@stevleibelt
Copy link

stevleibelt commented Mar 2, 2023

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())

@alete89
Copy link

alete89 commented Mar 2, 2023 via email

@XDavidT
Copy link

XDavidT commented Jun 19, 2023

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