当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Error message "no handlers could be found for logger ZODB.FileStorage" on copied ZODB database

问题描述:

I created a database using ZODB, then I copied-pasted it to another PC. I wonder why every time I log in this database (the copied one) I get this error:

no handlers could be found for logger (ZODB.FileStorage)

Note: the program doesn't break, it just print out the statement in red as if it's an error.

What is this annoying message and why does it happen every time I log in?

Update no(1): What is the python logging system ? Why do I need to configure it in my application?

Note:

  1. I only use ZODB.

  2. My OS is Windows XP sp2.

网友答案:

Short Answer

You don't need to configure it for your application. Its useful to do so.

Long Answer

The logging module is a python module that allows any python code to log information in a way that is output-agnostic to the actual application using it. Libraries at any depth can import the logging module, log info, warnings, errors, etc and not have to know exactly how the user will receive them.

What you are seeing is regarding the absence of a handler in your own application. ZODB is obviously logging information, yet because you have not defined a handler, this logging information is just vaporizing into the ether. It's notifying you that if you would like to see some logging info, you should define a handler

From the Logging Cookbook:

import logging

# create logger with 'spam_application'
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

This example creates a logger with a name for your app. It then creates two types of handlers. One is a file handler to write DEBUG level logs to a file. The second is a stream handler which will send errors to your console.

If you wanted to see more verbose information going to your console, you could change the log level to INFO:

ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

Also notice the concept of "formatters". You can set different log formats for different handlers.

If you at least create a stream handler in your app, then it will no longer warn you that a handler has not been defined.

For python 2.7+, the logging module includes a NullHandler. It is intended to be used in a library (such as your database library) to be a default handler that silences errors when the end-user is using the library without any logging handlers (as you were doing). So if you do not care about logging at all and simply want to silence it, you could add a NullHandler in your own code. It is recommended to actually set up logging, but this is just another options:

Adding NullHandler to the logger

import logging
logging.getLogger('spam_application').addHandler(logging.NullHandler())
分享给朋友:
您可能感兴趣的文章:
随机阅读: