3

the module of lib_xml.py :

import conf_store

def hello():
        print conf_store.logger
        conf_store.logger.debug('why')
        print 'where'

the module of conf_store.py:

#! /usr/bin/python 

import os, subprocess, logging, time, shutil, fcntl
import lib_xml

def log():
        """
        a log handle
        """
        import logging.handlers
        global logger
        LOG_PATH = "/opt/conf_store.log"
        logger = logging.getLogger('conf_store')
        logger.setLevel(logging.DEBUG)
        ch = logging.handlers.WatchedFileHandler(LOG_PATH)
        ch.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)



if __name__ == "__main__":
        log()

        while(True):
                lib_xml.hello()
                logger.debug('what')

How to share the object of logger between lib_xml.py and conf_store.py ?

2 Answers 2

8

You can leave that to the logging module.

Just import logging; logging.getLogger() with the same key will always return the same object; the following code added to lib_xml would log messages to the same logger:

import logging

logger = logging.getLogger('conf_store')

The logging configuration is global by design.

There is an advantage in just using the current module name as the logging key instead; it lets you tease out what location messages were logged from:

logger = logging.getLogger(__name__)
Sign up to request clarification or add additional context in comments.

Comments

0

remove global logger

return logger from log()

add one line: logger = log() after that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.