Skip to content

Commit

Permalink
Merge pull request #17 from imperva/layer_example
Browse files Browse the repository at this point in the history
layers example
  • Loading branch information
gadunga authored Nov 28, 2023
2 parents bf3cbac + 92cd7db commit dd70b7c
Show file tree
Hide file tree
Showing 14 changed files with 5,812 additions and 91 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cargo-features = ["workspace-inheritance"]

[workspace]
resolver = "2"
members = ["trace4rs", "trace4rs-config", "trace4rs-fmtorp"]
members = ["trace4rs", "trace4rs-config", "trace4rs-fmtorp", "examples"]

[workspace.package]
version = "0.5.1"
Expand Down Expand Up @@ -41,11 +41,11 @@ tracing-subscriber = { version = "0.3", features = [
tracing-log = "0.2"
time = { version = "0.3", features = ["formatting", "macros"] }
path-absolutize = "3.0.11"
trace4rs = { version = "*", path = "trace4rs" }
trace4rs-config = { version = "*", path = "trace4rs-config" }
trace4rs-fmtorp = { version = "*", path = "trace4rs-fmtorp" }
utc-offset = "0.4.0"
camino = "1.1.2"
tracing-span-tree = { git = "https://github.com/estk/tracing-span-tree" }
criterion = "0.5.0"
criterion-macro = "0.4.0"
log4rs = "1.0.0"
Expand Down
20 changes: 20 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "trace4rs-examples"
version = "0.0.0"
publish = false
edition = "2021"
rust-version = "1.67"

[lints]
workspace = true

[dev-dependencies]
log.workspace = true
tracing-log.workspace = true
tempfile.workspace = true
tracing-subscriber.workspace = true
trace4rs.workspace = true
literally.workspace = true
tracing.workspace = true
tokio.workspace = true
tracing-span-tree = { git = "https://github.com/estk/tracing-span-tree" }
13 changes: 5 additions & 8 deletions examples/interval.rs → examples/examples/interval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{
convert::TryFrom,
sync::Arc,
thread::sleep,
time::Duration,
};
Expand All @@ -17,7 +15,7 @@ use tracing::info;

fn main() {
// Create the handle
let handle = {
let config = {
let file = config::Appender::File {
path: "./file.log".into(),
};
Expand All @@ -26,15 +24,14 @@ fn main() {
appenders: literally::hset! {"file"},
format: Format::default(),
};
let config = Config {
Config {
default,
loggers: Default::default(),
appenders: literally::hmap! {"file" => file},
};

Arc::new(Handle::try_from(config).unwrap())
}
};
tracing::subscriber::set_global_default(handle.subscriber()).unwrap();
let (_, s) = <Handle>::from_config(&config).unwrap();
tracing::subscriber::set_global_default(s).unwrap();

for i in 0..usize::MAX {
info!("log message: {}", i);
Expand Down
86 changes: 86 additions & 0 deletions examples/examples/layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::{
io,
thread::sleep,
time::Duration,
};

use trace4rs::{
config::{
self,
Config,
Format,
},
handle::{
ExtendedT4,
LayeredT4,
},
Handle,
};
use tracing::{
info,
Level,
Subscriber,
};
use tracing_span_tree::SpanTree;
use tracing_subscriber::{
filter::{
Filtered,
Targets,
},
fmt::MakeWriter,
registry::LookupSpan,
Layer as _,
Registry,
};

fn main() {
let config = {
let file = config::Appender::File {
path: "file.log".into(),
};
let default = config::Logger {
level: config::LevelFilter::INFO,
appenders: literally::hset! {"file"},
format: Format::default(),
};
Config {
default,
loggers: Default::default(),
appenders: literally::hmap! {"file" => file},
}
};

// Create the handle
// target can be either "" or "layers"
let (_h, s) = init_with_metrics::<Registry, _>("", io::stdout, &config).unwrap();

tracing::subscriber::set_global_default(s).unwrap();

{
let _s = tracing::span!(tracing::Level::TRACE, "foo");
for i in 0..1_000 {
info!("log message: {}", i);
sleep(Duration::from_millis(1));
}
}
}

pub type FilteredST<Reg, Wrt> = Filtered<SpanTree<Wrt>, Targets, LayeredT4<Reg>>;

/// Init a `Handle` and `Subscriber` with span metrics collection.
/// The writer argument is where the said metrics will be written.
pub fn init_with_metrics<Reg, Wrt>(
target: impl Into<String>,
writer: Wrt,
config: &Config,
) -> Result<(Handle<Reg>, ExtendedT4<Reg, FilteredST<Reg, Wrt>>), trace4rs::error::Error>
where
Wrt: for<'a> MakeWriter<'a> + 'static,
Reg: Subscriber + for<'a> LookupSpan<'a> + Default + Send + Sync,
{
let layer = tracing_span_tree::span_tree_with(writer);
let filter = Targets::new().with_target(target, Level::TRACE);
let extra = layer.with_filter(filter);

Handle::from_config_with(config, extra)
}
17 changes: 7 additions & 10 deletions examples/null_appender.rs → examples/examples/null_appender.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryFrom;

use tokio::{
fs,
time::{
Expand All @@ -21,7 +19,7 @@ async fn main() {
let log_path = tmp_guard.path().join("file.log");

// Create the handle
let handle = {
let config = {
let default = config::Logger {
level: config::LevelFilter::TRACE,
appenders: literally::hset! {"file"},
Expand All @@ -45,25 +43,24 @@ async fn main() {
"hush" => hush,
}
};
let config = Config {
Config {
default,
loggers,
appenders,
};

Handle::try_from(config).unwrap()
}
};
tracing::subscriber::set_global_default(handle.subscriber()).unwrap();
let (_h, s) = <Handle>::from_config(&config).unwrap();
tracing::subscriber::set_global_default(s).unwrap();
tracing_log::LogTracer::init().unwrap();

sleep(Duration::from_millis(100)).await;
log::trace!(target: "hush", "this should go nowhere");
log::trace!("this should go to file");
sleep(Duration::from_millis(100)).await;

println!("path: {}", log_path.to_string_lossy());
println!("path is: {}", log_path.to_string_lossy());
let file_content = fs::read_to_string(log_path).await.unwrap();
println!("file: {}", file_content);
println!("file content: {}", file_content);
assert!(file_content.contains("this should go to file"));
assert!(!file_content.contains("this should go nowhere"));
}
18 changes: 7 additions & 11 deletions examples/task.rs → examples/examples/task.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
convert::TryFrom,
sync::Arc,
};
use std::sync::Arc;

use tokio::{
fs,
Expand All @@ -26,7 +23,7 @@ async fn main() {
let file_out_lossy = file_out.to_string_lossy();

// Create the handle
let handle = {
let config = {
let console = config::Appender::Console;
let file = config::Appender::File {
path: file_out_lossy.clone().into_owned(),
Expand All @@ -45,19 +42,18 @@ async fn main() {
appenders: literally::hset! {"file"},
format: Format::default(),
};
let config = Config {
Config {
default,
loggers: literally::hmap! {"trace4rs" => l1},
appenders,
};

Arc::new(Handle::try_from(config).unwrap())
}
};
tracing::subscriber::set_global_default(handle.subscriber()).unwrap();
let (h, s) = <Handle>::from_config(&config).unwrap();
tracing::subscriber::set_global_default(s).unwrap();
println!("Created subscriber for {}", file_out_lossy);

// Spawn an async task to correct appender paths
let handle_clone = handle.clone();
let handle_clone = Arc::new(h);
let file_out_lossy_clone = file_out_lossy.clone().into_owned();
let interval = 500;
tokio::spawn(async move {
Expand Down
25 changes: 13 additions & 12 deletions examples/thread.rs → examples/examples/thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
convert::TryFrom,
fs,
sync::Arc,
thread,
Expand All @@ -21,7 +20,7 @@ fn main() {
let file_out_lossy = file_out.to_string_lossy();

// Create the handle
let handle = {
let config = {
let console = config::Appender::console();
let file = config::Appender::file(file_out_lossy.clone().into_owned());
let appenders = literally::hmap! {
Expand All @@ -38,25 +37,27 @@ fn main() {
appenders: literally::hset! {"file"},
format: Format::default(),
};
let config = Config {
Config {
default,
loggers: literally::hmap! {"trace4rs" => l1},
appenders,
};

Arc::new(Handle::try_from(config).unwrap())
}
};
tracing::subscriber::set_global_default(handle.subscriber()).unwrap();
let (h, s) = <Handle>::from_config(&config).unwrap();

tracing::subscriber::set_global_default(s).unwrap();
println!("Created subscribler for {}", file_out_lossy);

let arcd_h = Arc::new(h);
// Spawn an thread to correct appender paths
let handle_clone = handle;
let file_out_lossy_clone = file_out_lossy.clone().into_owned();
let interval = 500;
thread::spawn(move || loop {
println!("Correcting the append for {}", file_out_lossy_clone);
handle_clone.correct_appender_paths().unwrap();
thread::sleep(Duration::from_millis(interval));
thread::spawn(move || {
loop {
println!("Correcting the append for {}", file_out_lossy_clone);
arcd_h.correct_appender_paths().unwrap();
thread::sleep(Duration::from_millis(interval));
}
});

// Alternate between removing and checking on the file
Expand Down
Loading

0 comments on commit dd70b7c

Please sign in to comment.