Add node name to the log lines (#7328)

* Initial commit

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* Add notes to original source code

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

* Some doc

* Test with trybuild

* Revert "Test with trybuild" (issue with trybuild atm)

This reverts commit 9055ec2206808ba3ddce6e3d87eb358907fa5e42.

https://github.com/dtolnay/trybuild/issues/53

* Apply suggestions

* Rename derive to proc-macro

* Remove "prefix" feature from informant

* Blocking task should use SpawnHandle::spawn_blocking

* Improve doc as suggested

* Fixes

Forked at: 601e2fa139
Parent branch: origin/master

* Apply suggestion

* Update client/cli/proc-macro/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* More suggestions

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* Improve error message

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* Fix async issue

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* CLEANUP

Forked at: 601e2fa139
Parent branch: origin/master

* Add test

* fix doc test

* Update client/cli/src/logging.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/basic-authorship/src/basic_authorship.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/basic-authorship/src/basic_authorship.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Apply suggestions

* Suggestions

* Clarify doc

* WIP

Forked at: 601e2fa139
Parent branch: origin/master

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Cecile Tonglet
2020-10-21 17:13:07 +02:00
committed by GitHub
parent 4e9256aba2
commit 8cebbd142d
20 changed files with 704 additions and 77 deletions
+49 -8
View File
@@ -25,6 +25,7 @@ pub mod arg_enums;
mod commands;
mod config;
mod error;
mod logging;
mod params;
mod runner;
@@ -34,8 +35,9 @@ pub use config::*;
pub use error::*;
pub use params::*;
pub use runner::*;
use sc_service::{Configuration, TaskExecutor};
pub use sc_cli_proc_macro::*;
pub use sc_service::{ChainSpec, Role};
use sc_service::{Configuration, TaskExecutor};
pub use sp_version::RuntimeVersion;
use std::io::Write;
pub use structopt;
@@ -43,10 +45,14 @@ use structopt::{
clap::{self, AppSettings},
StructOpt,
};
#[doc(hidden)]
pub use tracing;
use tracing_subscriber::{
filter::Directive, fmt::time::ChronoLocal, layer::SubscriberExt, FmtSubscriber, Layer,
};
pub use logging::PREFIX_LOG_SPAN;
/// Substrate client CLI
///
/// This trait needs to be defined on the root structopt of the application. It will provide the
@@ -310,13 +316,15 @@ pub fn init_logger(
let subscriber = FmtSubscriber::builder()
.with_env_filter(env_filter)
.with_ansi(enable_color)
.with_target(!simple)
.with_level(!simple)
.with_thread_names(!simple)
.with_timer(timer)
.with_writer(std::io::stderr)
.finish();
.event_format(logging::EventFormat {
timer,
ansi: enable_color,
display_target: !simple,
display_level: !simple,
display_thread_name: !simple,
})
.finish().with(logging::NodeNameLayer);
if let Some(profiling_targets) = profiling_targets {
let profiling = sc_tracing::ProfilingLayer::new(tracing_receiver, &profiling_targets);
@@ -339,8 +347,9 @@ pub fn init_logger(
#[cfg(test)]
mod tests {
use super::*;
use crate as sc_cli;
use std::{env, process::Command};
use tracing::{metadata::Kind, subscriber::Interest, Callsite, Level, Metadata};
use std::{process::Command, env};
#[test]
fn test_logger_filters() {
@@ -409,4 +418,36 @@ mod tests {
log::info!(target: "test-target", "{}", EXPECTED_LOG_MESSAGE);
}
}
const EXPECTED_NODE_NAME: &'static str = "THE_NODE";
#[test]
fn prefix_in_log_lines() {
let executable = env::current_exe().unwrap();
let output = Command::new(executable)
.env("ENABLE_LOGGING", "1")
.args(&["--nocapture", "prefix_in_log_lines_entrypoint"])
.output()
.unwrap();
let output = String::from_utf8(output.stderr).unwrap();
assert!(output.contains(&format!(" [{}] ", EXPECTED_NODE_NAME)));
}
/// This is no actual test, it will be used by the `prefix_in_log_lines` test.
/// The given test will call the test executable to only execute this test that
/// will only print a log line prefixed by the node name `EXPECTED_NODE_NAME`.
#[test]
fn prefix_in_log_lines_entrypoint() {
if env::var("ENABLE_LOGGING").is_ok() {
let test_pattern = "test-target=info";
init_logger(&test_pattern, Default::default(), Default::default()).unwrap();
prefix_in_log_lines_process();
}
}
#[crate::prefix_logs_with(EXPECTED_NODE_NAME)]
fn prefix_in_log_lines_process() {
log::info!("Hello World!");
}
}