# Python API with log\_path default set redirects unrelated logs

**URL:** <https://forum.ansible.com/t/python-api-with-log-path-default-set-redirects-unrelated-logs/17290>\
**Category:** Ansible Project\
**Created:** [January 16, 2015, 5:26am UTC](https://forum.ansible.com/t/python-api-with-log-path-default-set-redirects-unrelated-logs/17290 "2015-01-16T05:26:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Pettett](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@Jason\_Pettett](https://forum.ansible.com/u/Jason_Pettett)\
**Post date:** [January 16, 2015, 5:26am UTC](https://forum.ansible.com/t/python-api-with-log-path-default-set-redirects-unrelated-logs/17290/1 "2015-01-16T05:26:41Z")

</div>

Hello all,

I’m running some Ansible commands from within a flask application via the [Python API](http://docs.ansible.com/developing_api.html).

Ansible version: 1.8.2  
Python version: 2.7

In my home directory, I have .ansible.cfg with the following contents:

> [defaults]  
> host\_key\_checking = False  
> log\_path=/tmp/ansible.log

The issue I’m having is that as soon as the runner is imported (see attached stack trace later in this post), the log\_path defined above overrides the current logging location (in this case, stdout).

I’ve tracked it down to the following line:

> logging.basicConfig(filename=path, level=logging.DEBUG, format=‘%(asctime)s %(name)s %(message)s’)

Line 40 in _site-packages/ansible/callbacks.py_

Note that it also changes the format and minimum logging level which is problematic.

Is there currently an undocumented/unofficial way to retain my default log\_path, while avoiding this issue (when using the Python API)?

Traceback of where it is imported and modified:

> File “/home/dev3/projects/tcg-cas/app/util.py”, line 5, in

> from ansible.runner import Runner

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 37, in

> import ansible.inventory

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/ **init**.py”, line 26, in

> from ansible.inventory.ini import InventoryParser

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/ini.py”, line 21, in

> from ansible.inventory.host import Host

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/host.py”, line 19, in

> from ansible import utils

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/utils/ **init**.py”, line 29, in

> from ansible.utils.display\_functions import \*

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/utils/display\_functions.py”, line 22, in

> from ansible.callbacks import display

> File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/callbacks.py”, line 33, in

> raise Exception()

The original line where I placed the exception was as follows:

> logging.basicConfig(filename=path, level=logging.DEBUG, format=‘%(asctime)s %(name)s %(message)s’)

If there’s currently no way around this, does anyone with more knowledge of the Ansible ecosystem have any ideas of how this could be fixed from within Ansible?

For the moment, my solution is to overwrite the constant DEFAULT\_LOG\_PATH just before the first import of the Ansible runner like so:

> from ansible import constants  
> constants.DEFAULT\_LOG\_PATH = ‘’  
> from ansible.runner import Runner

The offending code will be skipped due to the surrounding conditional, but this is obviously horribly fragile and way less than ideal.

Anyone have any thoughts on this?

Thanks!
