Created
December 7, 2016 16:55
-
-
Save felixhummel/a0451e136ac9b21e931e27ba751375bc to your computer and use it in GitHub Desktop.
simple configurable logging for one-off scripts
This file contains 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
#!/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