Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active July 8, 2025 13:42
Show Gist options
  • Select an option

  • Save juanpabloaj/3e6a41f683c1767c17824811db01165b to your computer and use it in GitHub Desktop.

Select an option

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, format="%(asctime)s %(message)s")
@mendhak

mendhak commented Mar 19, 2021

Copy link
Copy Markdown

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

@shadowimmage

Copy link
Copy Markdown

just what I needed :)

@edn01

edn01 commented Sep 23, 2021

Copy link
Copy Markdown

πŸ‘

@alete89

alete89 commented Jan 26, 2022

Copy link
Copy Markdown

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

@tompurl

tompurl commented May 18, 2022

Copy link
Copy Markdown

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

@hydrocat

Copy link
Copy Markdown

should we open a PEP ?

@nguaman

nguaman commented Jun 15, 2022

Copy link
Copy Markdown

thanks!!!

@roman4ello

Copy link
Copy Markdown

πŸ‘

@asomov

asomov commented Sep 21, 2022

Copy link
Copy Markdown

thank you !

@hagai-arad

Copy link
Copy Markdown

THANKS!

@recklessop

Copy link
Copy Markdown

THANKS!

@teodoryantcheff

Copy link
Copy Markdown

πŸ‘

@stevleibelt

stevleibelt commented Mar 2, 2023

Copy link
Copy Markdown

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

alete89 commented Mar 2, 2023 via email

Copy link
Copy Markdown

@XDavidT

XDavidT commented Jun 19, 2023

Copy link
Copy Markdown

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