# Logging

> See what Kurogane and Chromium do and read the errors your app reports when it can't start.

Kurogane reports what it does through [`tracing`](https://docs.rs/tracing) events. Lifecycle and IPC detail go to `debug`. Problems go to `warn` and `error`.

Kurogane never writes to stdout or stderr itself. The one exception is the error `run_or_exit` prints when your app can't start. Nothing appears until your app installs a subscriber.

## Print Kurogane's log

Add `tracing-subscriber`:

```toml
[dependencies]
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
```

Install it before the app starts:

```rust
use tracing_subscriber::EnvFilter;
use tracing_subscriber::filter::LevelFilter;

fn main() {
    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::WARN.into())
                .from_env_lossy(),
        )
        .log_internal_errors(false)
        .init();

    kurogane::App::new("content").run_or_exit();
}
```

Warnings and errors then print by default. Add the detail with `RUST_LOG`:

```bash
RUST_LOG=kurogane=debug kurogane dev
```

Any other `tracing` subscriber works the same way.

> [!IMPORTANT]
> Keep `log_internal_errors(false)`. Without it the subscriber reports a line it can't write on stderr instead. That happens when your app's output goes into a program that has exited, such as `| tee` ended by Ctrl+C. When stderr is that same closed pipe the report panics. A panic inside a Chromium callback aborts your app.

## What the debug log shows

* Each step of startup and shutdown
* Each browser and window as it opens and closes
* IPC traffic between pages and your handlers
* The final command line Chromium starts with
* Every switch Chromium holds otherwise than Kurogane passed it

Chromium ignores a switch or feature name it doesn't know without a word. The last two lines are how you spot one. See [Chromium flags and preferences](https://kurogane-rs.org/docs/guides/chromium-flags.md).

## Chromium's own log

Chromium keeps a log of its own apart from Kurogane's. Kurogane leaves its settings at CEF's defaults. CEF reads the `log-file` and `log-severity` switches for them:

```rust
use kurogane::App;

fn main() {
    App::new("content")
        .chromium_flag_with_value("log-severity", "verbose")
        .chromium_flag_with_value("log-file", "chromium.log")
        .run_or_exit();
}
```

## When the app can't start

[`App::run_or_exit`](https://kurogane-rs.org/docs/api/app/app.md#method.run_or_exit) prints the error with each of its causes and exits with code 1. [`App::run`](https://kurogane-rs.org/docs/api/app/app.md#method.run) returns it as a [`RuntimeError`](https://kurogane-rs.org/docs/api/app/runtimeerror.md) for your app to handle.

A configuration problem is [`RuntimeError::InvalidConfiguration`](https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.InvalidConfiguration). It lists every problem the builder found before anything started.

When the Chromium runtime can't be used the error says which runtime failed and how to fix it:

| Error | When |
|---|---|
| `CefNotInstalled` | no runtime of your app's CEF version is installed |
| `CefPathMissing` | `CEF_PATH` names no directory |
| `CefVersionMismatch` | the runtime is another CEF build than the one your app was built against |
| `InvalidCefRuntime` | the runtime is incomplete |

`CefVersionMismatch` and `InvalidCefRuntime` carry a [`CefLocation`](https://kurogane-rs.org/docs/api/app/ceflocation.md). Each message gives the fix for its location:

| Location | Fix |
|---|---|
| `Bundle` | reinstall the app |
| `BesideExecutable` | remove the runtime beside the executable or replace it with the right CEF |
| `CefPath` | point `CEF_PATH` at the right CEF or unset it |
| `Installed` | remove it and reinstall it with `kurogane install` |

`kurogane doctor` checks the runtime, the build tools and the project before you run anything.
