logging.mdx
  1  ---
  2  title: "Logging"
  3  id: logging
  4  slug: "/logging"
  5  description: "Logging is crucial for monitoring and debugging LLM applications during development as well as in production. Haystack provides different logging solutions out of the box to get you started quickly, depending on your use case."
  6  ---
  7  
  8  import ClickableImage from "@site/src/components/ClickableImage";
  9  
 10  # Logging
 11  
 12  Logging is crucial for monitoring and debugging LLM applications during development as well as in production. Haystack provides different logging solutions out of the box to get you started quickly, depending on your use case.
 13  
 14  ## Standard Library Logging (default)
 15  
 16  Haystack logs through Python’s standard library. This gives you full flexibility and customizability to adjust the log format according to your needs.
 17  
 18  ### Changing the Log Level
 19  
 20  By default, Haystack's logging level is set to `WARNING`. To display more information, you can change it to `INFO`. This way, not only warnings but also information messages are displayed in the console output.
 21  
 22  To change the logging level to `INFO`, run:
 23  
 24  ```python
 25  import logging
 26  
 27  logging.basicConfig(
 28      format="%(levelname)s - %(name)s -  %(message)s",
 29      level=logging.WARNING,
 30  )
 31  logging.getLogger("haystack").setLevel(logging.INFO)
 32  ```
 33  
 34  #### Further Configuration
 35  
 36  See [Python’s documentation on logging](https://docs.python.org/3/howto/logging.html) for more advanced configuration.
 37  
 38  ## Real-Time Pipeline Logging
 39  
 40  Use Haystack's [`LoggingTracer`](https://github.com/deepset-ai/haystack/blob/main/haystack/tracing/logging_tracer.py) logs to inspect the data that's flowing through your pipeline in real-time.
 41  
 42  This feature is particularly helpful during experimentation and prototyping, as you don’t need to set up any tracing backend beforehand.
 43  
 44  Here’s how you can enable this tracer. In this example, we are adding color tags (this is optional) to highlight the components' names and inputs:
 45  
 46  ```python
 47  import logging
 48  from haystack import tracing
 49  from haystack.tracing.logging_tracer import LoggingTracer
 50  
 51  logging.basicConfig(
 52      format="%(levelname)s - %(name)s -  %(message)s",
 53      level=logging.WARNING,
 54  )
 55  logging.getLogger("haystack").setLevel(logging.DEBUG)
 56  
 57  tracing.tracer.is_content_tracing_enabled = (
 58      True  # to enable tracing/logging content (inputs/outputs)
 59  )
 60  tracing.enable_tracing(
 61      LoggingTracer(
 62          tags_color_strings={
 63              "haystack.component.input": "\x1b[1;31m",
 64              "haystack.component.name": "\x1b[1;34m",
 65          },
 66      ),
 67  )
 68  ```
 69  
 70  Here’s what the resulting log would look like when a pipeline is run:
 71  
 72  <ClickableImage src="/img/55c3d5c84282d726c95fb3350ec36be49a354edca8a6164f5dffdab7121cec58-image_2.png" alt="Console output showing Haystack pipeline execution with DEBUG level tracing logs including component names, types, and input/output specifications" />
 73  
 74  ## Structured Logging
 75  
 76  Haystack leverages the [structlog library](https://www.structlog.org/en/stable/) to provide structured key-value logs. This provides additional metadata with each log message and is especially useful if you archive your logs with tools like [ELK](https://www.elastic.co/de/elastic-stack), [Grafana](https://grafana.com/oss/agent/?plcmt=footer), or [Datadog](https://www.datadoghq.com/).
 77  
 78  If Haystack detects a [structlog installation](https://www.structlog.org/en/stable/) on your system, it will automatically switch to structlog for logging.
 79  
 80  ### Console Rendering
 81  
 82  To make development a more pleasurable experience, Haystack uses [structlog’s `ConsoleRender`](https://www.structlog.org/en/stable/console-output.html) by default to render structured logs as a nicely aligned and colorful output:
 83  
 84  <ClickableImage src="/img/e49a1f2-Screenshot_2024-02-27_at_16.13.51.png" alt="Python code snippet demonstrating basic logging setup with getLogger and a warning level log message output" />
 85  
 86  :::tip
 87  Rich Formatting
 88  
 89  Install [_rich_](https://rich.readthedocs.io/en/stable/index.html) to beautify your logs even more!
 90  :::
 91  
 92  ### JSON Rendering
 93  
 94  We recommend JSON logging when deploying Haystack to production. Haystack will automatically switch to JSON format if it detects no interactive terminal session. If you want to enforce JSON logging:
 95  
 96  - Run Haystack with the environment variable `HAYSTACK_LOGGING_USE_JSON` set to `true`.
 97  - Or, use Python to tell Haystack to log as JSON:
 98  
 99    ```python
100    import haystack.logging
101  
102    haystack.logging.configure_logging(use_json=True)
103    ```
104  
105  <ClickableImage src="/img/bff93d4-Screenshot_2024-02-27_at_16.15.35.png" alt="Python code snippet showing structured JSON logging configuration with example JSON formatted log output including event, level, and timestamp fields" />
106  
107  ### Disabling Structured Logging
108  
109  To disable structured logging despite an existing installation of structlog, set the environment variable `HAYSTACK_LOGGING_IGNORE_STRUCTLOG_ENV_VAR` to `true` when running Haystack.