Juniper / jsnapy

Python version of Junos Snapshot Administrator
Apache License 2.0
116 stars 58 forks source link

Jsnapy Overwriting Logger #343

Open jas2061461 opened 5 years ago

jas2061461 commented 5 years ago

Description of Issue/Question

Jsnapy is overwrite the logger I've defined. I believe this is similar to: https://github.com/Juniper/jsnapy/issues/263

Workarounds (besides printing and writing to a file)

Sample code:

#! /usr/bin/env python3.7
import logging
from jnpr.jsnapy import SnapAdmin

class RouterInfo:
    def __init__(self):
        self.routers = [ "d01-bbrj01", "d02-bbrj02" ]
        self.logger()

class NetconfSession(RouterInfo):
    def __init__(self):
        super().__init__()

    def logger(self):
        # logger
        self._logger = logging.getLogger(__name__)
        self._logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter(f"%(message)s")

        # clear last iteration 
        if (self._logger.hasHandlers()):
            self._logger.handlers.clear()

        # console handler
        ch = logging.StreamHandler()
        ch.setFormatter(formatter)
        self._logger.addHandler(ch)

        # file handler
        fh = logging.FileHandler("./blah.log")
        fh.setFormatter(formatter)
        self._logger.addHandler(fh)

def main():
    data = NetconfSession()
    data._logger.debug("this line")
    js = SnapAdmin()
    data._logger.debug("that line")

if __name__ == "__main__":
    main()

This produces:

this line

If I comment out "js = SnapAdmin()"

this line
that line

Additional details

Re-instantiating the logger after invoking SnapAdmin doesn't work when I'm looping through devices. It only works for the first device.

PIP List

Package         Version 
--------------- --------
asn1crypto      0.24.0  
bcrypt          3.1.6   
certifi         2019.3.9
cffi            1.12.3  
chardet         3.0.4   
colorama        0.4.1   
configparser    3.7.4   
cryptography    2.7     
enum34          1.1.6   
future          0.17.1  
icdiff          1.9.1   
idna            2.8     
ipaddress       1.0.22  
Jinja2          2.10.1  
jsnapy          1.3.2   
junos-eznc      2.2.1   
lxml            4.3.4   
MarkupSafe      1.1.1   
mypy-extensions 0.4.1   
napalm          2.4.0   
ncclient        0.6.6   
netaddr         0.7.19  
netmiko         2.3.3   
nornir          2.2.0   
nxapi-plumbing  0.5.2   
paramiko        2.5.0   
pip             19.1.1  
pkg-resources   0.0.0   
pyaml           19.4.1  
pycparser       2.19    
pydantic        0.18.2  
pyeapi          0.8.2   
pyIOSXR         0.53    
PyNaCl          1.3.0   
pyparsing       2.4.0   
pyserial        3.4     
PyYAML          5.1.1   
requests        2.22.0  
ruamel.yaml     0.15.97 
scp             0.13.2  
setuptools      41.0.1  
six             1.12.0  
textfsm         0.4.1   
urllib3         1.25.3  
wheel           0.33.4  
xmltodict       0.12.0 

Steps to Reproduce Issue

(Include debug logs if possible and relevant. Error trace would be helpful too)

Versions Report

(Provided by running jsnapy --version. Please also mention python version.)

shaygun commented 4 years ago

I have faced the same issue too, you can turn off this feature inside the logging.yml disable_existing_loggers: False and also disable the propagation in your script after creating jsnapy object from class to avoid duplicate logs: logging.propagate = False