You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2023/04/20 00:18:54 UTC

[airflow] branch main updated: Better explanation on how to log from tasks (#30746)

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

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 82ba645abb Better explanation on how to log from tasks (#30746)
82ba645abb is described below

commit 82ba645abb46c12f6b81e4df2af94abe2fc03fea
Author: Jarek Potiuk <ja...@potiuk.com>
AuthorDate: Thu Apr 20 02:18:44 2023 +0200

    Better explanation on how to log from tasks (#30746)
    
    * Better explanation on how to log from tasks
    
    After Daniel's explanation this should provide a better description
    on how to log from tasks.
    
    Co-authored-by: Daniel Standish <15...@users.noreply.github.com>
    Co-authored-by: Niko Oliveira <on...@amazon.com>
---
 .../logging-monitoring/logging-tasks.rst           | 36 ++++++++++------------
 1 file changed, 16 insertions(+), 20 deletions(-)

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 b43b858ac9..16b2b58e98 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
@@ -57,34 +57,30 @@ 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.
+Airflow uses standard the Python `logging <https://docs.python.org/3/library/logging.html>`_ framework to
+write logs, and for the duration of a task, the root logger is configured to write to the task's log.
 
-If you want to log to the task log from a custom class of yours you can do the following:
+Most operators will write logs to the task log automatically. This is because 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
+operators derive from. But also due to the root logger handling, any standard logger (using default settings) that
+propagates logging to the root will also write to the task log.
 
-* 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
+So if you want to log to the task log from custom code of yours you can do any of the following:
 
-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.
+* Log with the ``self.log`` logger from BaseOperator
+* Use standard ``print`` statements to print to ``stdout`` (not recommended, but in some cases it can be useful)
+* Use the standard logger approach of creating a logger using the Python module name
+  and using it to write to the task log
 
-Using task logger directly:
+This is the usual way loggers are used directly in Python code:
 
 .. code-block:: python
 
-  logger = logging.getLogger("airflow.task")
-  logger.info("This is a log message")
-
-Using child logger of task logger:
+  import logging
 
-.. code-block:: python
-
-  child_logger = logging.getLogger("airflow.task.child")
-  child_logger.info("This is a child log message")
+  logger = logging.getLogger(__name__)
+  logger.info("This is a log message")
 
 
 Interleaving of logs