File: messages.rst

package info (click to toggle)
python-eliot 1.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 8,641; makefile: 151
file content (35 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
.. _messages:

Messages
========

Sometimes you don't want to generate actions. sometimes you just want an individual isolated message, the way traditional logging systems work.
Here's how to do that.

When you have an action
-----------------------

If you already have an action object, you can log a message in that action's context:

.. code-block:: python

    from eliot import start_action

    class YourClass(object):
        def run(self):
            with start_action(action_type="myaction") as ctx:
                ctx.log(message_type="mymessage", key="abc", key2=4)

If you don't have an action
---------------------------

If you don't have a reference to an action, or you're worried the function will sometimes be called outside the context of any action at all, you can use ``log_message``:

.. code-block:: python

    from eliot import log_message

    def run(x):
        log_message(message_type="in_run", xfield=x)

The main downside to using this function is that it's a little slower, since it needs to handle the case where there is no action in context.