Expand description
A tracing layer that sends logs to Aliyun SLS.
§Feature Flags
Note: lz4
and deflate
cannot be enabled at the same time.
lz4
: enable lz4 compression for logs.deflate
: enable deflate compression for logs.log-comp
: enable theLogger
forlog
crate.derive-key
: enable the ability to derive the shard key (128bit hex) from any string using BLAKE3.
§Examples
§Tracing
use tracing_aliyun_sls::SlsLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let (layer, _guard) = SlsLayer::builder()
.access_key("access_key")
.access_secret("access_secret")
.endpoint("cn-hangzhou.log.aliyuncs.com")
.project("project")
.logstore("logstore")
.shard_key("shard_key") // Optional if you want to use `KeyHash` mode
.max_level(tracing::Level::INFO) // Optional, default is `tracing::Level::TRACE`
.drain_timeout(std::time::Duration::from_secs(10)) // Optional, default is 5 seconds
.build_layer();
tracing_subscriber::registry()
.with(layer)
.init();
}
§Log
If you want to use the Logger
for log
crate, you need to enable the log-comp
feature.
use tracing_aliyun_sls::SlsLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
#[tokio::main(flavor = "current_thread")]
async fn main() {
SlsLayer::builder()
.access_key("access_key")
.access_secret("access_secret")
.endpoint("cn-hangzhou.log.aliyuncs.com")
.project("project")
.logstore("logstore")
.shard_key("shard_key") // Optional if you want to use `KeyHash` mode
.max_level(tracing::Level::INFO) // Optional, default is `tracing::Level::TRACE`
.drain_timeout(std::time::Duration::from_secs(10)) // Optional, default is 5 seconds
.build_logger()
.init();
}
Structs§
- Logger
- A logger that sends logs to Aliyun SLS.
- SlsLayer
- A layer that collects logs and sends them to Aliyun SLS.
- SlsTracing
Builder - A builder for creating a SlsLayer.
- Work
Guard - A guard that will send a shutdown signal to the dispatcher when dropped.