0

I am facing the following scenario :

I set DEBUG=True/False and based on this I do the logging.

Traditional way of doing this is

if DEBUG:
   logger.log(whatever)

Is there a better way to write this code? Using closures/lambda functions etc.. ?

3 Answers 3

7

Look at the logging library's manual: http://docs.python.org/library/logging.html

you can configure directly in code, or in a config file ...

e.g.

import logging
logging.basicConfig(filename='example.log',level=logging.INFO)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

(see how it'll only display info and warning messages)

or, from a file:

import logging
import logging.config

logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

See the config section for the logging config format. http://docs.python.org/howto/logging.html#configuring-logging

The relevance of logging.getLogger(...) is that it allows you to set different log levels for different parts of your code.

Edit: if you're worried about expensive operations, it's best to use the following:

if logger.isEnabledFor(logging.DEBUG):
    logger.debug('Message with %s, %s', expensive_func1(),
                                        expensive_func2())

HTH

Sign up to request clarification or add additional context in comments.

Comments

3

You are over thinking this. You have 2 lines of code that are very clear and concise. There is not much that you could do to change this without making things more complicated.

The only other short and simple way to do logging is to move the debug check into a wrapper function.

def log(msg):
  if DEBUG:
    logger.log(msg)

This will mean that the rest of your code will just have one line when logging a message. The only potential downside is that if you are building the message string, it will be built even when DEBUG is false. This is really only an issue if you need a highly efficient program and you are constructing very complicated log strings.

1 Comment

This works fine until you want some extra features from your logging, such as warning/info/ messages (not all logging messages are for debugging. Some are needed in production), fine-grained config, externalised config (in files not code), variable substitution ... also, you may end up pulling in modules which already utilitses the "logging" library. So, better to use a solution like the pre-baked logging lib ..
3

In this particular instance, you could consider using python's logging module, which will handle this (and other logging needs) for you. In its most basic incarnation:

# Call once somewhere early in your code
logging.basicConfig(level=DEBUG and logging.DEBUG or logging.INFO)
....

logging.debug(whatever)

One thing to watch is that 'whatever' will always get evaluated (unlike in your example).

1 Comment

.. but amir75's answer is more complete :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.