Skip to content

Instantly share code, notes, and snippets.

@felixhummel
Created December 7, 2016 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixhummel/a0451e136ac9b21e931e27ba751375bc to your computer and use it in GitHub Desktop.
Save felixhummel/a0451e136ac9b21e931e27ba751375bc to your computer and use it in GitHub Desktop.
simple configurable logging for one-off scripts
#!/usr/bin/env python
# encoding: utf-8
"""
Call me like this::
python log_example.py
To set the log level::
LOGLEVEL=INFO log_example.py
Lowercase is OK too::
LOGLEVEL=debug log_example.py
"""
import logging
import os
try:
loglevel = getattr(logging, os.environ.get('LOGLEVEL', 'WARN').upper())
except AttributeError:
pass
logging.basicConfig(level=loglevel)
log = logging.getLogger(__name__)
if __name__ == '__main__':
log.debug('this is a debug message')
log.info('this is an info message')
log.warn('this is a warn message')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment