diff options
author | Marc Abramowitz <marc@marc-abramowitz.com> | 2015-08-26 10:57:26 -0700 |
---|---|---|
committer | Marc Abramowitz <marc@marc-abramowitz.com> | 2015-08-26 10:57:26 -0700 |
commit | 7bdc3cdb15f8f7cd0707fffe57b9c052ca9bb6f2 (patch) | |
tree | ca0d06cb56efeb0895aafd6067dca6e6c7660756 | |
parent | 50448d68e11e644341e349cdfa59174536eebbfb (diff) | |
download | ansible-7bdc3cdb15f8f7cd0707fffe57b9c052ca9bb6f2.tar.gz |
Add optional logging to consul_io inventory script
Configured with environment variables -- e.g.:
ANSIBLE_INVENTORY_CONSUL_IO_LOG_ENABLED=1 ANSIBLE_INVENTORY_CONSUL_IO_LOG_LEVEL=DEBUG /path/to/consul_io.py --list
This gives some verbose logging, including showing all HTTP requests being
made, which I am finding useful, as I am trying to improve the performance of
this script.
-rwxr-xr-x | contrib/inventory/consul_io.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/contrib/inventory/consul_io.py b/contrib/inventory/consul_io.py index 4e40f96873..75f2712fd1 100755 --- a/contrib/inventory/consul_io.py +++ b/contrib/inventory/consul_io.py @@ -129,6 +129,58 @@ import sys import ConfigParser import urllib, urllib2, base64 + +def get_log_filename(): + tty_filename = '/dev/tty' + stdout_filename = '/dev/stdout' + + if not os.path.exists(tty_filename): + return stdout_filename + if not os.access(tty_filename, os.W_OK): + return stdout_filename + if os.getenv('TEAMCITY_VERSION'): + return stdout_filename + + return tty_filename + + +def setup_logging(): + filename = get_log_filename() + + import logging.config + logging.config.dictConfig({ + 'version': 1, + 'formatters': { + 'simple': { + 'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', + }, + }, + 'root': { + 'level': os.getenv('ANSIBLE_INVENTORY_CONSUL_IO_LOG_LEVEL', 'WARN'), + 'handlers': ['console'], + }, + 'handlers': { + 'console': { + 'class': 'logging.FileHandler', + 'filename': filename, + 'formatter': 'simple', + }, + }, + 'loggers': { + 'iso8601': { + 'qualname': 'iso8601', + 'level': 'INFO', + }, + }, + }) + logger = logging.getLogger('consul_io.py') + logger.debug('Invoked with %r', sys.argv) + + +if os.getenv('ANSIBLE_INVENTORY_CONSUL_IO_LOG_ENABLED'): + setup_logging() + + try: import json except ImportError: |