Skip to content
Ethan Rowe edited this page Sep 19, 2012 · 1 revision

Example setup

Say you have the following Mandrel.py for bootstrapping your stuff:

bootstrap.SEARCH_PATH.append('~/.your_app')
bootstrap.SEARCH_PATH.append('/etc/your_app')

With the bootstrapper's default SEARCH_PATH of the bootstrap ROOT_DIR, this means you can choose to put your logging.cfg in one of three places:

  1. YOUR_PROJECT_PATH/logging.cfg
  2. ~/.your_app/logging.cfg
  3. /etc/your_app/logging.cfg

When you're developing locally, perhaps you don't bother with any logging configuration at all. That's fine; in that case, the mandrel.bootstrap.DEFAULT_LOGGING_CALLBACK will determine what function is used to configure logging, and by default it'll use mandrel.bootstrap.initialize_simple_logging. The root logger is configured to spit out everything on sys.stderr, easy.

You could define a function in your Mandrel.py and point DEFAULT_LOGGING_CALLBACK to it, if you want to customize the default behavior. Or you could have the Mandrel.py file, after setting search paths and such, use bootstrap.get_logger() to get the root logger and adjust its settings directly.

In any case, say you're on a server and you want Chef to lay down your logging configuration for you. The search path rules you've established in Mandrel.py determine where that file gets laid down.

From that point forward, you can put whatever you want in the logging configuration, according to your needs.

With the Configuration class

It is recommended that you extend mandrel.config.Configuration or mandrel.config.ForgivingConfiguration for your use case, in which case you can use the get_logger method provided there as a way of:

  • Hooking into the logger config mechanism without needing to go to the bootstrapper module directly.
  • Benefit from the name conventions enforced by the Configuration class.

One important aspect of the Configuration class' get_logger() method is namespacing: if you provide it with a logger name, that logger name is assumed to be a child logger of the NAME of your Configuration subclass.

For instance, suppose we have:

class Config(mandrel.config.ForgivingConfiguration):
    NAME = 'my_app'

    def __init__(self, *params):
        super(Config, self).__init__(*params)
        self.instance_set('logger', self.get_logger())
        self.instance_set('driver_logger', self.get_logger('driver'))

Then, in your app, you would find that:

  • Config().logger is a logger with name "my_app".
  • Config().driver_logger is a logger with name "my_app.driver".

The benefit there is that your app can utilize these child loggers for different purposes, but the hierarchical namespacing ensures that the folks responsible for your production systems can control the details however they see fit through the logging configuration file.

Clone this wiki locally