Skip to content

Commit 203a59f

Browse files
committed
refactor: extract tracing setup to logger::setup()
1 parent f0972c2 commit 203a59f

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod config;
1717
pub mod context;
1818
pub mod errors;
1919
pub mod handlers;
20+
pub mod logger;
2021
pub mod requests;
2122
pub mod responses;
2223
pub mod routes;

src/logger.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) wangeguo. All rights reserved.
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+
use tracing::metadata::LevelFilter;
16+
use tracing_subscriber::EnvFilter;
17+
18+
pub fn setup() {
19+
let filter =
20+
EnvFilter::builder().with_default_directive(LevelFilter::DEBUG.into()).from_env_lossy();
21+
tracing_subscriber::fmt().with_env_filter(filter).init();
22+
}

src/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515
use std::sync::Arc;
1616

1717
use clap::Parser;
18-
use codecraft::{app, config::Config, context::Context};
19-
use tracing::metadata::LevelFilter;
20-
use tracing_subscriber::EnvFilter;
18+
use codecraft::{app, config::Config, context::Context, logger};
2119

2220
#[tokio::main]
2321
async fn main() -> anyhow::Result<()> {
24-
let filter =
25-
EnvFilter::builder().with_default_directive(LevelFilter::DEBUG.into()).from_env_lossy();
26-
tracing_subscriber::fmt().with_env_filter(filter).init();
22+
logger::setup();
2723

2824
// This returns an error if the `.env` file doesn't exist, but that's not what we want
2925
// since we're not going to use a `.env` file if we deploy this application.

0 commit comments

Comments
 (0)