7
7
This library is a drop-in replacement for ` env_logger ` . Instead, it outputs messages to
8
8
android's logcat.
9
9
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 :
13
13
14
14
``` toml
15
15
[target .'cfg(target_os = "android")' .dependencies ]
16
16
android_logger = " 0.15"
17
17
```
18
18
19
- Example of initialization on activity creation, with log configuration:
19
+ ### Examples
20
20
21
- ``` rust
22
- #[macro_use] extern crate log;
23
- extern crate android_logger;
21
+ #### Example of initialization on activity creation, with log configuration
24
22
25
- use log :: LevelFilter ;
26
- use android_logger :: { Config , FilterBuilder } ;
23
+ ``` rust
24
+ use android_logger :: Config ;
27
25
28
26
fn native_activity_create () {
29
27
android_logger :: init_once (
30
28
Config :: default ()
31
- . with_max_level (LevelFilter :: Trace ) // limit log level
32
29
. 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.
37
31
);
38
32
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" );
41
35
}
42
36
```
43
37
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 `
45
39
46
40
``` rust
47
- #[macro_use] extern crate log;
48
- extern crate android_logger;
49
-
50
- use log :: LevelFilter ;
51
41
use android_logger :: Config ;
52
42
53
43
fn native_activity_create () {
54
44
android_logger :: init_once (
55
- Config :: default (). with_max_level ( LevelFilter :: Trace ),
45
+ Config :: default (). filter_level ( log :: LevelFilter :: Trace ),
56
46
);
57
47
}
58
48
```
59
49
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
+
60
63
There is a caveat that this library can only be initialized once
61
64
(hence the ` init_once ` function name). However, Android native activity can be
62
65
re-created every time the screen is rotated, resulting in multiple initialization calls.
63
66
Therefore this library will only log a warning for subsequent ` init_once ` calls.
64
67
68
+ ###
69
+
65
70
This library ensures that logged messages do not overflow Android log message limits
66
71
by efficiently splitting messages into chunks.
67
72
68
- ## Consistent log filtering in mixed Rust/C/C++ apps
73
+ ### Consistent log filtering in mixed Rust/C/C++ apps
69
74
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.
76
76
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
78
81
.-----.
79
82
| app |
80
83
'-----' Rust
@@ -97,18 +100,19 @@ C/C++ | '--------------.
97
100
```
98
101
99
102
` 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++
101
104
and Rust calls.
102
105
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
104
107
will consider the process-wide global state (set via
105
108
[ ` __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 ) )
106
109
and Android system properties when deciding if a message should be logged or
107
110
not. 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
110
114
111
- ## License
115
+ ### License
112
116
113
117
Licensed under either of
114
118
0 commit comments