Lines Matching full:logging
4 Logging Cookbook
9 This page contains a number of recipes related to logging, which have been found
12 .. currentmodule:: logging
14 Using logging in multiple modules
17 Multiple calls to ``logging.getLogger('someLogger')`` return a reference to the
25 import logging
29 logger = logging.getLogger('spam_application')
30 logger.setLevel(logging.DEBUG)
32 fh = logging.FileHandler('spam.log')
33 fh.setLevel(logging.DEBUG)
35 ch = logging.StreamHandler()
36 ch.setLevel(logging.ERROR)
38 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
57 import logging
60 module_logger = logging.getLogger('spam_application.auxiliary')
64 self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')
98 Logging from multiple threads
101 Logging from multiple threads requires no special effort. The following example
102 shows logging from the main (initIal) thread and another thread::
104 import logging
110 logging.debug('Hi from myfunc')
114 … logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')
120 logging.debug('Hello from main')
150 This shows the logging output interspersed as one might expect. This approach
159 text file while simultaneously logging errors or above to the console. To set
160 this up, simply configure the appropriate handlers. The logging calls in the
164 import logging
166 logger = logging.getLogger('simple_example')
167 logger.setLevel(logging.DEBUG)
169 fh = logging.FileHandler('spam.log')
170 fh.setLevel(logging.DEBUG)
172 ch = logging.StreamHandler()
173 ch.setLevel(logging.ERROR)
175 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
202 Logging to multiple destinations
211 import logging
213 # set up logging to file - see previous section for more details
214 logging.basicConfig(level=logging.DEBUG,
220 console = logging.StreamHandler()
221 console.setLevel(logging.INFO)
223 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
227 logging.getLogger('').addHandler(console)
230 logging.info('Jackdaws love my big sphinx of quartz.')
235 logger1 = logging.getLogger('myapp.area1')
236 logger2 = logging.getLogger('myapp.area2')
268 Here is an example of a module using the logging configuration server::
270 import logging
271 import logging.config
276 logging.config.fileConfig('logging.conf')
279 t = logging.config.listen(9999)
282 logger = logging.getLogger('simpleExample')
285 # loop through logging calls to see the difference
296 logging.config.stopListening()
300 properly preceded with the binary-encoded length, as the new logging
321 .. _network-logging:
323 Sending and receiving logging events across a network
326 Let's say you want to send logging events across a network, and handle them at
330 import logging, logging.handlers
332 rootLogger = logging.getLogger('')
333 rootLogger.setLevel(logging.DEBUG)
334 socketHandler = logging.handlers.SocketHandler('localhost',
335 logging.handlers.DEFAULT_TCP_LOGGING_PORT)
341 logging.info('Jackdaws love my big sphinx of quartz.')
346 logger1 = logging.getLogger('myapp.area1')
347 logger2 = logging.getLogger('myapp.area2')
358 import logging
359 import logging.handlers
365 """Handler for a streaming logging request.
367 This basically logs the record using whatever logging policy is
386 record = logging.makeLogRecord(obj)
399 logger = logging.getLogger(name)
408 Simple TCP socket-based logging receiver suitable for testing.
414 port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
433 logging.basicConfig(
461 Adding contextual information to your logging output
464 .. currentmodule:: logging
466 Sometimes you want logging output to contain contextual information in
467 addition to the parameters passed to the logging call. For example, in a
475 level of granularity you want to use in logging an application, it could
484 with logging event information is to use the :class:`LoggerAdapter` class.
493 information. When you call one of the logging methods on an instance of
508 contextual information is added to the logging output. It's passed the message
509 and keyword arguments of the logging call, and it passes back (potentially)
524 class CustomAdapter(logging.LoggerAdapter):
534 logger = logging.getLogger(__name__)
545 that it looks like a dict to logging. This would be useful if you want to
568 import logging
571 class ContextFilter(logging.Filter):
589 levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
590 logging.basicConfig(level=logging.DEBUG,
592 a1 = logging.getLogger('a.b.c')
593 a2 = logging.getLogger('d.e.f')
602 lvlname = logging.getLevelName(lvl)
623 Logging to a single file from multiple processes
626 Although logging is thread-safe, and logging to a single file from multiple
627 threads in a single process *is* supported, logging to a single file from
635 :ref:`This section <network-logging>` documents this approach in more detail and
653 .. (see <https://pymotw.com/3/logging/>)
659 logging package provides a :class:`~handlers.RotatingFileHandler`::
662 import logging
663 import logging.handlers
668 my_logger = logging.getLogger('MyLogger')
669 my_logger.setLevel(logging.DEBUG)
672 handler = logging.handlers.RotatingFileHandler(
708 Below is an example of a logging configuration dictionary - it's taken from
709 …n the Django project <https://docs.djangoproject.com/en/1.9/topics/logging/#configuring-logging>`_.
712 LOGGING = {
725 '()': 'project.logging.SpecialFilter',
736 'class':'logging.StreamHandler',
765 section <https://docs.djangoproject.com/en/1.9/topics/logging/#configuring-logging>`_
778 :class:`~logging.handlers.SysLogHandler` to insert a BOM into the message, but
789 #. Attach a :class:`~logging.Formatter` instance to your
790 :class:`~logging.handlers.SysLogHandler` instance, with a format string
808 produce RFC 5424-compliant messages. If you don't, logging may not complain,
813 Implementing structured logging
816 Although most logging messages are intended for reading by humans, and thus not
820 straightforward to achieve using the logging package. There are a number of
825 import logging
837 logging.basicConfig(level=logging.INFO, format='%(message)s')
838 logging.info(_('message 1', foo='bar', bar='baz', num=123, fnum=123.456))
853 import logging
881 logging.basicConfig(level=logging.INFO, format='%(message)s')
882 logging.info(_('message 1', set_value=set([1, 2, 3]), snowman='\u2603'))
897 .. currentmodule:: logging.config
902 There are times when you want to customize logging handlers in particular ways,
919 return logging.FileHandler(filename, mode, encoding)
921 You can then specify, in a logging configuration passed to :func:`dictConfig`,
922 that a logging handler be created by calling this function::
924 LOGGING = {
958 import logging, logging.config, os, shutil
965 return logging.FileHandler(filename, mode, encoding)
967 LOGGING = {
997 logging.config.dictConfig(LOGGING)
998 logger = logging.getLogger('mylogger')
1036 :class:`~logging.FileHandler` - for example, one of the rotating file handlers,
1045 You *can* configure filters using :func:`~logging.config.dictConfig`, though it
1047 :class:`~logging.Filter` is the only filter class included in the standard
1049 base class), you will typically need to define your own :class:`~logging.Filter`
1050 subclass with an overridden :meth:`~logging.Filter.filter` method. To do this,
1054 :class:`~logging.Filter` instance). Here is a complete example::
1056 import logging
1057 import logging.config
1060 class MyFilter(logging.Filter):
1073 LOGGING = {
1083 'class': 'logging.StreamHandler',
1094 logging.config.dictConfig(LOGGING)
1095 logging.debug('hello')
1096 logging.debug('hello - noshow')
1111 in :ref:`logging-config-dict-externalobj`. For example, you could have used
1116 handlers and formatters. See :ref:`logging-config-dict-userdef` for more
1117 information on how logging supports using user-defined objects in its
1131 import logging
1133 class OneLineExceptionFormatter(logging.Formatter):
1148 fh = logging.FileHandler('output.txt', 'w')
1152 root = logging.getLogger()
1153 root.setLevel(logging.DEBUG)
1158 logging.info('Sample message')
1162 logging.exception('ZeroDivisionError: %s', e)
1178 Speaking logging messages
1181 There might be situations when it is desirable to have logging messages rendered
1194 import logging
1198 class TTSHandler(logging.Handler):
1210 root = logging.getLogger()
1213 root.setLevel(logging.DEBUG)
1216 logging.info('Hello')
1217 logging.debug('Goodbye')
1229 .. _buffered-logging:
1231 Buffering logging messages and outputting them conditionally
1236 start logging debug events in a function, and if the function completes without
1242 functions where you want logging to behave this way. It makes use of the
1243 :class:`logging.handlers.MemoryHandler`, which allows buffering of logged events
1252 all the logging levels, writing to ``sys.stderr`` to say what level it's about
1253 to log at, and then actually logging a message at that level. You can pass a
1258 conditional logging that's required. The decorator takes a logger as a parameter
1262 default to a :class:`~logging.StreamHandler` which writes to ``sys.stderr``,
1263 ``logging.ERROR`` and ``100`` respectively.
1267 import logging
1268 from logging.handlers import MemoryHandler
1271 logger = logging.getLogger(__name__)
1272 logger.addHandler(logging.NullHandler())
1276 target_handler = logging.StreamHandler()
1278 flush_level = logging.ERROR
1318 logger.setLevel(logging.DEBUG)
1356 As you can see, actual logging output only occurs when an event is logged whose
1375 import logging
1378 class UTCFormatter(logging.Formatter):
1382 :class:`~logging.Formatter`. If you want to do that via configuration, you can
1383 use the :func:`~logging.config.dictConfig` API with an approach illustrated by
1386 import logging
1387 import logging.config
1390 class UTCFormatter(logging.Formatter):
1393 LOGGING = {
1407 'class': 'logging.StreamHandler',
1411 'class': 'logging.StreamHandler',
1421 logging.config.dictConfig(LOGGING)
1422 logging.warning('The local time is %s', time.asctime())
1435 Using a context manager for selective logging
1438 There are times when it would be useful to temporarily change the logging
1440 manager is the most obvious way of saving and restoring the logging context.
1442 optionally change the logging level and add a logging handler purely in the
1445 import logging
1481 logger = logging.getLogger('foo')
1482 logger.addHandler(logging.StreamHandler())
1483 logger.setLevel(logging.INFO)
1486 with LoggingContext(logger, level=logging.DEBUG):
1489 h = logging.StreamHandler(sys.stdout)
1490 with LoggingContext(logger, level=logging.DEBUG, handler=h, close=True):
1537 logging filters temporarily. Note that the above code works in Python 2 as well