This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch v2-6-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit b0bd39eeb1e81fff1c7013aaa4fce3bc70cbc47a
Author: Jarek Potiuk <[email protected]>
AuthorDate: Wed Apr 19 14:00:34 2023 +0200

    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.
    
    (cherry picked from commit 29bd9bf9d46bf072fec4606b0059d1bd3b85bf81)
---
 .../logging-monitoring/logging-tasks.rst           | 33 ++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git 
a/docs/apache-airflow/administration-and-deployment/logging-monitoring/logging-tasks.rst
 
b/docs/apache-airflow/administration-and-deployment/logging-monitoring/logging-tasks.rst
index f3da6dbabe..b43b858ac9 100644
--- 
a/docs/apache-airflow/administration-and-deployment/logging-monitoring/logging-tasks.rst
+++ 
b/docs/apache-airflow/administration-and-deployment/logging-monitoring/logging-tasks.rst
@@ -54,6 +54,39 @@ These patterns can be adjusted by 
:ref:`config:logging__log_filename_template`.
 
 In addition, you can supply a remote location to store current logs and 
backups.
 
+Writing to task logs from your code
+-----------------------------------
+
+Most operators will write logs to the task log automatically. This is because 
they derive from they
+have a ``log`` logger that you can use to write to the task log.
+This logger is created and configured by 
:class:`~airflow.utils.log.LoggingMixin` that all classic
+operators derive from.
+
+If you want to log to the task log from a custom class of yours you can do the 
following:
+
+* make sure your class extends from :class:`~airflow.utils.log.LoggingMixin`
+* just use standard print statements to print to stdout
+* use the ``airflow.task`` logger that is configured by default or create 
logger where ``airflow.task`` is
+  the parent logger
+
+The last option is the most flexible, as you can create your own logger and 
configure it as you see fit
+via advanced configuration options below.
+
+Using task logger directly:
+
+.. code-block:: python
+
+  logger = logging.getLogger("airflow.task")
+  logger.info("This is a log message")
+
+Using child logger of task logger:
+
+.. code-block:: python
+
+  child_logger = logging.getLogger("airflow.task.child")
+  child_logger.info("This is a child log message")
+
+
 Interleaving of logs
 --------------------
 

Reply via email to