• Home
Name Date Size #Lines LOC

..--

patches/04-Jul-2025-214190

src/04-Jul-2025-931624

tests/04-Jul-2025-4028

.android-checksum.jsonD04-Jul-20252 KiB11

.cargo-checksum.jsonD04-Jul-20251.3 KiB11

Android.bpD04-Jul-20252.8 KiB123115

CHANGELOG.mdD04-Jul-20251.7 KiB9142

Cargo.tomlD04-Jul-20251.7 KiB8069

LICENSED04-Jul-202510 KiB202169

LICENSE-APACHED04-Jul-202510 KiB202169

METADATAD04-Jul-2025436 1817

MODULE_LICENSE_APACHE2D04-Jul-20250

README.mdD04-Jul-20254.6 KiB12597

TEST_MAPPINGD04-Jul-20251.1 KiB5554

cargo_embargo.jsonD04-Jul-2025149 1110

README.md

1## Send Rust logs to Logcat
2
3[![Version](https://img.shields.io/crates/v/android_logger.svg)](https://crates.io/crates/android_logger)
4[![CI status](https://github.com/rust-mobile/android_logger-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/rust-mobile/android_logger-rs/actions/workflows/ci.yml/)
5
6
7This library is a drop-in replacement for `env_logger`. Instead, it outputs messages to
8android's logcat.
9
10This only works on Android and requires linking to `log` which
11is only available under android. With Cargo, it is possible to conditionally require
12this library:
13
14```toml
15[target.'cfg(target_os = "android")'.dependencies]
16android_logger = "0.15"
17```
18
19Example of initialization on activity creation, with log configuration:
20
21```rust
22#[macro_use] extern crate log;
23extern crate android_logger;
24
25use log::LevelFilter;
26use android_logger::{Config,FilterBuilder};
27
28fn native_activity_create() {
29    android_logger::init_once(
30        Config::default()
31            .with_max_level(LevelFilter::Trace) // limit log level
32            .with_tag("mytag") // logs will show under mytag tag
33            .with_filter( // configure messages for specific crate
34                FilterBuilder::new()
35                    .parse("debug,hello::crate=error")
36                    .build())
37    );
38
39    trace!("this is a verbose {}", "message");
40    error!("this is printed by default");
41}
42```
43
44To allow all logs, use the default configuration with min level Trace:
45
46```rust
47#[macro_use] extern crate log;
48extern crate android_logger;
49
50use log::LevelFilter;
51use android_logger::Config;
52
53fn native_activity_create() {
54    android_logger::init_once(
55        Config::default().with_max_level(LevelFilter::Trace),
56    );
57}
58```
59
60There is a caveat that this library can only be initialized once
61(hence the `init_once` function name). However, Android native activity can be
62re-created every time the screen is rotated, resulting in multiple initialization calls.
63Therefore this library will only log a warning for subsequent `init_once` calls.
64
65This library ensures that logged messages do not overflow Android log message limits
66by efficiently splitting messages into chunks.
67
68## Consistent log filtering in mixed Rust/C/C++ apps
69
70Android's C logging API determines the effective log level based on [a
71combination](https://cs.android.com/android/platform/superproject/main/+/main:system/logging/liblog/properties.cpp;l=243;drc=b74a506c1b69f5b295a8cdfd7e2da3b16db15934)
72of a process-wide global variable, [system-wide
73properties](https://cs.android.com/android/platform/superproject/main/+/main:system/logging/logd/README.property;l=45;drc=99c545d3098018a544cb292e1501daca694bee0f),
74and call-specific default. `log` + `android_logger` crates add another layer of
75log filtering on top of that, independent from the C API.
76
77```
78    .-----.
79    | app |
80    '-----'     Rust
81C/C++ | '--------------.
82      |                v
83      |          .-----------.   filter by log::STATIC_MAX_LEVEL +
84      |          | log crate | - log::MAX_LOG_LEVEL_FILTER,
85      |          '-----------'   overrideable via log::set_max_level
86      |                |
87      |                v
88      |     .----------------------.
89      |     | android_logger crate | - filter by Config::max_level
90      |     '----------------------'
91      |                |
92      |   .------------'
93      v   v
94   .--------.
95   | liblog | - filter by global state or system-wide properties
96   '--------'
97```
98
99`liblog` APIs introduced in Android API 30 let `android_logger` delegate log
100filtering decision to `liblog`, making the log level consistent across C, C++
101and Rust calls.
102
103If you build `android_logger` with `android-api-30` feature enabled, the logger
104will consider the process-wide global state (set via
105[`__android_log_set_minimum_priority`](https://cs.android.com/android/platform/superproject/main/+/main:prebuilts/runtime/mainline/runtime/sdk/common_os/include/system/logging/liblog/include/android/log.h;l=364;drc=4cf460634134d51dba174f8af60dffb10f703f51))
106and Android system properties when deciding if a message should be logged or
107not. In this case, the effective log level is the _least verbose_ of the levels
108set between those and Rust log facilities.
109
110## License
111
112Licensed under either of
113
114 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
115 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
116
117at your option.
118
119### Contribution
120
121Unless you explicitly state otherwise, any contribution intentionally
122submitted for inclusion in the work by you, as defined in the Apache-2.0
123license, shall be dual licensed as above, without any additional terms or
124conditions.
125