# How can my inventory script output logs to the log\_path in ansible.cfg?

**URL:** <https://forum.ansible.com/t/how-can-my-inventory-script-output-logs-to-the-log-path-in-ansible-cfg/39288>\
**Category:** Get Help\
**Tags:** inventory\
**Created:** [December 23, 2024, 7:33am UTC](https://forum.ansible.com/t/how-can-my-inventory-script-output-logs-to-the-log-path-in-ansible-cfg/39288 "2024-12-23T07:33:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![huangsijun17](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/huangsijun17/32/7051_2.png) [@huangsijun17](https://forum.ansible.com/u/huangsijun17)\
**Post date:** [December 23, 2024, 7:33am UTC](https://forum.ansible.com/t/how-can-my-inventory-script-output-logs-to-the-log-path-in-ansible-cfg/39288/1 "2024-12-23T07:33:12Z")

</div>

I wrote a relatively complex inventory script in python to automatically get the host list from the HTTP request response and query the local cache database and production database to classify it. I hope to output some logging system to this script, but I don’t know how to call the built-in logging system of ansible correctly.  
I have set log\_path in ansible.cfg and successfully output ansible logs. But it is difficult for this inventory script to output content to ansible logs.

* * *

I wrote a separate inventory script for testing. All its “logs” above the error level are output to one line, and the content at the info level is not output.

```python
#!/usr/bin/env python3

import logging
import sys

logging.basicConfig(level=logging.INFO)

# Get the root logger
root_logger = logging.getLogger()

print(root_logger, file=sys.stderr)

# Output log level
logging.error("Log level: %s", logging.getLevelName(root_logger.getEffectiveLevel()))

# Output handler information
logging.error("Handlers:")
for handler in root_logger.handlers:
    logging.error(f" - Type: {type(handler). __name__ }")
    if isinstance(handler, logging.FileHandler):
        logging.error(f" - File path: {handler.baseFilename}")
    if isinstance(handler, logging.StreamHandler):
        logging.error(f" - Output stream: {handler.stream}")
    logging.error(f" - Formatter: {handler.formatter}")

# Output formatter information
logging.error("Formatter:")
if root_logger.handlers:
    formatter = root_logger.handlers[0].formatter
    logging.error(f" - Format: {formatter._fmt}")
    logging.error(f" - Date format: {formatter.datefmt}")

logging.debug("debug message\r\n")
logging.info("info message\r\n")
logging.warning("warning message\r\n")
logging.error("error message\r\n")
logging.critical("critical message\r\n")

print("print stderr message\r\n", file=sys.stderr)

# Outputs a blank host list.
print("{}")

```

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [December 23, 2024, 8:26am UTC](https://forum.ansible.com/t/how-can-my-inventory-script-output-logs-to-the-log-path-in-ansible-cfg/39288/2 "2024-12-23T08:26:07Z")

</div>

Inventory _scripts_ are for basic inventory generation. If you want to do more interesting things, better write an inventory _plugin_. Inventory plugins have access to many facilities from Ansible, including Ansible’s option management and the `Display` class, which logs to `log_path`.
