• Home
  • Raw
  • Download

Lines Matching +full:python3 +full:- +full:paste

1 .. _logging-cookbook:
7 :Author: Vinay Sajip <vinay_sajip at red-dove dot com>
15 ---------------------------------
38 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
77 .. code-block:: none
79 2005-03-23 23:47:11,663 - spam_application - INFO -
81 2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -
83 2005-03-23 23:47:11,665 - spam_application - INFO -
85 2005-03-23 23:47:11,668 - spam_application - INFO -
87 2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -
89 2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -
91 2005-03-23 23:47:11,670 - spam_application - INFO -
93 2005-03-23 23:47:11,671 - spam_application - INFO -
95 2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -
97 2005-03-23 23:47:11,673 - spam_application - INFO -
101 -----------------------------
134 .. code-block:: none
136 0 Thread-1 Hi from myfunc
138 505 Thread-1 Hi from myfunc
140 1007 Thread-1 Hi from myfunc
142 1508 Thread-1 Hi from myfunc
143 2010 Thread-1 Hi from myfunc
145 2512 Thread-1 Hi from myfunc
147 3013 Thread-1 Hi from myfunc
148 3515 Thread-1 Hi from myfunc
150 4017 Thread-1 Hi from myfunc
152 4518 Thread-1 Hi from myfunc
158 --------------------------------
166 previous simple module-based configuration example::
179 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
196 The ability to create new handlers with higher- or lower-severity filters can be
204 .. _multiple-destinations:
207 --------------------------------
217 # set up logging to file - see previous section for more details
219 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
220 datefmt='%m-%d %H:%M',
227 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
249 .. code-block:: none
258 .. code-block:: none
260 10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.
261 10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
262 10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.
263 10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
264 10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
274 ----------------------------
308 properly preceded with the binary-encoded length, as the new logging
330 --------------------------------
341 performing mail or network infrastructure). But almost any network-based
346 One solution is to use a two-part approach. For the first part, attach only a
348 performance-critical threads. They simply write to their queue, which can be
352 in your code. If you are a library developer who has performance-critical
367 resource-friendly than, say, having threaded versions of the existing handler
372 que = queue.Queue(-1) # no limit on size
390 .. code-block:: none
404 .. _network-logging:
407 -----------------------------------------------------
456 Handle multiple requests - each expected to be a 4-byte length,
467 chunk = chunk + self.connection.recv(slen - len(chunk))
484 # is normally called AFTER logger-level filtering. If you want
491 Simple TCP socket-based logging receiver suitable for testing.
517 format='%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s')
528 .. code-block:: none
547 To run a logging listener in production, you may need to use a process-management tool
550 provides the bare-bones files to run the above functionality using Supervisor: you
555 .. _context-info:
558 ----------------------------------------------------
562 networked application, it may be desirable to log client-specific information
566 :class:`Logger` instances on a per-connection basis, this is not a good idea
586 :class:`Logger` instance and a dict-like object which contains your contextual
606 an 'extra' key in the keyword argument whose value is the dict-like object
610 The advantage of using 'extra' is that the values in the dict-like object are
613 the keys of the dict-like object. If you need a different method, e.g. if you
620 This example adapter expects the passed in dict-like object to have a
637 You don't need to pass an actual dict to a :class:`LoggerAdapter` - you could
643 .. _filters-contextual:
648 You can also add contextual information to log output using a user-defined
656 add, say, information from the request - say, the remote IP address and remote
657 user's username - to the ``LogRecord``, using the attribute names 'ip' and
685 … format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s')
701 .. code-block:: none
703 2010-09-06 22:38:15,292 a.b.c DEBUG IP: 123.231.231.123 User: fred A debug message
704 …2010-09-06 22:38:15,300 a.b.c INFO IP: 192.168.0.1 User: sheila An info message with som…
705 …2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1 User: sheila A message at CRITICAL le…
706 …2010-09-06 22:38:15,300 d.e.f ERROR IP: 127.0.0.1 User: jim A message at ERROR level…
707 …2010-09-06 22:38:15,300 d.e.f DEBUG IP: 127.0.0.1 User: sheila A message at DEBUG level…
708 …2010-09-06 22:38:15,300 d.e.f ERROR IP: 123.231.231.123 User: fred A message at ERROR level…
709 …2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 192.168.0.1 User: jim A message at CRITICAL le…
710 …2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1 User: sheila A message at CRITICAL le…
711 …2010-09-06 22:38:15,300 d.e.f DEBUG IP: 192.168.0.1 User: jim A message at DEBUG level…
712 …2010-09-06 22:38:15,301 d.e.f ERROR IP: 127.0.0.1 User: sheila A message at ERROR level…
713 …2010-09-06 22:38:15,301 d.e.f DEBUG IP: 123.231.231.123 User: fred A message at DEBUG level…
714 …2010-09-06 22:38:15,301 d.e.f INFO IP: 123.231.231.123 User: fred A message at INFO level …
717 .. _multiple-processes:
720 ------------------------------------------------
722 Although logging is thread-safe, and logging to a single file from multiple
731 :ref:`This section <network-logging>` documents this approach in more detail and
746 all logging events to one of the processes in your multi-process application.
751 thread rather than a separate listener process -- the implementation would be
780 f = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')
784 # This is the listener process top-level loop: wait for logging events
795 logger.handle(record) # No level or filter logic applied - just do it!
824 # This is the worker process top-level loop, which just logs ten events with
843 queue = multiprocessing.Queue(-1)
902 … 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
918 'filename': 'mplog-foo.log',
924 'filename': 'mplog-errors.log',
957 - e.g. the ``foo`` logger has a special handler which stores all events in the
958 ``foo`` subsystem in a file ``mplog-foo.log``. This will be used by the logging
969 .. code-block:: python
971 queue = multiprocessing.Queue(-1)
975 .. code-block:: python
977 queue = multiprocessing.Manager().Queue(-1) # also works with the examples above
1000 <https://uwsgi-docs.readthedocs.io/en/latest/>`_ (or similar), multiple worker
1002 file-based handlers directly in your web application. Instead, use a
1004 process. This can be set up using a process management tool such as Supervisor - see
1009 -------------------
1049 .. code-block:: none
1066 .. _format-styles:
1069 ------------------------------------
1072 formatting messages with variable content was to use the %-formatting
1087 .. code-block:: pycon
1099 2010-10-28 15:11:55,341 foo.bar DEBUG This is a DEBUG message
1101 2010-10-28 15:12:11,526 foo.bar CRITICAL This is a CRITICAL message
1106 2010-10-28 15:13:06,924 foo.bar DEBUG This is a DEBUG message
1108 2010-10-28 15:13:11,494 foo.bar CRITICAL This is a CRITICAL message
1113 That can still use %-formatting, as shown here::
1116 2010-10-28 15:19:29,833 foo.bar ERROR This is another, ERROR, message
1127 uses %-formatting to merge the format string and the variable arguments.
1129 all logging calls which are out there in existing code will be using %-format
1132 There is, however, a way that you can use {}- and $- formatting to construct
1156 Either of these can be used in place of a format string, to allow {}- or
1157 $-formatting to be used to build the actual "message" part which appears in the
1161 underscore --- not to be confused with _, the single underscore used as a
1165 copy and paste into your own code. They can be used as follows (assuming that
1168 .. code-block:: pycon
1235 .. _custom-logrecord:
1238 -------------------------
1281 top-level logger, but this would not be invoked if an application developer
1282 attached a handler to a lower-level library logger --- so output from that
1308 However, it should be borne in mind that each link in the chain adds run-time
1313 .. _zeromq-handlers:
1315 Subclassing QueueHandler - a ZeroMQ example
1316 -------------------------------------------
1354 Subclassing QueueListener - a ZeroMQ example
1355 --------------------------------------------
1384 :ref:`A basic logging tutorial <logging-basic-tutorial>`
1386 :ref:`A more advanced logging tutorial <logging-advanced-tutorial>`
1389 An example dictionary-based configuration
1390 -----------------------------------------
1392 Below is an example of a logging configuration dictionary - it's taken from
1393 …he Django project <https://docs.djangoproject.com/en/stable/topics/logging/#configuring-logging>`_.
1449 section <https://docs.djangoproject.com/en/stable/topics/logging/#configuring-logging>`_
1452 .. _cookbook-rotator-namer:
1455 --------------------------------------------------------------
1458 snippet, which shows zlib-based compression of the log file::
1480 ----------------------------------------
1497 Here's the script - the docstrings and the comments hopefully explain how it
1546 # would appear - hence the "if posix" clause.
1577 # would appear - hence the "if posix" clause.
1633 … 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
1637 'format': '%(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
1654 'filename': 'mplog-foo.log',
1660 'filename': 'mplog-errors.log',
1709 -----------------------------------------------------
1713 following structure: an optional pure-ASCII component, followed by a UTF-8 Byte
1714 Order Mark (BOM), followed by Unicode encoded using UTF-8. (See the
1715 :rfc:`relevant section of the specification <5424#section-6>`.)
1720 beginning of the message and hence not allowing any pure-ASCII component to
1725 want to produce :rfc:`5424`-compliant messages which include a BOM, an optional
1726 pure-ASCII sequence before it and arbitrary Unicode after it, encoded using
1727 UTF-8, then you need to do the following:
1735 The Unicode code point U+FEFF, when encoded using UTF-8, will be
1736 encoded as a UTF-8 BOM -- the byte-string ``b'\xef\xbb\xbf'``.
1740 way, it will remain unchanged after UTF-8 encoding).
1744 range, that's fine -- it will be encoded using UTF-8.
1746 The formatted message *will* be encoded using UTF-8 encoding by
1748 :rfc:`5424`-compliant messages. If you don't, logging may not complain, but your
1749 messages will not be RFC 5424-compliant, and your syslog daemon may complain.
1753 -------------------------------
1756 readily machine-parseable, there might be circumstances where you want to output
1761 which uses JSON to serialise the event in a machine-parseable manner::
1781 .. code-block:: none
1830 .. code-block:: none
1838 .. _custom-handlers:
1843 --------------------------------------------
1849 handlers in the stdlib don't offer built-in support. You can customize handler
1883 'encoding': 'utf-8',
1926 'encoding': 'utf-8',
1941 .. code-block:: shell-session
1943 $ sudo python3.3 chowntest.py
1945 2013-11-05 09:34:51,128 DEBUG mylogger A debug message
1946 $ ls -l chowntest.log
1947 -rw-r--r-- 1 pulse pulse 55 2013-11-05 09:34 chowntest.log
1951 supports :func:`dictConfig` - namely, Python 2.7, 3.2 or later. With pre-3.3
1955 In practice, the handler-creating function may be in a utility module somewhere
1970 types of file change - e.g. setting specific POSIX permission bits - in the
1974 :class:`~logging.FileHandler` - for example, one of the rotating file handlers,
1980 .. _formatting-styles:
1983 --------------------------------------------------------------
1999 because internally the logging package uses %-formatting to merge the format
2002 code will be using %-format strings.
2006 existing code could be using a given logger name and using %-formatting.
2008 For logging to work interoperably between any third-party libraries and your
2024 should be careful to support all formatting styles and allow %-formatting as
2035 There is another, perhaps simpler way that you can use {}- and $- formatting to
2037 :ref:`arbitrary-object-messages`) that when logging you can use an arbitrary
2060 Either of these can be used in place of a format string, to allow {}- or
2061 $-formatting to be used to build the actual "message" part which appears in the
2098 .. _filters-dictconfig:
2103 -------------------------------------------
2156 logging.debug('hello - noshow')
2162 .. code-block:: none
2173 in :ref:`logging-config-dict-externalobj`. For example, you could have used
2178 handlers and formatters. See :ref:`logging-config-dict-userdef` for more
2179 information on how logging supports using user-defined objects in its
2180 configuration, and see the other cookbook recipe :ref:`custom-handlers` above.
2183 .. _custom-format-exception:
2186 -------------------------------
2188 There might be times when you want to do customized exception formatting - for
2231 .. code-block:: none
2240 .. _spoken-messages:
2243 -------------------------
2247 text-to-speech (TTS) functionality available in your system, even if it doesn't have
2266 cmd = ['espeak', '-s150', '-ven+f3', msg]
2294 .. _buffered-logging:
2297 ------------------------------------------------------------
2310 - passed to another handler (the ``target`` handler) for processing. By default,
2319 parameter to ``foo`` which, if true, will log at ERROR and CRITICAL levels -
2395 .. code-block:: none
2434 .. _utc-formatting:
2437 --------------------------------------------------
2493 .. code-block:: none
2495 2015-10-17 12:53:29,501 The local time is Sat Oct 17 13:53:29 2015
2496 2015-10-17 13:53:29,501 The local time is Sat Oct 17 13:53:29 2015
2502 .. _context-manager:
2505 ---------------------------------------------
2544 block exit - you could do this if you don't need the handler any more.
2560 logger.debug('5. This should appear twice - once on stderr and once on stdout.')
2576 .. code-block:: shell-session
2581 5. This should appear twice - once on stderr and once on stdout.
2582 5. This should appear twice - once on stderr and once on stdout.
2588 .. code-block:: shell-session
2591 5. This should appear twice - once on stderr and once on stdout.
2595 .. code-block:: shell-session
2600 5. This should appear twice - once on stderr and once on stdout.
2610 .. _starter-template:
2613 ----------------------------------
2617 * Use a logging level based on command-line arguments
2622 Suppose we have a command-line application whose job is to stop, start or
2627 command-line argument, defaulting to ``logging.INFO``. Here's one way that
2640 parser.add_argument('--log-level', default='INFO', choices=levels)
2728 .. code-block:: shell-session
2745 .. code-block:: shell-session
2747 $ python app.py --log-level DEBUG start foo
2751 $ python app.py --log-level DEBUG stop foo bar
2755 $ python app.py --log-level DEBUG restart foo bar baz
2761 .. code-block:: shell-session
2763 $ python app.py --log-level WARNING start foo
2764 $ python app.py --log-level WARNING stop foo bar
2765 $ python app.py --log-level WARNING restart foo bar baz
2770 .. _qt-gui:
2773 --------------------
2777 cross-platform UI framework with Python bindings using `PySide2
2796 .. code-block:: python3
2830 # string is just a convenience - you could format a string for output any way
2848 # are named something like "Dummy-1". The function below gets the Qt name of the
2892 # * A read-only text edit window which holds formatted log messages
2922 fs = '%(asctime)s %(qThreadName)-12s %(levelname)-8s %(message)s'
2937 # Connect the non-worker slots and signals
3006 .. patterns-to-avoid:
3009 -----------------
3026 a copy/paste/forget-to-change error).
3046 and wasted debugging time - log entries end up in unexpected places, or are
3049 Use the techniques outlined in :ref:`multiple-processes` to circumvent such
3079 the :ref:`existing mechanisms <context-info>` for passing contextual
3082 more fine-grained than that).