0

I'm trying to learn how to create a custom logger. I'm not exactly sure what the below error is telling me, so if anyone could lead me in the right direction that would be great.

When I import and run the logger from my launcher.py file...

import custom_logging as logging
logging.getLogger(__name__)

I get this error:

No handlers could be found for logger "mb.custom-logger"

I know that the custom-logger part of the logger name is coming from within the custom_logger/__init__.py file....

... a bunch of code

log = getLogger('mb-logger')
log.warning('Creating log : {0}'.format(log_file))

But I'm not sure where the mb part comes from in the error message or what it relates to. Any thoughts?

Also to give some context, my project is set up like this:

Project_Root/
    bin/
        launcher.py (this is where the error is occurring)
    modules/
       custom-logger/
            source/
                custom_logger/
                    __init__.py (this is where the custom logger is created)
martineau
  • 119,623
  • 25
  • 170
  • 301
Mike Bourbeau
  • 481
  • 11
  • 29
  • 1
    You probably need to show us how the custom logger is being created in the `__init__.py` script. – martineau Jun 24 '16 at 22:23
  • you may want to simplify the [project's directory structure](http://docs.python-guide.org/en/latest/writing/structure/#structure-of-the-repository) – miraculixx Jun 25 '16 at 17:26

2 Answers2

1

According to Python - No handlers could be found for logger "OpenGL.error", to debug your code you need to have:

import custom_logging as logging
logging.basicConfig()

And then you should see the details of the error in the command line output.

My assumption is that you modified the logging module.

Community
  • 1
  • 1
keiv.fly
  • 3,343
  • 4
  • 26
  • 45
  • In my custom_logger/__init__.py I'm editing the logger config with a dictionary like in this example: http://docs.python-guide.org/en/latest/writing/logging/ logging.config.dictConfig(log_settings); logging.captureWarnings(True); Will calling basicConfig() override my custom settings? – Mike Bourbeau Jun 25 '16 at 15:46
  • Also, what does the 'mb' refer to in `mb.custom_logger`? – Mike Bourbeau Jun 25 '16 at 16:08
1

Finally found the problem. @martineau suggested showing the code, so I started editing it to be more presentable. During that process I found the error!

To answer my initial question, the mb in the error message refers to the root_logger.

When I was overriding the getLogger method inside of my custom_logger/init.py, the logger that I was returning didn't match up with the root logger. Like so:

Error Code:

root_logger = getLogger(mb_tool)                    # Name that needs to match = 'mb_tool'

def getLogger(name='default'):
    for handler in root_logger.handlers:
        handler.setLevel(DEBUG)
    return logging.getLogger(mb.{0}'.format(name))  # Name that doesn't match = 'mb'

Fixed:

root_logger = getLogger(mb_tool)  

def getLogger(name='default'):
    for handler in root_logger.handlers:
        handler.setLevel(DEBUG)
    return logging.getLogger(mb_tool.{0}'.format(name))  

Changing the mb to mb_tool fixed everything. Thanks for taking the time to help, everyone.

Mike Bourbeau
  • 481
  • 11
  • 29