~ubuntu-branches/ubuntu/quantal/python2.7/quantal

« back to all changes in this revision

Viewing changes to Doc/howto/logging-cookbook.rst

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 19:28:43 UTC
  • mto: (36.1.11 sid)
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: package-import@ubuntu.com-20120309192843-n84bbtrkfxw34p6n
Tags: upstream-2.7.3~rc1
ImportĀ upstreamĀ versionĀ 2.7.3~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
610
610
to have all the processes log to a :class:`SocketHandler`, and have a separate
611
611
process which implements a socket server which reads from the socket and logs
612
612
to file. (If you prefer, you can dedicate one thread in one of the existing
613
 
processes to perform this function.) The following section documents this
614
 
approach in more detail and includes a working socket receiver which can be
615
 
used as a starting point for you to adapt in your own applications.
 
613
processes to perform this function.) :ref:`This section <network-logging>`
 
614
documents this approach in more detail and includes a working socket receiver
 
615
which can be used as a starting point for you to adapt in your own
 
616
applications.
616
617
 
617
618
If you are using a recent version of Python which includes the
618
619
:mod:`multiprocessing` module, you could write your own handler which uses the
679
680
``.1``. Each of the existing backup files is renamed to increment the suffix
680
681
(``.1`` becomes ``.2``, etc.)  and the ``.6`` file is erased.
681
682
 
682
 
Obviously this example sets the log length much much too small as an extreme
 
683
Obviously this example sets the log length much too small as an extreme
683
684
example.  You would want to set *maxBytes* to an appropriate value.
684
685
 
 
686
An example dictionary-based configuration
 
687
-----------------------------------------
 
688
 
 
689
Below is an example of a logging configuration dictionary - it's taken from
 
690
the `documentation on the Django project <https://docs.djangoproject.com/en/1.3/topics/logging/#configuring-logging>`_.
 
691
This dictionary is passed to :func:`~logging.config.dictConfig` to put the configuration into effect::
 
692
 
 
693
    LOGGING = {
 
694
        'version': 1,
 
695
        'disable_existing_loggers': True,
 
696
        'formatters': {
 
697
            'verbose': {
 
698
                'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
 
699
            },
 
700
            'simple': {
 
701
                'format': '%(levelname)s %(message)s'
 
702
            },
 
703
        },
 
704
        'filters': {
 
705
            'special': {
 
706
                '()': 'project.logging.SpecialFilter',
 
707
                'foo': 'bar',
 
708
            }
 
709
        },
 
710
        'handlers': {
 
711
            'null': {
 
712
                'level':'DEBUG',
 
713
                'class':'django.utils.log.NullHandler',
 
714
            },
 
715
            'console':{
 
716
                'level':'DEBUG',
 
717
                'class':'logging.StreamHandler',
 
718
                'formatter': 'simple'
 
719
            },
 
720
            'mail_admins': {
 
721
                'level': 'ERROR',
 
722
                'class': 'django.utils.log.AdminEmailHandler',
 
723
                'filters': ['special']
 
724
            }
 
725
        },
 
726
        'loggers': {
 
727
            'django': {
 
728
                'handlers':['null'],
 
729
                'propagate': True,
 
730
                'level':'INFO',
 
731
            },
 
732
            'django.request': {
 
733
                'handlers': ['mail_admins'],
 
734
                'level': 'ERROR',
 
735
                'propagate': False,
 
736
            },
 
737
            'myproject.custom': {
 
738
                'handlers': ['console', 'mail_admins'],
 
739
                'level': 'INFO',
 
740
                'filters': ['special']
 
741
            }
 
742
        }
 
743
    }
 
744
 
 
745
For more information about this configuration, you can see the `relevant
 
746
section <https://docs.djangoproject.com/en/1.3/topics/logging/#configuring-logging>`_
 
747
of the Django documentation.