Juniper / py-junos-eznc

Python library for Junos automation
https://www.juniper.net/documentation/en_US/junos-pyez/information-products/pathway-pages/junos-pyez-developer-guide.html
Apache License 2.0
670 stars 345 forks source link

Logger initialized with SW and JSNAPy module tend to print output multiple times on console and file #810

Open darKnight18 opened 6 years ago

darKnight18 commented 6 years ago
import logging

from jnpr.junos.device import Device
from jnpr.junos.utils.sw import SW

def update_progress(dev, report):
    logger = logging.getLogger()
    #logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    logger.propagate = 0
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # Setup file logging for Junos install
    report_handler = logging.FileHandler('/var/log/ex4200/install_1.log')
    report_handler.setFormatter(formatter)
    logger.addHandler(report_handler)
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)
    console_handler.setFormatter(formatter)
    logger.addHandler(console_handler)
    logger.info(report)

class ConnectDevice:
    def __init__(self):
        self.dev = Device('x.x.x.x',passwd='y.y.y.y', user='demo')

    def software_update(self):
        self.dev.open()
        sw = SW(self.dev)
        rsp = sw.install(package="/var/tmp/junos/jinstall-ex-4200-15.1R6.7-domestic-signed.tgz", validate=True, progress=update_progress, remote_path='/var/tmp')

con = ConnectDevice()
con.software_update()

Console and File Output root@amoghv:/etc/jsnapy/testfiles# root@amoghv:/etc/jsnapy/testfiles# python test.py 2018-01-19 15:09:24,129 - main - INFO - computing checksum on local package: /var/tmp/junos/jinstall-ex-4200-15.1R6.7-domestic-signed.tgz 2018-01-19 15:09:24,371 - main - INFO - cleaning filesystem ... 2018-01-19 15:09:24,371 - main - INFO - cleaning filesystem ... 2018-01-19 15:09:35,246 - main - INFO - before copy, computing checksum on remote package: /var/tmp/jinstall-ex-4200-15.1R6.7-domestic-signed.tgz 2018-01-19 15:09:35,246 - main - INFO - before copy, computing checksum on remote package: /var/tmp/jinstall-ex-4200-15.1R6.7-domestic-signed.tgz 2018-01-19 15:09:35,246 - main - INFO - before copy, computing checksum on remote package: /var/tmp/jinstall-ex-4200-15.1R6.7-domestic-signed.tgz 2018-01-19 15:10:10,518 - main - INFO - jinstall-ex-4200-15.1R6.7-domestic-signed.tgz: 13778944 / 137685524 (10%) 2018-01-19 15:10:10,518 - main - INFO - jinstall-ex-4200-15.1R6.7-domestic-signed.tgz: 13778944 / 137685524 (10%) 2018-01-19 15:10:10,518 - main - INFO - jinstall-ex-4200-15.1R6.7-domestic-signed.tgz: 13778944 / 137685524 (10%) 2018-01-19 15:10:10,518 - main - INFO - jinstall-ex-4200-15.1R6.7-domestic-signed.tgz: 13778944 / 137685524 (10%)

apurvaraghu commented 8 months ago

According to your script, each time the function update_progress() is called, it adds a new console handler and a file handler to the root logger. Please try to change the way of configuring the logging handlers by adding them only once. Below is the modified code.

import logging

from jnpr.junos.device import Device
from jnpr.junos.utils.sw import SW

# Initialize the logger once
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

report_handler = logging.FileHandler('test.log')
report_handler.setFormatter(formatter)
logger.addHandler(report_handler)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)

def update_progress(dev, report):
    logger.info(report)

class ConnectDevice:
    def __init__(self):
        self.dev = Device('x.x.x.x',passwd='xyz', user='xyz')

    def software_update(self):
        self.dev.open()
        sw = SW(self.dev)
        rsp = sw.install(package="/path/to/package/filename", validate=True, progress=update_progress, remote_path='/var/tmp')

con = ConnectDevice()
con.software_update()

output: 2024-02-24 05:46:25,822 - ncclient.transport.ssh - INFO - [host 10.13.7.10 session-id 34290] Received message from host 2024-02-24 05:46:25,823 - root - INFO - computing checksum on local package: /var/tmp/pkg 2024-02-24 05:46:25,823 - root - before copy, computing checksum on remote package: /var/tmp/pkg

Kindly try the modified code and check the output.