Google is committed to advancing racial equity for Black communities. See how.

Logging in Rust

Rust programs on Fuchsia generally use the log crate and its info!/warn!/error! macros. The fuchsia_syslog crate is used for initialization.

See the language agnostic logging docs for more information about recording and viewing logs.

Requirements

GN dependencies

The necessary crates can be included with two additions to deps in BUILD.gn:

deps = [
  "//src/lib/syslog/rust:syslog",   # for initialization
  "//third_party/rust_crates:log",  # for recording messages
]

See Rust: Overview for more information about building Rust within Fuchsia.

Sandbox dependency

In order to connect to a diagnostics service, fuchsia.logger.LogSink must be in the sandbox of the connecting component's .cmx file:

{
  "sandbox": {
    "services": [
      "fuchsia.logger.LogSink"
    ]
  }
}

The syslog library will fallback to stderr if the LogSink connection fails.

Initialization

The Rust log crate must be connected to the Fuchsia backend before it can emit messages. Failure to initialize the library will result in dropped messages.

Basic

In your component's main.rs:

use fuchsia_syslog as syslog;

fn main() {
    // configures a single tag with the process name
    syslog::init().unwrap();
}

With tags

By default the process name is used as the tag for all log messages. Tags can be used to further categorize log messages from the program, and they can also be globally overridden.

use fuchsia_syslog as syslog;

fn main() {
    // overrides the use of process name for tag
    syslog::init_with_tags(&["my_tags"]).unwrap();

    // additional tag for just this message
    syslog::fx_log_info!(tag: "init", "an update on program initialization");
}

Configure severity

The syslog crate starts at INFO severity but can be overridden.

use fuchsia_syslog as syslog;

fn main() {
    syslog::init().unwrap();

    // suppress INFO and below
    syslog::set_severity(syslog::levels::WARN);
}

Recording messages

Most uses of logging are with the log crate's macros:

trace!("something happened: {}", 5); // maps to TRACE
debug!("something happened: {}", 4); // maps to DEBUG
info!("something happened: {}", 3);  // maps to INFO
warn!("something happened: {}", 2);  // maps to WARN
error!("something happened: {}", 1); // maps to ERROR

The fuchsia_syslog crate also offers macros like fx_log_info! which allow manually specifying the tag of a message.

Standard streams

println!, eprintln! etc. go to standard out (stdout) and standard error (stderr).

See stdout & stderr in the language-agnostic logging docs for details on the routing of stdio streams in the system.