---
title: "Logging from your Python code"
url: "https://axoflow.com/docs/axosyslog-core/4.28/chapter-configuration-file/python-code-logging/"
last_modified: "2023-07-02T20:37:04+02:00"
---

> For the complete documentation index, see [llms.txt](https://axoflow.com/docs/axosyslog-core/4.28/llms.txt).

# Logging from your Python code

You can extend and customize AxoSyslog easily by writing [destinations](https://axoflow.com/docs/axosyslog-core/4.28/chapter-destinations/python-destination/index.md), [parsers](https://axoflow.com/docs/axosyslog-core/4.28/chapter-parsers/python-parser/index.md), [template functions](https://axoflow.com/docs/axosyslog-core/4.28/chapter-destinations/python-destination/index.md#template-function-python), and [sources](https://axoflow.com/docs/axosyslog-core/4.28/chapter-sources/python-source/index.md) in Python.

To debug and troubleshoot your Python code, AxoSyslog allows you to use the `logger()` method to send log messages to the [`internal()`](https://axoflow.com/docs/axosyslog-core/4.28/chapter-sources/configuring-sources-internal/index.md) source of AxoSyslog. That way the diagnostic messages of your Python code are treated the same way as other such log messages of AxoSyslog. This has the following benefits:

- The `logger()` method respects the log level settings of AxoSyslog. You can write error, warning, info, debug, and trace level messages.
- You can follow what your Python code is doing even if AxoSyslog is running as a daemon in the background.

Logging to the `internal()` source is available in AxoSyslog version 3.20 and later.

To send log messages to the internal() source from Python

1. Add the following import to your Python code:

   ```shell
       import syslogng
   ```
2. Create a logger object:

   ```shell
       logger = syslogng.Logger()
   ```
3. Use the logger object in your Python code, for example:

   ```shell
       logger.info("This is a sample log message send from the Python code.")
   ```

   You can use the following log levels: `logger.error`, `logger.warning`, `logger.info`, `logger.debug`, `logger.trace`
4. Make sure that your AxoSyslog configuration includes the `internal()` source, for example:

   ```shell
       source s_internal { internal(); };
       destination d_internal { file("/var/log/internal.txt"); };
       log {source(s_internal); destination(d_internal); };
   ```

Last modified July 2, 2023: [Change highlight mode of code examples (2f8a9593)](https://github.com/axoflow/axosyslog-core-docs/commit/2f8a95937c6498193e7168ce8b0dc831e9f0f8ad)
