logforth 0.9.1

A versatile and extensible logging implementation.
Documentation
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Describe how to format a log record.

pub use custom::CustomLayout;
pub use identical::IdenticalLayout;
#[cfg(feature = "json")]
pub use json::JsonLayout;
pub use kv::KvDisplay;
pub use text::LevelColor;
pub use text::TextLayout;

mod custom;
mod identical;
#[cfg(feature = "json")]
mod json;
mod kv;
mod text;

/// A layout describes how to format a log record.
#[derive(Debug)]
pub enum Layout {
    Identical(IdenticalLayout),
    Text(TextLayout),
    #[cfg(feature = "json")]
    Json(JsonLayout),
    Custom(CustomLayout),
}

impl Layout {
    pub(crate) fn format<F>(&self, record: &log::Record, f: &F) -> anyhow::Result<()>
    where
        F: Fn(&log::Record) -> anyhow::Result<()>,
    {
        match self {
            Layout::Identical(layout) => {
                layout.format(record, &|args| f(&record.to_builder().args(args).build()))
            }
            Layout::Text(layout) => {
                layout.format(record, &|args| f(&record.to_builder().args(args).build()))
            }
            #[cfg(feature = "json")]
            Layout::Json(layout) => {
                layout.format(record, &|args| f(&record.to_builder().args(args).build()))
            }
            Layout::Custom(layout) => {
                layout.format(record, &|args| f(&record.to_builder().args(args).build()))
            }
        }
    }
}