python variable sharing between packages/modules

前端 未结 3 1727
离开以前
离开以前 2021-01-23 16:02

trying to understand and learn how to write packages... testing with something i\'ve always used, logging...

Can you please help me understand why the \'log\' variable i

3条回答
  •  情歌与酒
    2021-01-23 16:57

    This will return the log array, and you will be able to use the logging function associated.

    main.py:

    #!/usr/bin/env python
    
    import sys
    
    sys.path.append('CLUSTER')
    import clusterlogging.differentlogging
    log=clusterlogging.differentlogging.ttylogging()
    
    log.debug("Logging module loaded")
    log.info ("It worked")
    

    differentlogging.py :

    #!/usr/bin/env python
    
    def ttylogging():
        print "Console Logging loaded"
        import sys
        import logging
        class NullHandler(logging.Handler):
            def emit(self, record):
                pass
    
        DEFAULTLOGLEVEL=logging.INFO
        log = logging.getLogger(__name__)
        log.addHandler(NullHandler())
    
        log.setLevel(DEFAULTLOGLEVEL)
        logStreamHandler = logging.StreamHandler(sys.stdout)
        logStreamHandler.setFormatter(logging.Formatter("%(asctime)s %(levelname)5s %(name)s %(lineno)d: %(message)s"))
        log.addHandler(logStreamHandler)
    
        return log  
    
    def mysqllogging():
        print "mysql logging module here"
    
    def sysloglogging():
        print "rsyslog logging module here"
    

提交回复
热议问题