Python global logging [duplicate]

2019-03-27 13:48发布

This question already has an answer here:

How do I make a Logger global so that I can use it in every module I make?

Something like this in moduleA:

import logging
import moduleB

log = logging.getLogger('')

result = moduleB.goFigure(5)
log.info('Answer was', result)

With this in moduleB:

def goFigure(integer):
    if not isinstance(integer, int):
        log.critical('not an integer')
    else:
        return integer + 1

Currently, I will get an error because moduleB does not know what log is. How do I get around that?

3条回答
时光不老,我们不散
2楼-- · 2019-03-27 13:59

A module has by default only access to builtin functions and builtin constants. For all other variables, functions... you have to use the keyword import.

Now for your concrete example, you can import the log-variable of moduleA in modulesB like this:

from moduleA import log

The following would be equivalent because the logging-module returns the same instance of the logger that was returned to moduleA:

import logging
log = logging.getLogger('')

Another solution for you could be to use the default-logger of the logging module like this:

logging.info("Hello")
查看更多
戒情不戒烟
3楼-- · 2019-03-27 14:03

You could make your own logging "module" which instantiates the logger, than have all of your code import that instead. Think:

logger.py:

import logging
log = logging.getLogger('')

codeA.py:

from logger import log
log.info('whatever')

codeB.py:

from logger import log
log.warn('some other thing')
查看更多
地球回转人心会变
4楼-- · 2019-03-27 14:11

Creating a global logger which can be used to

  1. create a new log file or
  2. returns logger for a global log file.

Create a module called called myLogger.py : This will have the log creation code

myLogger.py:

import logging

def myLog(name, fname = 'myGlobalLog.log'):
'''Debug Log'''                                                                                                 
    logger = logging.getLogger(name);                                                                               
    logger.setLevel(logging.DEBUG)                                                                                  
    fhan = logging.FileHandler(fname)                                                                               
    fhan.setLevel(logging.DEBUG)                                                                                    
    logger.addHandler(fhan)                                                                                         
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')                           
    fhan.setFormatter(formatter)
    '''comment this to enable requests logger'''                                                                    
    #logger.disabled = True
    return logger

Now to create a new log in your module say A.py

from myLogger import myLog
log = myLog(__name__, 'newLog.log')
log.debug("In new log file")

Thus you have to pass the file name along while getting the logger.

To use the global logger in A.py:

from myLogger import myLog
log = myLog(__name__)
log.debug("In myGlobalLog file")

Need not pass the file name in this case as we gonna use the global log.

查看更多
登录 后发表回答