Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: merge color as default and add no-color feature #3

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ rust-version = "1.71.0"
version = "0.1.0"

[features]
colored = ["dep:colored"]
json = ["dep:serde_json", "dep:serde"]
no-color = ["colored/no-color"]

[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" }
Expand All @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions examples/colored_stdio.rs → examples/no_color_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/append/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Default for StdoutAppend {
impl StdoutAppend {
pub fn new() -> Self {
Self {
layout: LayoutImpl::SimpleText(SimpleTextLayout),
layout: LayoutImpl::SimpleText(SimpleTextLayout::default()),
}
}

Expand Down Expand Up @@ -77,7 +77,7 @@ impl Default for StderrAppend {
impl StderrAppend {
pub fn new() -> Self {
Self {
layout: LayoutImpl::SimpleText(SimpleTextLayout),
layout: LayoutImpl::SimpleText(SimpleTextLayout::default()),
}
}

Expand Down
88 changes: 0 additions & 88 deletions src/layout/colored_simple_text.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,8 +29,6 @@ pub trait Layout {
#[derive(Debug)]
pub enum LayoutImpl {
SimpleText(SimpleTextLayout),
#[cfg(feature = "colored")]
ColoredSimpleText(ColoredSimpleTextLayout),
#[cfg(feature = "json")]
SimpleJson(SimpleJsonLayout),
}
Expand All @@ -43,8 +37,6 @@ impl Layout for LayoutImpl {
fn format_bytes(&self, record: &Record) -> anyhow::Result<Vec<u8>> {
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),
}
Expand Down
41 changes: 39 additions & 2 deletions src/layout/simple_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,58 @@
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 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<Vec<u8>> {
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()
Expand Down