1299
1299
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
1300
1300
logger2.error('The five boxing wizards jump quickly.')
1302
At the receiving end, you can set up a receiver using the :mod:`socketserver`
1302
At the receiving end, you can set up a receiver using the :mod:`SocketServer`
1303
1303
module. Here is a basic working example::
1307
1307
import logging.handlers
1312
class LogRecordStreamHandler(socketserver.StreamRequestHandler):
1312
class LogRecordStreamHandler(SocketServer.StreamRequestHandler):
1313
1313
"""Handler for a streaming logging request.
1315
1315
This basically logs the record using whatever logging policy is
1351
1351
# cycles and network bandwidth!
1352
1352
logger.handle(record)
1354
class LogRecordSocketReceiver(socketserver.ThreadingTCPServer):
1354
class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer):
1355
1355
"""simple TCP socket-based logging receiver suitable for testing.
1360
1360
def __init__(self, host='localhost',
1361
1361
port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
1362
1362
handler=LogRecordStreamHandler):
1363
socketserver.ThreadingTCPServer.__init__(self, (host, port), handler)
1363
SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
1365
1365
self.timeout = 1
1366
1366
self.logname = None
1645
1645
timed intervals.
1648
.. class:: TimedRotatingFileHandler(filename [,when [,interval [,backupCount[, encoding[, delay]]]]])
1648
.. class:: TimedRotatingFileHandler(filename [,when [,interval [,backupCount[, encoding[, delay[, utc]]]]]])
1650
1650
Returns a new instance of the :class:`TimedRotatingFileHandler` class. The
1651
1651
specified file is opened and used as the stream for logging. On rotating it also
1655
1655
You can use the *when* to specify the type of *interval*. The list of possible
1656
values is, note that they are not case sensitive:
1656
values is below. Note that they are not case sensitive.
1658
1658
+----------------+-----------------------+
1659
1659
| Value | Type of interval |
1674
1674
The system will save old log files by appending extensions to the filename.
1675
1675
The extensions are date-and-time based, using the strftime format
1676
1676
``%Y-%m-%d_%H-%M-%S`` or a leading portion thereof, depending on the
1677
rollover interval. If *backupCount* is nonzero, at most *backupCount* files
1678
If the *utc* argument is true, times in UTC will be used; otherwise
1681
If *backupCount* is nonzero, at most *backupCount* files
1678
1682
will be kept, and if more would be created when rollover occurs, the oldest
1679
1683
one is deleted. The deletion logic uses the interval to determine which
1680
1684
files to delete, so changing the interval may leave old files lying around.
2241
2245
.. function:: fileConfig(fname[, defaults])
2243
Reads the logging configuration from a :mod:`configparser`\-format file named
2244
*fname*. This function can be called several times from an application,
2245
allowing an end user the ability to select from various pre-canned
2246
configurations (if the developer provides a mechanism to present the choices
2247
and load the chosen configuration). Defaults to be passed to the ConfigParser
2248
can be specified in the *defaults* argument.
2247
Reads the logging configuration from a ConfigParser-format file named *fname*.
2248
This function can be called several times from an application, allowing an end
2249
user the ability to select from various pre-canned configurations (if the
2250
developer provides a mechanism to present the choices and load the chosen
2251
configuration). Defaults to be passed to ConfigParser can be specified in the
2252
*defaults* argument.
2251
2255
.. function:: listen([port])
2275
2279
Configuration file format
2276
2280
^^^^^^^^^^^^^^^^^^^^^^^^^
2278
The configuration file format understood by :func:`fileConfig` is
2279
based on :mod:`configparser` functionality. The file must contain
2280
sections called ``[loggers]``, ``[handlers]`` and ``[formatters]``
2281
which identify by name the entities of each type which are defined in
2282
the file. For each such entity, there is a separate section which
2283
identified how that entity is configured. Thus, for a logger named
2284
``log01`` in the ``[loggers]`` section, the relevant configuration
2285
details are held in a section ``[logger_log01]``. Similarly, a handler
2286
called ``hand01`` in the ``[handlers]`` section will have its
2287
configuration held in a section called ``[handler_hand01]``, while a
2288
formatter called ``form01`` in the ``[formatters]`` section will have
2289
its configuration specified in a section called
2290
``[formatter_form01]``. The root logger configuration must be
2291
specified in a section called ``[logger_root]``.
2282
The configuration file format understood by :func:`fileConfig` is based on
2283
ConfigParser functionality. The file must contain sections called ``[loggers]``,
2284
``[handlers]`` and ``[formatters]`` which identify by name the entities of each
2285
type which are defined in the file. For each such entity, there is a separate
2286
section which identified how that entity is configured. Thus, for a logger named
2287
``log01`` in the ``[loggers]`` section, the relevant configuration details are
2288
held in a section ``[logger_log01]``. Similarly, a handler called ``hand01`` in
2289
the ``[handlers]`` section will have its configuration held in a section called
2290
``[handler_hand01]``, while a formatter called ``form01`` in the
2291
``[formatters]`` section will have its configuration specified in a section
2292
called ``[formatter_form01]``. The root logger configuration must be specified
2293
in a section called ``[logger_root]``.
2293
2295
Examples of these sections in the file are given below. ::