NameError: global name 'logger' is not def

2020-07-08 08:05发布

问题:

I wrote a logging.json file from where the logging configuration is setup after which i created a class in which all my methods will be there but for logging when i use logger inside the class it is throwing error NameError: global name 'logger' is not defined

app.py

#/usr/bin/python

import sys
import logging
import time
import json
import os
import logging.config

def setup_logging(default_path='logging.json',default_level=logging.INFO,env_key='LOG_CFG'):
    """Setup logging configuration"""
    path = default_path
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = json.load(f)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)

class Generic:

        def __init__(self , file_path):
                self.file_path = file_path

        def check_mime(self):
                logger.info('Generic on file {} starts at {}'.format(file_path , time.time()))


print 'File path is {}'.format(sys.argv[1])
file_path = sys.argv[1]

def parser():
        parser = Generic(file_path)
        parser.check_mime()
def main():
        print 'This is intended for module purpose only'
        setup_logging()
        parser()

if __name__ == '__main__':
        main()

logging.json

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        }
    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "simple",
            "stream": "ext://sys.stdout"
        },

        "info_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "INFO",
            "formatter": "simple",
            "filename": "logs/gp.log",
            "maxBytes": 10485760,
            "backupCount": 20,
            "encoding": "utf8"
        },

        "error_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "ERROR",
            "formatter": "simple",
            "filename": "logs/errors.log",
            "maxBytes": 10485760,
            "backupCount": 20,
            "encoding": "utf8"
        }
    },

    "loggers": {
        "my_module": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": "no"
        }
    },

    "root": {
        "level": "INFO",
        "handlers": ["console", "info_file_handler", "error_file_handler"]
    }
}

Problem :

When i run the program error

$ python app.py /home/default/domain.txt
File path is /home/default/domain.txt
This is intended for module purpose only
Traceback (most recent call last):
  File "app.py", line 44, in <module>
    main()
  File "app.py", line 41, in main
    parser()
  File "app.py", line 37, in parser
    parser.check_mime()
  File "app.py", line 29, in check_mime
    logger.info('GenericParser
NameError: global name 'logger' is not defined

Iam using the logging example following in this link [ https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/ ]

Any suggestions on how to solve this as logger is not global, any way to make it global.?

回答1:

The example you link to has:

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) #<<<<<<<<<<<<<<<<<<<<

you missed the logger definition.

You can either put a self.logger = logging.getLogger(__name__) in your Generic.__init__() function, or define a global logger right after the import as in the example.



回答2:

This below should be added to your code

logger=None
def setup():
   logger.debug('put some text')
   return 0

def main():
   global logger
   logger = logging.getLogger('give_some_logger_name')
   logger.setLevel(logging.DEBUG)

   ret = setup()


回答3:

In your class you are using logger but you have not defined it. Either set logger like:

    logger = logging.getLogger(__name__)

or use logging instead:

    logging.info('Generic on file {} starts at {}'.format(file_path , time.time()))