> ## Documentation Index
> Fetch the complete documentation index at: https://chatcli.edilsonfreitas.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: Debugging Error Logs with Pipes

> Learn how to use shell pipes and one-shot mode to analyze complex logs and find the root cause of errors quickly.

Analyzing lengthy log files can be a time-consuming and frustrating task. This recipe will show you how to use **ChatCLI** together with `cat`, `grep`, or `kubectl logs` to turn the AI into your personal log analyst.

## The Problem

You are facing a bug in production. The log file has thousands of lines with `INFO`, `WARN`, and `ERROR`. Finding the relevant exception and understanding the cascade of events that led to the error is like finding a needle in a haystack.

## Ingredients

* Access to a log file (e.g., `app.log`, `/var/log/syslog`).
* Or access to a command that generates logs (e.g., `kubectl logs <pod-name>`).

***

## Step by Step

<Steps>
  <Step title="Pipe the Log Output to ChatCLI">
    The magic happens when you use the pipe operator (`|`) from your shell to send the output of a command directly to `chatcli` in one-shot mode (`-p`).

    <Tabs>
      <Tab title="Local Log File">
        Analyze a complete log file by asking the AI to identify the most frequent error and explain its cause.

        ```bash theme={"system"}
        cat /var/log/nginx/error.log | chatcli -p "Analyze this Nginx log, identify the most recurring error, explain the likely cause, and suggest a command to fix it."
        ```
      </Tab>

      <Tab title="Kubernetes Container Logs">
        If you are a DevOps engineer, you can do this directly with `kubectl`.

        ```bash theme={"system"}
        kubectl logs my-app-pod-xyz-123 | chatcli -p "This is the log of a pod that is restarting. Find the fatal exception (panic) and tell me which source code file and line it likely occurred at."
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="Refine the Analysis with grep">
    If the log is massive, sending everything can be inefficient. You can pre-filter the relevant lines with `grep` and send only the error context to the AI.

    Let's assume you know the error is related to a `NullPointerException`.

    ```bash theme={"system"}
    grep -C 20 "NullPointerException" application.log | chatcli -p "The main error is a NullPointerException. Based on the 20 lines of context before and after, what is the call sequence that led to this error?"
    ```

    <Info>
      **Deconstructing the Command:**

      * `grep -C 20 "NullPointerException"`: Finds the line with the error and captures **20 lines of context** (`C`) before and after.
      * `| chatcli -p "..."`: Sends this focused excerpt to the AI with a very specific question, resulting in a faster and more accurate analysis.
    </Info>
  </Step>

  <Step title="Use the Response to Take Action">
    The AI will return a structured analysis, saving you time on reading and interpretation.

    **Example AI Response (for the Kubernetes scenario):**

    ```markdown theme={"system"}
    The log analysis indicates a fatal exception (panic) caused by a null pointer.

    **Likely Root Cause:**
    The exception `panic: runtime error: invalid memory address or nil pointer dereference` occurs in the following call stack:
    - `main.go:152`: in the `processPayment()` function
    - `main.go:98`: in the `handleRequest()` function

    This suggests that the `dbConnection` variable in the `processPayment` function was nil when called.

    **Suggested Fix:**
    Verify that the database connection was properly initialized before calling `processPayment()` at line 98 of `main.go`. Add a nil check:

    `if dbConnection == nil { return fmt.Errorf("database connection is not active") }`
    ```

    <Tip>
      **Result:** Instead of spending minutes (or hours) reading logs, you got the root cause, the location in the code, and a fix suggestion in seconds.
    </Tip>
  </Step>
</Steps>
