Open darKnight18 opened 6 years 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.
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%)