Skip to main content

midnight_ledger/
tracing.rs

1// This file is part of midnight-ledger.
2// Copyright (C) 2025 Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use tracing_subscriber::filter::LevelFilter;
15use tracing_subscriber::filter::targets::Targets;
16use tracing_subscriber::layer::SubscriberExt;
17use tracing_subscriber::util::SubscriberInitExt;
18use tracing_subscriber::{Layer, Registry};
19
20pub enum LogLevel {
21    Off,
22    Trace,
23    Debug,
24    Info,
25    Warn,
26    Error,
27}
28
29impl From<LogLevel> for LevelFilter {
30    fn from(level: LogLevel) -> Self {
31        use LogLevel::*;
32        match level {
33            Off => LevelFilter::OFF,
34            Trace => LevelFilter::TRACE,
35            Debug => LevelFilter::DEBUG,
36            Info => LevelFilter::INFO,
37            Warn => LevelFilter::WARN,
38            Error => LevelFilter::ERROR,
39        }
40    }
41}
42
43pub fn init_logger(level: LogLevel) {
44    Registry::default()
45        .with(
46            tracing_subscriber::fmt::layer()
47                .with_writer(std::io::stderr)
48                .with_filter(Targets::new().with_default(level)),
49        )
50        .try_init()
51        .ok();
52    info!("Welcome to ledger v{}!", env!("CARGO_PKG_VERSION"));
53}