Monday, July 29, 2013

Re: Changing log filename

Since your handler is used for the "myproject" logger, to use that handler all your apps would have to use loggers descended from that logger, that is, all your apps would have to use something like:

  logging.getLogger('myproject')
or
  logging.getLogger('myproject.app1')
or
  logging.getLogger('myproject.app4.models")  # might also be expressed as logging.getLogger(__name__), but, depending on where you put your apps, that might produce app4.models instead.

This is less than satisfactory since it ties your apps to this project, and means that they must be edited to be used in a new project.

Also uncomfortable is that if you want apps that you have pip installed (as opposed to having copied into your project's directory) have to be modified to use this scheme.

Note that if you uses logging in multiple modules within a project, all such modules have to be edited.


An (commonly used) alternative is to attach your handler to the root logger, since it is the ancestor of all loggers.  So long as you don't set propagate to False (default is True) on any descendant logger, all events will reach the handler on the root logger.  This gives you a single place to change the file name.

The logging documentation at http://docs.python.org/2.7/library/logging.html is not as clear as I might wish about the tree of ancestors and dependents and how events are passed to higher level handlers, but the information is there to be dug out.


On Fri, Jul 26, 2013 at 10:01 PM, David Kumar <dathku@gmail.com> wrote:
I have a standard logger that I would like to use for all of my applications, the only thing I want to change is the name of the file.  Is there a way to do that with out having to create a logger and handler for each of my apps.

Here is the logging settings that I am using.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(module)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'app_logger': {
            'level':'DEBUG',
            'class':'logging.handlers.TimedRotatingFileHandler',
            'filename': '/var/log/django/app.log',
            'backupCount': 5,
            'when' : 'midnight',
            'formatter': 'standard',
        },
    },
    'loggers': {
        'myproject': {
            'handlers': ['app_logger'],
            'level': 'INFO',
        }
    }
}


In my app I would like to be able to override the 'filename': '/var/log/django/another_app.log',
import logging
log = logging.getLogger('myproject')
<change log file to '/var/log/django/another_app.log'>
log.info("Something here")

Any ideas?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment