# Microblog: Debug logging in Anchor and Solana

Logging can be expensive for programs deployed to Solana or logs may contain sensitive information that should not be available during the normal operation of a program. 

In these scenarios, we miss having different log levels, as we are used to with other application frameworks.  In this micro-post, we will implement this feature for both Anchor and plain Solana applications.

We will try to keep things simple: introduce a macro that includes or omits logs based on a Cargo feature.

## Anchor

The `debug` macro, for Anchor programs, is implemented as below:

```rust
#[macro_export]
macro_rules! debug {
    ($($rest:tt)*) => {
        #[cfg(feature="verbose")]
        anchor_lang::prelude::msg!($($rest)*)
    };
}
```

The macro checks for the `verbose` Cargo feature and includes the logging statement if it is enabled.

For this to work, you will need to declare a `verbose` feature in the program's `Cargo.toml`:

```toml
[features]
...
verbose = []
...
```

Now you can use this feature in your code:

```rust
debug!("some super detailed info");
debug!("the secret is {}", secret);
```

We can build binaries with or without debug logging using the following Anchor commands:

```bash
# with debug logging
anchor build -- --features verbose
# without debug logging
anchor build
```

Unfortunately, both `anchor build` and `anchor test` run Cargo builds with `--release` flag so we have to be explicit about enabling the feature.

## Solana

The differences are minimal between Anchor and non-Anchor versions. 

The `debug` macro is defined as below:

```rust
#[macro_export]
macro_rules! debug {
    ($($rest:tt)*) => {
        #[cfg(debug_assertions)]
        solana_program::msg!($($rest)*)
    };
}
```

Here we use the standard `debug_assertions` conditional compilation instead. This removes the need for defining custom features and can be controlled by the standard Cargo profiles.

And `cargo` commands are used for the build:

```bash
# with debug logging
cargo build
# without debug logging
cargo build --release
```
