task_local/
task_local.rs

1// Copyright 2024 FastLabs Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Example of using task-local storage with `logForth`.
16
17use logforth::diagnostic::TaskLocalDiagnostic;
18use logforth::diagnostic::task_local::FutureExt;
19use logforth::record::LevelFilter;
20use logforth_layout_text::TextLayout;
21
22#[tokio::main]
23async fn main() {
24    logforth::starter_log::builder()
25        .dispatch(|d| {
26            d.filter(LevelFilter::All)
27                .diagnostic(TaskLocalDiagnostic::default())
28                .append(logforth::append::Stderr::default().with_layout(TextLayout::default()))
29        })
30        .apply();
31
32    async {
33        async {
34            log::error!("Hello error!");
35            log::warn!("Hello warn!");
36            log::info!("Hello info!");
37        }
38        .with_task_local_context([("k3".to_string(), "v3".to_string())])
39        .await;
40        log::debug!("Hello debug!");
41        log::trace!("Hello trace!");
42    }
43    .with_task_local_context([("k1".to_string(), "v1".to_string())])
44    .with_task_local_context([("k2".to_string(), "v2".to_string())])
45    .await;
46}