log.txt 9.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
.. role:: code(literal)
.. role:: file(literal)

.. XXX figure out how the code literals should be dealt with in sphinx. There is probably something builtin.

py.log documentation and musings


This document is an attempt to briefly state the actual specification of the
:code:`py.log` module.  It was written by Francois Pinard and also contains
some ideas for enhancing the py.log facilities.

NOTE that :code:`py.log` is subject to refactorings, it may change with
the next release.

This document is meant to trigger or facilitate discussions.  It shamelessly
steals from the `Agile Testing`__ comments, and from other sources as well,
without really trying to sort them out.

__ http://agiletesting.blogspot.com/2005/06/keyword-based-logging-with-py-library.html

Logging organisation

The :code:`py.log` module aims a niche comparable to the one of the
`logging module`__ found within the standard Python distributions, yet
with much simpler paradigms for configuration and usage.

__ http://www.python.org/doc/2.4.2/lib/module-logging.html

Holger Krekel, the main :code:`py` library developer, introduced
the idea of keyword-based logging and the idea of logging *producers* and
*consumers*.  A log producer is an object used by the application code
to send messages to various log consumers.  When you create a log
producer, you define a set of keywords that are then used to both route
the logging messages to consumers, and to prefix those messages.

In fact, each log producer has a few keywords associated with it for
identification purposes.  These keywords form a tuple of strings, and
may be used to later retrieve a particular log producer.

A log producer may (or may not) be associated with a log consumer, meant
to handle log messages in particular ways.  The log consumers can be
``STDOUT``, ``STDERR``, log files, syslog, the Windows Event Log, user
defined functions, etc.  (Yet, logging to syslog or to the Windows Event
Log is only future plans for now).  A log producer has never more than
one consumer at a given time, but it is possible to dynamically switch
a producer to use another consumer.  On the other hand, a single log
consumer may be associated with many producers.

Note that creating and associating a producer and a consumer is done
automatically when not otherwise overriden, so using :code:`py` logging
is quite comfortable even in the smallest programs.  More typically,
the application programmer will likely design a hierarchy of producers,
and will select keywords appropriately for marking the hierarchy tree.
If a node of the hierarchical tree of producers has to be divided in
sub-trees, all producers in the sub-trees share, as a common prefix, the
keywords of the node being divided.  In other words, we go further down
in the hierarchy of producers merely by adding keywords.

Using the py.log library

To use the :code:`py.log` library, the user must import it into a Python
application, create at least one log producer and one log consumer, have
producers and consumers associated, and finally call the log producers
as needed, giving them log messages.


Once the :code:`py` library is installed on your system, a mere::

  import py

holds enough magic for lazily importing the various facilities of the
:code:`py` library when they are first needed.  This is really how
:code:`py.log` is made available to the application.  For example, after
the above ``import py``, one may directly write ``py.log.Producer(...)``
and everything should work fine, the user does not have to worry about
specifically importing more modules.

Creating a producer

There are three ways for creating a log producer instance:

  + As soon as ``py.log`` is first evaluated within an application
    program, a default log producer is created, and made available under
    the name ``py.log.default``.  The keyword ``default`` is associated
    with that producer.

  + The ``py.log.Producer()`` constructor may be explicitly called
    for creating a new instance of a log producer.  That constructor
    accepts, as an argument, the keywords that should be associated with
    that producer.  Keywords may be given either as a tuple of keyword
    strings, or as a single space-separated string of keywords.

  + Whenever an attribute is *taken* out of a log producer instance,
    for the first time that attribute is taken, a new log producer is
    created.  The keywords associated with that new producer are those
    of the initial producer instance, to which is appended the name of
    the attribute being taken.

The last point is especially useful, as it allows using log producers
without further declarations, merely creating them *on-the-fly*.

Creating a consumer

There are many ways for creating or denoting a log consumer:

  + A default consumer exists within the ``py.log`` facilities, which
    has the effect of writing log messages on the Python standard output
    stream.  That consumer is associated at the very top of the producer
    hierarchy, and as such, is called whenever no other consumer is

  + The notation ``py.log.STDOUT`` accesses a log consumer which writes
    log messages on the Python standard output stream.

  + The notation ``py.log.STDERR`` accesses a log consumer which writes
    log messages on the Python standard error stream.

  + The ``py.log.File()`` constructor accepts, as argument, either a file
    already opened in write mode or any similar file-like object, and
    creates a log consumer able to write log messages onto that file.

  + The ``py.log.Path()`` constructor accepts a file name for its first
    argument, and creates a log consumer able to write log messages into
    that file.  The constructor call accepts a few keyword parameters:

      + ``append``, which is ``False`` by default, may be used for
        opening the file in append mode instead of write mode.

      + ``delayed_create``, which is ``False`` by default, maybe be used
        for opening the file at the latest possible time.  Consequently,
        the file will not be created if it did not exist, and no actual
        log message gets written to it.

      + ``buffering``, which is 1 by default, is used when opening the
        file.  Buffering can be turned off by specifying a 0 value.  The
        buffer size may also be selected through this argument.

  + Any user defined function may be used for a log consumer.  Such a
    function should accept a single argument, which is the message to
    write, and do whatever is deemed appropriate by the programmer.
    When the need arises, this may be an especially useful and flexible

  + The special value ``None`` means no consumer at all.  This acts just
    like if there was a consumer which would silently discard all log
    messages sent to it.

Associating producers and consumers

Each log producer may have at most one log consumer associated with
it.  A log producer gets associated with a log consumer through a
``py.log.setconsumer()`` call.  That function accepts two arguments,
the first identifying a producer (a tuple of keyword strings or a single
space-separated string of keywords), the second specifying the precise
consumer to use for that producer.  Until this function is called for a
producer, that producer does not have any explicit consumer associated
with it.

Now, the hierarchy of log producers establishes which consumer gets used
whenever a producer has no explicit consumer.  When a log producer
has no consumer explicitly associated with it, it dynamically and
recursively inherits the consumer of its parent node, that is, that node
being a bit closer to the root of the hierarchy.  In other words, the
rightmost keywords of that producer are dropped until another producer
is found which has an explicit consumer.  A nice side-effect is that,
by explicitly associating a consumer with a producer, all consumer-less
producers which appear under that producer, in the hierarchy tree,
automatically *inherits* that consumer.

Writing log messages

All log producer instances are also functions, and this is by calling
them that log messages are generated.  Each call to a producer object
produces the text for one log entry, which in turn, is sent to the log
consumer for that producer.

The log entry displays, after a prefix identifying the log producer
being used, all arguments given in the call, converted to strings and
space-separated.  (This is meant by design to be fairly similar to what
the ``print`` statement does in Python).  The prefix itself is made up
of a colon-separated list of keywords associated with the producer, the
whole being set within square brackets.

Note that the consumer is responsible for adding the newline at the end
of the log entry.  That final newline is not part of the text for the
log entry.

.. Other details
.. -------------
.. XXX: fill in details
.. + Should speak about pickle-ability of :code:`py.log`.
.. + What is :code:`log.get` (in :file:`logger.py`)?