Skip to content

Allow building a Logger yourself #53

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

Merged
merged 4 commits into from
Jan 16, 2018
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
40 changes: 40 additions & 0 deletions examples/direct_logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
Using `env_logger::Logger` and the `log::Log` trait directly.

This example doesn't rely on environment variables, or having a static logger installed.
*/

extern crate log;
extern crate env_logger;

fn record() -> log::Record<'static> {
let error_metadata = log::MetadataBuilder::new()
.target("myApp")
.level(log::Level::Error)
.build();

log::Record::builder()
.metadata(error_metadata)
.args(format_args!("Error!"))
.line(Some(433))
.file(Some("app.rs"))
.module_path(Some("server"))
.build()
}

fn main() {
use log::Log;

let stylish_logger = env_logger::Builder::new()
.filter(None, log::LevelFilter::Error)
.write_style(env_logger::WriteStyle::Always)
.build();

let unstylish_logger = env_logger::Builder::new()
.filter(None, log::LevelFilter::Error)
.write_style(env_logger::WriteStyle::Never)
.build();

stylish_logger.log(&record());
unstylish_logger.log(&record());
}
26 changes: 22 additions & 4 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub use termcolor::Color;
/// [`style`]: #method.style
pub struct Formatter {
buf: Rc<RefCell<Buffer>>,
write_style: WriteStyle,
}

/// A set of styles to apply to the terminal output.
Expand Down Expand Up @@ -179,7 +180,16 @@ impl Default for WriteStyle {
}

/// A terminal target with color awareness.
pub(crate) struct Writer(BufferWriter);
pub(crate) struct Writer {
inner: BufferWriter,
write_style: WriteStyle,
}

impl Writer {
pub(crate) fn write_style(&self) -> WriteStyle {
self.write_style
}
}

/// A builder for a terminal writer.
///
Expand Down Expand Up @@ -232,7 +242,10 @@ impl Builder {
Target::Stdout => BufferWriter::stdout(color_choice),
};

Writer(writer)
Writer {
inner: writer,
write_style: self.write_style,
}
}
}

Expand Down Expand Up @@ -355,10 +368,15 @@ impl Style {
impl Formatter {
pub(crate) fn new(writer: &Writer) -> Self {
Formatter {
buf: Rc::new(RefCell::new(writer.0.buffer())),
buf: Rc::new(RefCell::new(writer.inner.buffer())),
write_style: writer.write_style(),
}
}

pub(crate) fn write_style(&self) -> WriteStyle {
self.write_style
}

/// Begin a new [`Style`].
///
/// # Examples
Expand Down Expand Up @@ -414,7 +432,7 @@ impl Formatter {
}

pub(crate) fn print(&self, writer: &Writer) -> io::Result<()> {
writer.0.print(&self.buf.borrow())
writer.inner.print(&self.buf.borrow())
}

pub(crate) fn clear(&mut self) {
Expand Down
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A simple logger configured via an environment variable which writes
//! A simple logger configured via environment variables which writes
//! to stdout or stderr, for use with the logging facade exposed by the
//! [`log` crate][log-crate-url].
//!
Expand Down Expand Up @@ -426,7 +426,7 @@ impl Builder {
/// This method is kept private because the only way we support building
/// loggers is by installing them as the single global logger for the
/// `log` crate.
fn build(&mut self) -> Logger {
pub fn build(&mut self) -> Logger {
Logger {
writer: self.writer.build(),
filter: self.filter.build(),
Expand Down Expand Up @@ -460,11 +460,9 @@ impl Log for Logger {
// so will always at least have capacity for the largest log record formatted
// on that thread.
//
// Because these buffers are tied to a particular logger, we don't let callers
// create instances of `Logger` themselves, or they'll race to configure the
// thread local buffer with their own configuration. This is still potentially
// an issue if a caller attempts to set and use the global logger multiple times,
// but in that case it's clearer that there's shared state at play.
// If multiple `Logger`s are used by the same threads then the thread-local
// formatter might have different color support. If this is the case the
// formatter and its buffer are discarded and recreated.

thread_local! {
static FORMATTER: RefCell<Option<Formatter>> = RefCell::new(None);
Expand All @@ -473,8 +471,15 @@ impl Log for Logger {
FORMATTER.with(|tl_buf| {
let mut tl_buf = tl_buf.borrow_mut();

if tl_buf.is_none() {
*tl_buf = Some(Formatter::new(&self.writer));
// Check the buffer style. If it's different from the logger's
// style then drop the buffer and recreate it.
match *tl_buf {
Some(ref mut formatter) => {
if formatter.write_style() != self.writer.write_style() {
*formatter = Formatter::new(&self.writer)
}
},
ref mut tl_buf => *tl_buf = Some(Formatter::new(&self.writer))
}

// The format is guaranteed to be `Some` by this point
Expand Down