pid-tracking: copy+paste error in lttng untrack --pid --all
[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
15
16
17def example():
18 logger = logging.getLogger('my-logger')
19
20 while True:
21 logger.debug('debug message')
22 logger.info('info message')
23 logger.warn('warn message')
24 logger.error('error message')
25 logger.critical('critical message')
26 time.sleep(1)
27
28
29if __name__ == '__main__':
30 example()
31~~~
32
33Importing `lttngust` adds a logging handler which emits LTTng-UST
34events. You do not need to get a special logger for tracing to work.
35
36Use the `--python` option of the `lttng enable-event`,
37`lttng disable-event`, and `lttng list` commands to target
38Python applications. For example, here's how to enable the events
39produced by the Python logger above:
40
41<pre class="term">
42lttng enable-event --python my-logger
43</pre>
44
45Standard Python log levels are supported using the `PYTHON_` prefix.
46For example, here's how to enable the warning (and more important)
47events produced by the Python logger above:
48
49<pre class="term">
50lttng enable-event --python my-logger --loglevel PYTHON_WARNING
51</pre>
52
53See [Enabling and disabling events](#doc-enabling-disabling-events) for
54more options.
55
56When loading, the LTTng-UST Python agent tries to register to the
57[session daemon](#doc-lttng-sessiond). Note that the session daemon
58needs to be started _before_ the Python application is started. If a
59session daemon is found, the agent tries to register to it during
605&nbsp;seconds, after which the application continues without LTTng
61tracing support. This timeout value is overriden by the
62`LTTNG_UST_PYTHON_REGISTER_TIMEOUT` environment variable (milliseconds).
63
64If the session daemon stops while a registered Python application is
65registered, the application retries to connect and register to a session
66daemon every 3&nbsp;seconds. This timeout value is overridden by the
67`LTTNG_UST_PYTHON_REGISTER_RETRY_DELAY` environment variable.
This page took 0.027825 seconds and 4 git commands to generate.