# python: writing server-style Python sources

The Python source allows you to write your own source in Python. You can import external Python modules to receive or fetch the messages. Since many services have a Python library, the Python source makes integrating AxoSyslog very easy and quick.

You can write two different type of sources in Python:

  * Server-style sources that receives messages. Write server-style sources if you want to use an event-loop based, nonblocking server framework in Python, or if you want to implement a custom loop.

  * Fetcher-style sources that actively fetch messages. In general, write fetcher-style sources (for example, when using simple blocking APIs), unless you explicitly need a server-style source.




This section describes server-style sources. For details on fetcher-style sources, see [python-fetcher: writing fetcher-style Python sources](../../docs/axosyslog-core/chapter-sources/python-fetcher-source/index.md).

The following points apply to using Python blocks in AxoSyslog in general:

  * Python parsers and template functions are available in AxoSyslog version 3.10 and later.

Python destinations and sources are available in AxoSyslog version 3.18 and later.

  * Supported Python versions: 2.7 and 3.4+ (if you are using pre-built binaries, check the dependencies of the package to find out which Python version it was compiled with).

  * The Python block must be a top-level block in the AxoSyslog configuration file.

  * If you store the Python code in a separate Python file and only include it in the AxoSyslog configuration file, make sure that the PYTHONPATH environment variable includes the path to the Python file, and export the PYTHON_PATH environment variable. For example, if you start AxoSyslog manually from a terminal and you store your Python files in the `/opt/syslog-ng/etc` directory, use the following command: `export PYTHONPATH=/opt/syslog-ng/etc`.

In production, when AxoSyslog starts on boot, you must configure your startup script to include the Python path. The exact method depends on your operating system. For recent Red Hat Enterprise Linux, Fedora, and CentOS distributions that use systemd, the `systemctl` command sources the `/etc/sysconfig/syslog-ng` file before starting AxoSyslog. (On openSUSE and SLES, `/etc/sysconfig/syslog` file.) Append the following line to the end of this file: `PYTHONPATH="<path-to-your-python-file>"`, for example, `PYTHONPATH="/opt/syslog-ng/etc"`.

  * The Python object is initiated every time when AxoSyslog is started or reloaded.

Warning If you reload AxoSyslog, existing Python objects are destroyed, therefore the context and state information of Python blocks is lost. Log rotation and updating the configuration of AxoSyslog typically involves a reload. 

  * The Python block can contain multiple Python functions.

  * Using Python code in AxoSyslog can significantly decrease the performance of AxoSyslog, especially if the Python code is slow. In general, the features of AxoSyslog are implemented in C, and are faster than implementations of the same or similar features in Python.

  * Validate and lint the Python code before using it. The AxoSyslog application does not do any of this.

  * Python error messages are available in the `internal()` source of AxoSyslog.

  * You can access the name-value pairs of AxoSyslog directly through a message object or a dictionary.

  * To help debugging and troubleshooting your Python code, you can send log messages to the `internal()` source of AxoSyslog. For details, see [Logging from your Python code](../../docs/axosyslog-core/chapter-configuration-file/python-code-logging/index.md).




Note

Starting with 3.26, AxoSyslog assigns a persist name to Python sources and destinations. The persist name is generated from the class name. If you want to use the same Python class multiple times in your AxoSyslog configuration, add a unique `persist-name()` to each source or destination, otherwise AxoSyslog will not start. For example:
```
 
       log {
            source { python(class(PyNetworkSource) options("port" "8080") persist-name("<unique-string>); };
            source { python(class(PyNetworkSource) options("port" "8081")); };
        };
    
```

Alternatively, you can include the following line in the Python package: `@staticmethod generate_persist_name`. For example:
```
 
       from syslogng import LogSource
          class PyNetworSource(LogSource):
            @staticmethod
            def generate_persist_name(options):
                return options["port"]
            def run(self):
                pass
            def request_exit(self):
                pass
    
```

## Declaration:

Python sources consist of two parts. The first is a AxoSyslog source object that you define in your AxoSyslog configuration and use in the log path. This object references a Python class, which is the second part of the Python source. The Python class receives or fetches the log messages, and can do virtually anything that you can code in Python. You can either embed the Python class into your AxoSyslog configuration file, or [store it in an external Python file](../../docs/axosyslog-core/chapter-configuration-file/python-code-external-file/index.md).
```
 
       source <name_of_the_python_source>{
            python(
                class("<name_of_the_python_class_executed_by_the_source>")
                options(
                    "option1" "value1",
                    "option2" "value2"
                )
            );
        };
        
        python {
        from syslogng import LogSource
        from syslogng import LogMessage
        
        class <name_of_the_python_class_executed_by_the_source>(LogSource):
            def init(self, options): # optional
                print("init")
                print(options)
                self.exit = False
                return True
        
            def deinit(self): # optional
                print("deinit")
        
            def run(self): # mandatory
                print("run")
                while not self.exit:
                    # Must create a message
                    msg = LogMessage("this is a log message")
                    self.post_message(msg)
        
            def request_exit(self): # mandatory
                print("exit")
                self.exit = True
        };
    
```

## Methods of the python() source

Server-style Python sources must be inherited from the `syslogng.LogSource` class, and must implement at least the `run` and `request_exit` methods. Multiple inheritance is allowed, but only for pure Python super classes.

You can implement your own event loop, or integrate the event loop of an external framework or library, for example, [KafkaConsumer](<https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html>), [Flask](<http://flask.pocoo.org/>), [Twisted engine](<https://twisted.org/>), and so on.

To post messages, call `LogSource::post_message()` method in the `run` method.

For the list of available optional parameters, see [python() and python-fetcher() source options](../../docs/axosyslog-core/chapter-sources/python-source/reference-source-python/index.md).

## init(self, options) method (optional)

The AxoSyslog application initializes Python objects every time when it is started or reloaded. The `init` method is executed as part of the initialization. You can perform any initialization steps that are necessary for your source to work.

Warning If you reload AxoSyslog, existing Python objects are destroyed, therefore the context and state information of Python blocks is lost. Log rotation and updating the configuration of AxoSyslog typically involves a reload. 

When this method returns with False, AxoSyslog does not start. It can be used to check options and return False when they prevent the successful start of the source.

`options`: This optional argument contains the contents of the `options()` parameter of the AxoSyslog configuration object as a Python dictionary.

## run(self) method (mandatory)

Use the `run` method to implement an event loop, or start a server framework or library. Create `LogMessage` instances in this method, and pass them to the log paths by calling `LogSource::post_message()`.

Currently, `run` stops permanently if an unhandled exception happens.

For details on parsing and posting messages, see [Python LogMessage API](../../docs/axosyslog-core/chapter-sources/python-source/python-source-logmessage/index.md).

## request_exit(self) method (mandatory)

The AxoSyslog application calls this method when AxoSyslog is shut down or restarted. The `request_exit` method must shut down the event loop or framework, so the `run` method can return gracefully. If you use blocking operations within the `run()` method, use `request_exit()` to interrupt those operations and set an exit flag, otherwise AxoSyslog is not able to stop. Note that AxoSyslog calls the `request_exit` method from a thread different from the source thread.

## close_batch(self)

Closes the current source-side batch. Source-side batching helps AxoSyslog to effectively process a larger chunk of messages, instead of processing messages each message. For example, when feeding a destination queue and instead of taking a lock on the queue for every message (causing contention), we only take it once per batch.

The native drivers built into AxoSyslog typically close batches once every mainloop iteration, allowing a single iteration to process multiple messages. For instance, when receiving multiple messages in a single TCP datagram, all of those messages can be processed as a part of the same batch.

In Python-based log sources, a batch will automatically be closed after every message posted via `post_message()`, except if `self.auto_close_batches` is set to `False` during initialization. In case `self.auto_close_batches` is set to `False`, the driver has to call `close_batch()` explicitly, preferably at a natural boundary between incoming batches of messages. A good example is when we retrieve several messages via the same HTTP REST call, then the right time to close the batch would be after the last message in the response is posted.

## The deinit(self) method (optional)

This method is executed when AxoSyslog is stopped or reloaded. This method does not return a value.

Warning If you reload AxoSyslog, existing Python objects are destroyed, therefore the context and state information of Python blocks is lost. Log rotation and updating the configuration of AxoSyslog typically involves a reload. 

## set_transport_name(self, name)

Set the transport name used to retrieve messages This function can be called to customize the [`${TRANSPORT}` name-value pair](../../docs/axosyslog-core/chapter-manipulating-messages/customizing-message-format/reference-macros/index.md#macro-transport).

* * *

[Python LogMessage API](../../docs/axosyslog-core/chapter-sources/python-source/python-source-logmessage/index.md)

[python() and python-fetcher() source options](../../docs/axosyslog-core/chapter-sources/python-source/reference-source-python/index.md)

Last modified November 20, 2024: [Broken link updates (5644de9a)](<https://github.com/axoflow/axosyslog-core-docs/commit/5644de9a8069da37e3bebf0ed5a4e73cf958a66b>)