Logging from your Python code

You can extend and customize AxoSyslog easily by writing destinations, parsers, template functions, and sources in Python.

To debug and troubleshoot your Python code, AxoSyslog allows you to use the logger() method to send log messages to the internal() 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:

        import syslogng
    
  2. Create a logger object:

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

        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:

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