diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c8611e3..e18005f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,7 +62,7 @@ jobs: - name: Run examples run: | cargo run --example simple_stdio - cargo run --features="colored" --example colored_stdio + cargo run --features="no-color" --example no_color_stdio cargo run --features="json" --example json_stdio required: diff --git a/Cargo.toml b/Cargo.toml index 765f397..5f84c2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,12 +26,12 @@ rust-version = "1.71.0" version = "0.1.0" [features] -colored = ["dep:colored"] +no-color = ["colored/no-color"] json = ["dep:serde_json", "dep:serde"] [dependencies] anyhow = { version = "1.0" } -colored = { version = "2.1", optional = true } +colored = { version = "2.1" } humantime = { version = "2.1" } log = { version = "0.4", features = ["std", "kv_unstable"] } paste = { version = "1.0" } @@ -43,9 +43,9 @@ name = "simple_stdio" path = "examples/simple_stdio.rs" [[example]] -name = "colored_stdio" -path = "examples/colored_stdio.rs" -required-features = ["colored"] +name = "no_color_stdio" +path = "examples/no_color_stdio.rs" +required-features = ["no-color"] [[example]] name = "json_stdio" diff --git a/examples/colored_stdio.rs b/examples/no_color_stdio.rs similarity index 89% rename from examples/colored_stdio.rs rename to examples/no_color_stdio.rs index 28100bd..3c8ef22 100644 --- a/examples/colored_stdio.rs +++ b/examples/no_color_stdio.rs @@ -13,14 +13,13 @@ // limitations under the License. use log::LevelFilter; -use logforth::ColoredSimpleTextLayout; use logforth::DispatchAppend; use logforth::LogLevelFilter; use logforth::Logger; use logforth::StdoutAppend; fn main() { - let append = StdoutAppend::new().with_layout(ColoredSimpleTextLayout::default()); + let append = StdoutAppend::new(); let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace)); Logger::new().add_append(append).apply().unwrap(); diff --git a/examples/simple_stdio.rs b/examples/simple_stdio.rs index 3c8ef22..21f7f01 100644 --- a/examples/simple_stdio.rs +++ b/examples/simple_stdio.rs @@ -13,13 +13,14 @@ // limitations under the License. use log::LevelFilter; +use logforth::SimpleTextLayout; use logforth::DispatchAppend; use logforth::LogLevelFilter; use logforth::Logger; use logforth::StdoutAppend; fn main() { - let append = StdoutAppend::new(); + let append = StdoutAppend::new().with_layout(SimpleTextLayout::default()); let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace)); Logger::new().add_append(append).apply().unwrap(); diff --git a/src/append/stdio.rs b/src/append/stdio.rs index b21c9a1..a4429e1 100644 --- a/src/append/stdio.rs +++ b/src/append/stdio.rs @@ -34,7 +34,7 @@ impl Default for StdoutAppend { impl StdoutAppend { pub fn new() -> Self { Self { - layout: LayoutImpl::SimpleText(SimpleTextLayout), + layout: LayoutImpl::SimpleText(SimpleTextLayout::default()), } } @@ -77,7 +77,7 @@ impl Default for StderrAppend { impl StderrAppend { pub fn new() -> Self { Self { - layout: LayoutImpl::SimpleText(SimpleTextLayout), + layout: LayoutImpl::SimpleText(SimpleTextLayout::default()), } } diff --git a/src/layout/colored_simple_text.rs b/src/layout/colored_simple_text.rs deleted file mode 100644 index 237210d..0000000 --- a/src/layout/colored_simple_text.rs +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2024 tison -// -// 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. - -use std::path::Path; -use std::time::SystemTime; - -use colored::Color; -use colored::ColoredString; -use colored::Colorize; -use log::Level; -use log::Record; - -use crate::layout::kv_display::KvDisplay; -use crate::Layout; -use crate::LayoutImpl; - -#[derive(Default, Debug, Clone)] -pub struct ColoredSimpleTextLayout { - pub colors: ColoredLevel, -} - -#[derive(Debug, Clone)] -pub struct ColoredLevel { - pub error: Color, - pub warn: Color, - pub info: Color, - pub debug: Color, - pub trace: Color, -} - -impl Default for ColoredLevel { - fn default() -> Self { - Self { - error: Color::Red, - warn: Color::Yellow, - info: Color::Green, - debug: Color::Blue, - trace: Color::Magenta, - } - } -} - -impl Layout for ColoredSimpleTextLayout { - fn format_bytes(&self, record: &Record) -> anyhow::Result> { - let color = match record.level() { - Level::Error => self.colors.error, - Level::Warn => self.colors.warn, - Level::Info => self.colors.info, - Level::Debug => self.colors.debug, - Level::Trace => self.colors.trace, - }; - let record_level = record.level().to_string(); - let record_level = ColoredString::from(record_level).color(color); - - let text = format!( - "{} {:>5} {}: {}:{} {}{}", - humantime::format_rfc3339_micros(SystemTime::now()), - record_level, - record.module_path().unwrap_or(""), - record - .file() - .and_then(|file| Path::new(file).file_name()) - .and_then(|name| name.to_str()) - .unwrap_or_default(), - record.line().unwrap_or(0), - record.args(), - KvDisplay::new(record.key_values()), - ); - Ok(text.into_bytes()) - } -} - -impl From for LayoutImpl { - fn from(layout: ColoredSimpleTextLayout) -> Self { - LayoutImpl::ColoredSimpleText(layout) - } -} diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 5c38625..c2c5b00 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -12,15 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[cfg(feature = "colored")] -pub use colored_simple_text::ColoredSimpleTextLayout; use log::Record; #[cfg(feature = "json")] pub use simple_json::SimpleJsonLayout; pub use simple_text::SimpleTextLayout; -#[cfg(feature = "colored")] -mod colored_simple_text; mod kv_display; #[cfg(feature = "json")] mod simple_json; @@ -33,8 +29,6 @@ pub trait Layout { #[derive(Debug)] pub enum LayoutImpl { SimpleText(SimpleTextLayout), - #[cfg(feature = "colored")] - ColoredSimpleText(ColoredSimpleTextLayout), #[cfg(feature = "json")] SimpleJson(SimpleJsonLayout), } @@ -43,8 +37,6 @@ impl Layout for LayoutImpl { fn format_bytes(&self, record: &Record) -> anyhow::Result> { match self { LayoutImpl::SimpleText(layout) => layout.format_bytes(record), - #[cfg(feature = "colored")] - LayoutImpl::ColoredSimpleText(layout) => layout.format_bytes(record), #[cfg(feature = "json")] LayoutImpl::SimpleJson(layout) => layout.format_bytes(record), } diff --git a/src/layout/simple_text.rs b/src/layout/simple_text.rs index f2e4c5d..4cb24b6 100644 --- a/src/layout/simple_text.rs +++ b/src/layout/simple_text.rs @@ -15,6 +15,10 @@ use std::path::Path; use std::time::SystemTime; +use colored::Color; +use colored::ColoredString; +use colored::Colorize; +use log::Level; use log::Record; use crate::layout::kv_display::KvDisplay; @@ -22,14 +26,47 @@ use crate::Layout; use crate::LayoutImpl; #[derive(Default, Debug, Clone)] -pub struct SimpleTextLayout; +pub struct SimpleTextLayout { + pub colors: ColoredLevel, +} + +#[derive(Debug, Clone)] +pub struct ColoredLevel { + pub error: Color, + pub warn: Color, + pub info: Color, + pub debug: Color, + pub trace: Color, +} + +impl Default for ColoredLevel { + fn default() -> Self { + Self { + error: Color::Red, + warn: Color::Yellow, + info: Color::Green, + debug: Color::Blue, + trace: Color::Magenta, + } + } +} impl Layout for SimpleTextLayout { fn format_bytes(&self, record: &Record) -> anyhow::Result> { + let color = match record.level() { + Level::Error => self.colors.error, + Level::Warn => self.colors.warn, + Level::Info => self.colors.info, + Level::Debug => self.colors.debug, + Level::Trace => self.colors.trace, + }; + let record_level = record.level().to_string(); + let record_level = ColoredString::from(record_level).color(color); + let text = format!( "{} {:>5} {}: {}:{} {}{}", humantime::format_rfc3339_micros(SystemTime::now()), - record.level(), + record_level, record.module_path().unwrap_or(""), record .file()