6 日志

03-日志 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/android/logging.html

6 日志

应使用 log crate,以便自动将日志输出到 logcat(设备上)或 stdout(主机上):

hello_rust_logs/Android.bp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
rust_binary {
    name: "hello_rust_logs",
    crate_name: "hello_rust_logs",
    srcs: ["src/main.rs"],
    rustlibs: [
        "liblog_rust",
        "liblogger",
    ],
    host_supported: true,
}

hello_rust_logs/src/main.rs:

// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
//! Rust logging demo.

use log::{debug, error, info};

/// Logs a greeting.
fn main() {
    logger::init(
        logger::Config::default()
            .with_tag_on_device("rust")
            .with_max_level(log::LevelFilter::Trace),
    );
    debug!("Starting program.");
    info!("Things are going fine.");
    error!("Something went wrong!");
}

在设备上构建、推送并运行该二进制:

1
2
3
m hello_rust_logs
adb push "$ANDROID_PRODUCT_OUT/system/bin/hello_rust_logs" /data/local/tmp
adb shell /data/local/tmp/hello_rust_logs

日志会出现在 adb logcat 中:

1
adb logcat -s rust
1
2
3
09-08 08:38:32.454  2420  2420 D rust: hello_rust_logs: Starting program.
09-08 08:38:32.454  2420  2420 I rust: hello_rust_logs: Things are going fine.
09-08 08:38:32.454  2420  2420 E rust: hello_rust_logs: Something went wrong!
  • liblogger 中的 logger 实现只需要出现在最终二进制里;若从库中打日志,只需依赖 log 门面 crate。
最后修改 August 11, 2026: 更新 (70a5af133)