Skip to content

Commit

Permalink
Use colors for log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gshuflin committed Jul 14, 2020
1 parent 248a903 commit 568329f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/engine/internals/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def lib(self):
def decompress_tarball(self, tarfile_path, dest_dir):
return self.lib.decompress_tarball(tarfile_path, dest_dir)

def init_rust_logging(self, level, log_show_rust_3rdparty):
return self.lib.init_logging(level, log_show_rust_3rdparty)
def init_rust_logging(self, level, log_show_rust_3rdparty: bool, use_color: bool):
return self.lib.init_logging(level, log_show_rust_3rdparty, use_color)

def setup_pantsd_logger(self, log_file_path, level):
return self.lib.setup_pantsd_logger(log_file_path, level)
Expand Down
9 changes: 6 additions & 3 deletions src/python/pants/init/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
logging.addLevelName(pants_logging.TRACE, "TRACE")


def init_rust_logger(log_level: LogLevel, log_show_rust_3rdparty: bool) -> None:
Native().init_rust_logging(log_level.level, log_show_rust_3rdparty)
def init_rust_logger(log_level: LogLevel, log_show_rust_3rdparty: bool, use_color: bool) -> None:
Native().init_rust_logging(log_level.level, log_show_rust_3rdparty, use_color)


class NativeHandler(StreamHandler):
Expand Down Expand Up @@ -131,7 +131,10 @@ def setup_logging(global_bootstrap_options):
level = LogLevel.ERROR if getattr(global_bootstrap_options, "quiet", False) else global_level
log_dir = global_bootstrap_options.logdir

Native().init_rust_logging(level.level, global_bootstrap_options.log_show_rust_3rdparty)
log_show_rust_3rdparty = global_bootstrap_options.log_show_rust_3rdparty
use_color = global_bootstrap_options.colors

init_rust_logger(level, log_show_rust_3rdparty, use_color)
setup_logging_to_stderr(level, warnings_filter_regexes=ignores)
if log_dir:
setup_logging_to_file(global_level, log_dir=log_dir, warnings_filter_regexes=ignores)
Expand Down
15 changes: 8 additions & 7 deletions src/python/pants/option/global_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ def register_bootstrap_options(cls, register):
"engine.",
)

register(
"--colors",
type=bool,
default=sys.stdout.isatty(),
recursive=True,
help="Set whether log messages are displayed in color.",
)

register(
"--v1",
advanced=True,
Expand Down Expand Up @@ -860,13 +868,6 @@ def register_options(cls, register):
# global-scope options, for convenience.
cls.register_bootstrap_options(register)

register(
"--colors",
type=bool,
default=sys.stdout.isatty(),
help="Set whether log messages are displayed in color.",
)

register(
"--tag",
type=list,
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/pantsd/pants_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def _pantsd_logging(self) -> Iterator[IO[str]]:
# for further forks.
with stdio_as(stdin_fd=-1, stdout_fd=-1, stderr_fd=-1):
# Reinitialize logging for the daemon context.
init_rust_logger(self._log_level, self._log_show_rust_3rdparty)
use_color = self._bootstrap_options.for_global_scope().colors
init_rust_logger(self._log_level, self._log_show_rust_3rdparty, use_color=use_color)

level = self._log_level
ignores = self._bootstrap_options.for_global_scope().ignore_pants_warnings
Expand Down
12 changes: 12 additions & 0 deletions src/rust/engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/rust/engine/logging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors = [ "Pants Build <pantsbuild@gmail.com>" ]
publish = false

[dependencies]
colored = "1.9.0"
chrono = "0.4.10"
lazy_static = "1"
log = "0.4"
Expand Down
25 changes: 23 additions & 2 deletions src/rust/engine/logging/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::PythonLogLevel;

use colored::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
Expand Down Expand Up @@ -31,6 +32,7 @@ lazy_static! {
pub struct Logger {
pantsd_log: Mutex<MaybeWriteLogger<File>>,
stderr_log: Mutex<MaybeWriteLogger<Stderr>>,
use_color: AtomicBool,
show_rust_3rdparty_logs: AtomicBool,
stderr_handlers: Mutex<HashMap<Uuid, StdioHandler>>,
}
Expand All @@ -41,16 +43,18 @@ impl Logger {
pantsd_log: Mutex::new(MaybeWriteLogger::empty()),
stderr_log: Mutex::new(MaybeWriteLogger::empty()),
show_rust_3rdparty_logs: AtomicBool::new(true),
use_color: AtomicBool::new(true),
stderr_handlers: Mutex::new(HashMap::new()),
}
}

pub fn init(max_level: u64, show_rust_3rdparty_logs: bool) {
pub fn init(max_level: u64, show_rust_3rdparty_logs: bool, use_color: bool) {
let max_python_level: Result<PythonLogLevel, _> = max_level.try_into();
match max_python_level {
Ok(python_level) => {
let level: log::LevelFilter = python_level.into();
set_max_level(level);
LOGGER.use_color.store(use_color, Ordering::SeqCst);
LOGGER
.show_rust_3rdparty_logs
.store(show_rust_3rdparty_logs, Ordering::SeqCst);
Expand Down Expand Up @@ -155,6 +159,7 @@ impl Log for Logger {

fn log(&self, record: &Record) {
use chrono::Timelike;
use log::Level;
let destination = get_destination();
match destination {
Destination::Stderr => {
Expand All @@ -164,8 +169,24 @@ impl Log for Logger {
cur_date.format(TIME_FORMAT_STR),
cur_date.time().nanosecond() / 10_000_000 // two decimal places of precision
);

let level = record.level();
let log_string: String = format!("{} [{}] {}", time_str, level, record.args());
let use_color = self.use_color.load(Ordering::SeqCst);

let level_marker = match level {
_ if !use_color => format!("[{}]", level).normal().clear(),
Level::Info => format!("[{}]", level).normal().bold(),
Level::Error | Level::Warn => format!("[{}]", level).red().bold(),
Level::Debug => format!("[{}]", level).green(),
Level::Trace => format!("[{}]", level).magenta(),
};

let record_args = match level {
Level::Error => format!("{}", record.args()).red(),
_ => format!("{}", record.args()).normal().clear(),
};

let log_string = format!("{} {} {}", time_str, level_marker, record_args);

{
// If there are no handlers, or sending to any of the handlers failed, send to stderr
Expand Down
11 changes: 8 additions & 3 deletions src/rust/engine/src/externs/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ py_module_initializer!(native_engine, |py, m| {
m.add(
py,
"init_logging",
py_fn!(py, init_logging(a: u64, b: bool)),
py_fn!(py, init_logging(a: u64, b: bool, c: bool)),
)?;
m.add(
py,
Expand Down Expand Up @@ -1649,8 +1649,13 @@ fn materialize_directories(
})
}

fn init_logging(_: Python, level: u64, show_rust_3rdparty_logs: bool) -> PyUnitResult {
Logger::init(level, show_rust_3rdparty_logs);
fn init_logging(
_: Python,
level: u64,
show_rust_3rdparty_logs: bool,
use_color: bool,
) -> PyUnitResult {
Logger::init(level, show_rust_3rdparty_logs, use_color);
Ok(None)
}

Expand Down
1 change: 1 addition & 0 deletions tests/python/pants_test/init/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def setUpClass(cls) -> None:
# verbosity as necessary.
level=LogLevel.ERROR.level,
log_show_rust_3rdparty=False,
use_color=False,
)

@contextmanager
Expand Down

0 comments on commit 568329f

Please sign in to comment.