def test_enable_basic_logging(fs): assert Path("scrapli.log").is_file() is False enable_basic_logging(file=True, level="debug") scrapli_logger = logging.getLogger("scrapli") assert scrapli_logger.level == 10 assert isinstance(scrapli_logger.handlers[1], ScrapliFileHandler) assert isinstance(scrapli_logger.handlers[1].formatter, ScrapliFormatter) assert scrapli_logger.propagate is False assert Path("scrapli.log").is_file() is True # reset the main logger to propagate and delete the file handler so caplog works! logger.propagate = True del logger.handlers[1]
def test_enable_basic_logging_bad_mode(): with pytest.raises(ScrapliException): enable_basic_logging(file="mylog.log", level="debug", mode="tacocat") # reset the main logger to propagate and delete the file handler so caplog works! logger.propagate = True
"""examples.logging.opinionated_logging""" from scrapli import Scrapli from scrapli.logging import enable_basic_logging # the `enable_basic_logging` function accepts a bool or a string for the `file` argument -- if you # provide a string that string will be used as the output path for the log file, if you just pass # `True` as in this example, a file called "scrapli.log" will be created in your working directory enable_basic_logging(file=True, level="debug") MY_DEVICE = { "host": "172.18.0.11", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, "platform": "cisco_iosxe", } def main(): """ Example demonstrating basic logging with scrapli In this example rather than dealing with python logging module directly, we simply import and call the `enable_basic_logging` function of the scrapli.logging package. This will apply logging formatting and handlers in an opinionated fashion -- so basically just use this for testing or for quickly getting logs, but do not use this for your custom applications. """ with Scrapli(**MY_DEVICE) as conn: print(conn.get_prompt()) print(conn.send_command("show run | i hostname").result)