Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added

* Option `flush` to enable flushing of the `io::Write` after each record.

## 2.2.0 - 2017-12-10
### Added

Expand Down
13 changes: 13 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl<S> slog::Serializer for SerdeSerializer<S>
/// to a given `io`
pub struct Json<W: io::Write> {
newlines: bool,
flush: bool,
values: Vec<OwnedKVList>,
io: RefCell<W>,
pretty: bool,
Expand Down Expand Up @@ -238,6 +239,9 @@ impl<W> slog::Drain for Json<W>
if self.newlines {
try!(io.write_all("\n".as_bytes()));
}
if self.flush {
io.flush()?;
}
Ok(())
}
}
Expand All @@ -250,6 +254,7 @@ impl<W> slog::Drain for Json<W>
/// Create with `Json::new`.
pub struct JsonBuilder<W: io::Write> {
newlines: bool,
flush: bool,
values: Vec<OwnedKVList>,
io: W,
pretty: bool,
Expand All @@ -261,6 +266,7 @@ impl<W> JsonBuilder<W>
fn new(io: W) -> Self {
JsonBuilder {
newlines: true,
flush: false,
values: vec![],
io: io,
pretty: false,
Expand All @@ -274,6 +280,7 @@ impl<W> JsonBuilder<W>
Json {
values: self.values,
newlines: self.newlines,
flush: self.flush,
io: RefCell::new(self.io),
pretty: self.pretty,
}
Expand All @@ -285,6 +292,12 @@ impl<W> JsonBuilder<W>
self
}

/// Enable flushing of the `io::Write` after every log record
pub fn set_flush(mut self, enabled: bool) -> Self {
self.flush = enabled;
self
}

/// Set whether or not pretty formatted logging should be used
pub fn set_pretty(mut self, enabled: bool) -> Self {
self.pretty = enabled;
Expand Down