python-application: add `logging.basicConfig()` to example
[lttng-docs.git] / contents / using-lttng / instrumenting / python-application.md
CommitLineData
024571e4
PP
1---
2id: python-application
3since: 2.7
4---
5
6Python 2 and Python 3 applications using the standard
7<a href="https://docs.python.org/3/howto/logging.html" class="ext"><code>logging</code> module</a>
8can be traced by LTTng using the LTTng-UST Python agent.
9
10Import the `lttngust` package in your Python application. For example:
11
12~~~ python
13import lttngust
14import logging
d7976b2f 15import time
024571e4
PP
16
17
18def example():
d302f5b1 19 logging.basicConfig()
024571e4
PP
20 logger = logging.getLogger('my-logger')
21
22 while True:
23 logger.debug('debug message')
24 logger.info('info message')
25 logger.warn('warn message')
26 logger.error('error message')
27 logger.critical('critical message')
28 time.sleep(1)
29
30
31if __name__ == '__main__':
32 example()
33~~~
34
35Importing `lttngust` adds a logging handler which emits LTTng-UST
36events. You do not need to get a special logger for tracing to work.
37
d302f5b1
PP
38Note that `logging.basicConfig()`, which adds to the root logger a basic
39logging handler which prints to the standard error stream, is not
40strictly required for LTTng-UST tracing to work, but in versions of
41Python preceding 3.2, a warning message could be seen indicating that no
42handler exists for the logger `my-logger`.
43
024571e4
PP
44Use the `--python` option of the `lttng enable-event`,
45`lttng disable-event`, and `lttng list` commands to target
46Python applications. For example, here's how to enable the events
47produced by the Python logger above:
48
49<pre class="term">
50lttng enable-event --python my-logger
51</pre>
52
53Standard Python log levels are supported using the `PYTHON_` prefix.
54For example, here's how to enable the warning (and more important)
55events produced by the Python logger above:
56
57<pre class="term">
58lttng enable-event --python my-logger --loglevel PYTHON_WARNING
59</pre>
60
61See [Enabling and disabling events](#doc-enabling-disabling-events) for
62more options.
63
64When loading, the LTTng-UST Python agent tries to register to the
65[session daemon](#doc-lttng-sessiond). Note that the session daemon
66needs to be started _before_ the Python application is started. If a
67session daemon is found, the agent tries to register to it during
685&nbsp;seconds, after which the application continues without LTTng
69tracing support. This timeout value is overriden by the
70`LTTNG_UST_PYTHON_REGISTER_TIMEOUT` environment variable (milliseconds).
71
72If the session daemon stops while a registered Python application is
73registered, the application retries to connect and register to a session
74daemon every 3&nbsp;seconds. This timeout value is overridden by the
75`LTTNG_UST_PYTHON_REGISTER_RETRY_DELAY` environment variable.
This page took 0.025127 seconds and 4 git commands to generate.