Skip to main content

pingap_logger/
lib.rs

1// Copyright 2024-2025 Tree xie.
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
15use snafu::Snafu;
16use tracing::error;
17use tracing_subscriber::EnvFilter;
18use tracing_subscriber::filter::Directive;
19
20mod access;
21mod async_logger;
22mod file_appender;
23#[cfg(unix)]
24mod syslog;
25mod writer;
26
27const LOG_TARGET: &str = "pingap::logger";
28
29#[derive(Debug, Snafu)]
30pub enum Error {
31    #[snafu(display("IO error {source}"))]
32    Io { source: std::io::Error },
33    #[snafu(display("Invalid {message}"))]
34    Invalid { message: String },
35}
36
37pub fn new_env_filter(level: &str) -> EnvFilter {
38    let mut initial_filter = EnvFilter::from_default_env();
39    for item in level.split(",") {
40        match item.parse::<Directive>() {
41            Ok(directive) => {
42                initial_filter = initial_filter.add_directive(directive);
43            },
44            Err(e) => {
45                error!(
46                    target: LOG_TARGET,
47                    error = e.to_string(),
48                    "parse directive fail"
49                );
50            },
51        };
52    }
53    initial_filter
54}
55
56pub use access::*;
57pub use async_logger::*;
58pub use writer::*;