File: logging.rst

package info (click to toggle)
python-openleadr-python 0.5.34%2Bdfsg.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,496 kB
  • sloc: python: 6,942; xml: 663; makefile: 32; sh: 18
file content (66 lines) | stat: -rw-r--r-- 1,655 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
=======
Logging
=======

OpenLEADR uses the standard Python Logging facility. Following the `Python guidelines <https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library>`_, no default handlers are configured, but you can easily get going if you want to:

Log to standard output
----------------------

To print logs to your standard output, you can use the following convenience method:

.. code-block:: python3

    import openleadr
    import logging
    openleadr.enable_default_logging(level=logging.INFO)

Which is the same as:

.. code-block:: python3

    import logging
    logger = logging.getLogger('openleadr')
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    logger.addHandler(handler)


Setting the logging level
-------------------------

You can set different logging levels for the logging generated by OpenLEADR:

.. code-block:: python3

    import logging
    logger = logging.getLogger('openleadr')
    logger.setLevel(logging.WARNING)

The different `logging levels <https://docs.python.org/3/library/logging.html#levels>`_ are:

.. code-block:: python3

    logging.DEBUG
    logging.INFO
    logging.WARNING
    logging.ERROR
    logging.CRITICAL

Redirecting logs to a different file
------------------------------------

To write logs to a file:

.. code-block:: python3

    import logging
    logger = logging.getLogger('openleadr')

    handler = logging.FileHandler('mylogfile.txt')
    logger.addHandler(logger)

More info
---------

Detailed info on logging configuration can be found on `the official Python documentation <https://docs.python.org/3/library/logging.html>`_.