Skip to content

Commit 1000d6b

Browse files
potiukCloud Composer Team
authored and
Cloud Composer Team
committed
Add explicit information about how to write task logs (#30732)
There was no explicit information in our documentation on how to write logs from your tasks. While for classic operators, that is easy and straightforward as they all have log property which is the right logger coming from LoggingMixin, for taskflow code and custom classes it is is not straightforward that you have to use `airflow.task` logger (or a child of it) or that you have extend LoggingMixin to use the built-in logging configuration. GitOrigin-RevId: 29bd9bf9d46bf072fec4606b0059d1bd3b85bf81
1 parent 2ebefbb commit 1000d6b

File tree

1 file changed

+33
-0
lines changed
  • docs/apache-airflow/administration-and-deployment/logging-monitoring

1 file changed

+33
-0
lines changed

docs/apache-airflow/administration-and-deployment/logging-monitoring/logging-tasks.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ These patterns can be adjusted by :ref:`config:logging__log_filename_template`.
5454

5555
In addition, you can supply a remote location to store current logs and backups.
5656

57+
Writing to task logs from your code
58+
-----------------------------------
59+
60+
Most operators will write logs to the task log automatically. This is because they derive from they
61+
have a ``log`` logger that you can use to write to the task log.
62+
This logger is created and configured by :class:`~airflow.utils.log.LoggingMixin` that all classic
63+
operators derive from.
64+
65+
If you want to log to the task log from a custom class of yours you can do the following:
66+
67+
* make sure your class extends from :class:`~airflow.utils.log.LoggingMixin`
68+
* just use standard print statements to print to stdout
69+
* use the ``airflow.task`` logger that is configured by default or create logger where ``airflow.task`` is
70+
the parent logger
71+
72+
The last option is the most flexible, as you can create your own logger and configure it as you see fit
73+
via advanced configuration options below.
74+
75+
Using task logger directly:
76+
77+
.. code-block:: python
78+
79+
logger = logging.getLogger("airflow.task")
80+
logger.info("This is a log message")
81+
82+
Using child logger of task logger:
83+
84+
.. code-block:: python
85+
86+
child_logger = logging.getLogger("airflow.task.child")
87+
child_logger.info("This is a child log message")
88+
89+
5790
Interleaving of logs
5891
--------------------
5992

0 commit comments

Comments
 (0)