77This library is a drop-in replacement for ` env_logger ` . Instead, it outputs messages to
88android's logcat.
99
10- This only works on Android and requires linking to ` log ` which
11- is only available under android . With Cargo, it is possible to conditionally require
12- this library:
10+ This only works on Android and requires linking to ` liblog ` which
11+ is only available under Android . With Cargo, it is possible to conditionally
12+ include this crate and library requirement when targeting Android only :
1313
1414``` toml
1515[target .'cfg(target_os = "android")' .dependencies ]
1616android_logger = " 0.15"
1717```
1818
19- Example of initialization on activity creation, with log configuration:
19+ ### Examples
2020
21- ``` rust
22- #[macro_use] extern crate log;
23- extern crate android_logger;
21+ #### Example of initialization on activity creation, with log configuration
2422
25- use log :: LevelFilter ;
26- use android_logger :: { Config , FilterBuilder } ;
23+ ``` rust
24+ use android_logger :: Config ;
2725
2826fn native_activity_create () {
2927 android_logger :: init_once (
3028 Config :: default ()
31- . with_max_level (LevelFilter :: Trace ) // limit log level
3229 . 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 ())
30+ . parse_filters (" debug,hello::crate=error" ) // Limig log level to Debug, and limit the hello::crate module further to Error.
3731 );
3832
39- trace ! (" this is a verbose {}" , " message" );
40- error! (" this is printed by default" );
33+ log :: debug ! (" this is a verbose {}" , " message" );
34+ log :: error! (" this is printed by default" );
4135}
4236```
4337
44- To allow all logs, use the default configuration with min level Trace:
38+ To allow all logs, use the default configuration with the global module level set to ` Trace `
4539
4640``` rust
47- #[macro_use] extern crate log;
48- extern crate android_logger;
49-
50- use log :: LevelFilter ;
5141use android_logger :: Config ;
5242
5343fn native_activity_create () {
5444 android_logger :: init_once (
55- Config :: default (). with_max_level ( LevelFilter :: Trace ),
45+ Config :: default (). filter_level ( log :: LevelFilter :: Trace ),
5646 );
5747}
5848```
5949
50+ #### Example with a custom log formatter
51+
52+ ``` rust
53+ use android_logger :: Config ;
54+
55+ android_logger :: init_once (
56+ Config :: default ()
57+ . format (| f , record | write! (f , " my_app: {}" , record . args ()))
58+ )
59+ ```
60+
61+ ### Single-initialization guarantee
62+
6063There is a caveat that this library can only be initialized once
6164(hence the ` init_once ` function name). However, Android native activity can be
6265re-created every time the screen is rotated, resulting in multiple initialization calls.
6366Therefore this library will only log a warning for subsequent ` init_once ` calls.
6467
68+ ###
69+
6570This library ensures that logged messages do not overflow Android log message limits
6671by efficiently splitting messages into chunks.
6772
68- ## Consistent log filtering in mixed Rust/C/C++ apps
73+ ### Consistent log filtering in mixed Rust/C/C++ apps
6974
70- Android's C logging API determines the effective log level based on [ a
71- combination] ( https://cs.android.com/android/platform/superproject/main/+/main:system/logging/liblog/properties.cpp;l=243;drc=b74a506c1b69f5b295a8cdfd7e2da3b16db15934 )
72- of a process-wide global variable, [ system-wide
73- properties] ( https://cs.android.com/android/platform/superproject/main/+/main:system/logging/logd/README.property;l=45;drc=99c545d3098018a544cb292e1501daca694bee0f ) ,
74- and call-specific default. ` log ` + ` android_logger ` crates add another layer of
75- log filtering on top of that, independent from the C API.
75+ Android's C logging API determines the effective log level based on [ the combination] of a process-wide global variable, [ system-wide properties] , and call-specific default. ` log ` + ` android_logger ` crates add another layer of log filtering on top of that, independent from the C API.
7676
77- ```
77+ [ the combination ] : https://cs.android.com/android/platform/superproject/main/+/main:system/logging/liblog/properties.cpp;l=243;drc=b74a506c1b69f5b295a8cdfd7e2da3b16db15934
78+ [ system-wide properties ] : https://cs.android.com/android/platform/superproject/main/+/main:system/logging/logd/README.property;l=45;drc=99c545d3098018a544cb292e1501daca694bee0f
79+
80+ ``` text
7881 .-----.
7982 | app |
8083 '-----' Rust
@@ -97,23 +100,24 @@ C/C++ | '--------------.
97100```
98101
99102` liblog ` APIs introduced in Android API 30 let ` android_logger ` delegate log
100- filtering decision to ` liblog ` , making the log level consistent across C, C++
103+ filtering decisions to ` liblog ` , making the log level consistent across C, C++
101104and Rust calls.
102105
103- If you build ` android_logger ` with ` android-api-30 ` feature enabled, the logger
106+ If you build ` android_logger ` with the ` android-api-30 ` feature enabled, the logger
104107will consider the process-wide global state (set via
105108[ ` __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 ) )
106109and Android system properties when deciding if a message should be logged or
107110not. In this case, the effective log level is the _ least verbose_ of the levels
108- set between those and [ Rust log
109- facilities] ( https://docs.rs/log/latest/log/fn.set_max_level.html ) .
111+ set between those and [ Rust log facilities] .
112+
113+ [ Rust log facilities ] : https://docs.rs/log/latest/log/fn.set_max_level.html
110114
111- ## License
115+ ### License
112116
113117Licensed under either of
114118
115- * Apache License, Version 2.0, ([ LICENSE-APACHE] ( LICENSE-APACHE ) or http://www.apache.org/licenses/LICENSE-2.0 )
116- * MIT license ([ LICENSE-MIT] ( LICENSE-MIT ) or http://opensource.org/licenses/MIT )
119+ * Apache License, Version 2.0, ([ LICENSE-APACHE] ( LICENSE-APACHE ) or < http://www.apache.org/licenses/LICENSE-2.0 > )
120+ * MIT license ([ LICENSE-MIT] ( LICENSE-MIT ) or < http://opensource.org/licenses/MIT > )
117121
118122at your option.
119123
0 commit comments