[Python-Dev] Changes to logging

Vinay Sajip vinay_sajip@red-dove.com
Sat, 15 Feb 2003 12:30:11 -0000


I've got a set of patches which I want to make to logging:

1. Added the record being processed as a parameter to handleError.
2. Handler.handle returns the result of applying the filter to the record.
3. Added a seek(0, 2) in RotatingFileHandler before the tell() call. This
is because under Windows, tell() returns 0 until the first actual write.
4. Renamed warn and WARN to warning and WARNING. This may
break existing code, but the standard Python module will use warning/WARNING
rather than warn/WARN. The fatal and FATAL synonyms for critical and
CRITICAL
have also been removed.
5. Added a defaultEncoding attribute and some simple support for encoding
Unicode messages.
6. Added process ID to the list of LogRecord attributes.
7. Modified Logger.removeHandler so that it does not close the handler on
removal.

As far as point 4 is concerned, I feel that the last discussion on
python-dev was inconclusive. The consensus seemed to say WARNING and
CRITICAL were OK, but then there was some doubt about WARNING due to the
number of changes Zope would need. Though Zope is an important application,
I'm not sure Zope changes should be the yardstick for this sort of change;
>From looking for instances of WARN and warn in the .py files from a recent
read-only checkout of Zope, I couldn't see all that many places where
changes would be needed. Have I missed something?

For point 5, I'm not sure how best to handle Unicode support, so I'm
canvassing suggestions. The API allows overriding of the message formatting
logic at various stages - LogRecord, Handler or Formatter. This makes it
fairly easy to provide arbitrary custom logic for Unicode encoding, etc. The
change I've made allows for simple default processing - a "defaultEncoding"
attribute defaulted to "latin_1" has been added to logging. Currently, the
message format string is determined via LogRecord.getMessage(), which does a
str() on the passed in message argument passed to the logging call. Under
the proposed change:

- If the running Python has no Unicode support, the system does a str(), as
it does currently.
- If the message argument is not a Unicode string, the system does a str(),
as it does currently.
- If the message argument is a Unicode string (type(msg) ==
types.UnicodeType), the system does a msg.encode(defaultEncoding).

The resulting string is treated as the format string and the % operator
applied with the arguments to obtain the final message string.

Comments, please?

Vinay Sajip
Red Dove Consultants Ltd.
http://www.red-dove.com/