Skip to content

Display log messages from imported modules in Custom Script? #16766

Answered by llamafilm
llamafilm asked this question in Q&A
Discussion options

You must be logged in to vote

I found a solution, by adding a custom logging handler. Here's a working example.

import logging
import requests

from extras.scripts import Script

class NetboxHandler(logging.Handler):
    """Send log messages from imported modules to Netbox custom script log"""

    def __init__(self, script: Script)-> None:
        logging.Handler.__init__(self)
        self.script = script
        self.setFormatter(logging.Formatter('[%(name)s] %(message)s'))

    def emit(self, record: logging.LogRecord) -> None:
        msg = self.format(record)

        if record.levelno == 60:
            self.script.log_success(msg)
        elif record.levelno == logging.ERROR:
            self.script.log_failure(

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by llamafilm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant