You are currently viewing the docs for Dioxus 0.5.7 which is no longer maintained.

Logging

Dioxus has a wide range of supported platforms, each with their own logging requirements. We'll discuss the different options available for your projects.

The Tracing Crate

The Tracing crate is the logging interface that the Dioxus library uses. It is not required to use the Tracing crate, but you will not recieve logs from the Dioxus library.

The Tracing crate provides a variety of simple println-like macros with varying levels of severity.

fn main() {
    tracing::trace!("trace");
    tracing::debug!("debug");
    tracing::info!("info");
    tracing::warn!("warn");
    tracing::error!("error");
}

All the loggers provided on this page are, besides configuration and initialization, interfaced using these macros. Often you will also utilize the Tracing crate's Level enum. This enum usually represents the maximum log severity you want your application to emit and can be loaded from a variety of sources such as configuration file, environment variable, and more.

For more information, visit the Tracing crate's docs.

Dioxus Logger

Dioxus Logger is a logging utility that will start the appropriate logger for the platform. Currently every platform except mobile is supported.

To use Dioxus Logger, call the init() function:

use tracing::Level;

fn main() {
    // Init logger
    dioxus_logger::init(Level::INFO).expect("failed to init logger");
    // Dioxus launch code
}

The dioxus_logger::init() function initializes Dioxus Logger with the appropriate tracing logger using the default configuration and provided Level.

Platform Intricacies

On web, Dioxus Logger will use tracing-wasm. On Desktop and server-based targets, Dioxus Logger will use tracing-subscriber's FmtSubscriber.

Final Notes

Dioxus Logger is the preferred logger to use with Dioxus if it suites your needs. There are more features to come and Dioxus Logger is planned to become an integral part of Dioxus. If there are any feature suggestions or issues with Dioxus Logger, feel free to reach out on the Dioxus Discord Server!

For more information, visit Dioxus Logger's docs.

Desktop and Server

For Dioxus' desktop and server targets, you can generally use the logger of your choice.

Some popular options are:

To keep this guide short, we will not be covering the usage of these crates.

For a full list of popular tracing-based logging crates, visit this list in the Tracing crate's docs.

Web

tracing-wasm is a logging interface that can be used with Dioxus' web platform.

The easiest way to use WASM Logger is with the set_as_global_default function:

fn main() {
    // Init logger
    tracing_wasm::set_as_global_default();
    // Dioxus code
}

This starts tracing with a Level of Trace.

Using a custom level is a little trickier. We need to use the WasmLayerConfigBuilder and start the logger with set_as_global_default_with_config():

use tracing::Level;

fn main() {
    // Init logger
    let tracing_config = tracing_wasm::WASMLayerConfigBuilder::new().set_max_level(Level::INFO).build();
    tracing_wasm::set_as_global_default_with_config(tracing_config);
    // Dioxus code
}

Mobile

Unfortunately there are no tracing crates that work with mobile targets. As an alternative you can use the log crate.

Android

Android Logger is a logging interface that can be used when targeting Android. Android Logger runs whenever an event native_activity_create is called by the Android system:

use log::LevelFilter;
use android_logger::Config;

fn native_activity_create() {
    android_logger::init_once(
        Config::default()
            .with_max_level(LevelFilter::Info)
            .with_tag("myapp");
    );
}

The with_tag() is what your app's logs will show as.

Viewing Logs

Android logs are sent to logcat. To use logcat through the Android debugger, run:

adb -d logcat

Your Android device will need developer options/usb debugging enabled.

For more information, visit android_logger's docs.

iOS

The current option for iOS is the oslog crate.

fn main() {
    // Init logger
    OsLogger::new("com.example.test")
        .level_filter(LevelFilter::Debug)
        .init()
        .expect("failed to init logger");
    // Dioxus code
}

Viewing Logs

You can view the emitted logs in Xcode.

For more information, visit oslog.