Skip to content

Commit

Permalink
refactor: use jiff for timestamp (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Aug 12, 2024
1 parent 0543373 commit 8f7d8c7
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 144 deletions.
17 changes: 6 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/cratesland/logforth"
rust-version = "1.71.0"
version = "0.8.0"
version = "0.9.0"

categories = ["development-tools::debugging"]
keywords = ["logging", "log", "opentelemetry", "fastrace"]
Expand All @@ -34,27 +34,27 @@ rustdoc-args = ["--cfg", "docs"]

[features]
fastrace = ["dep:fastrace"]
json = ["dep:serde_json", "dep:serde"]
json = ["dep:serde_json", "dep:serde", "jiff/serde"]
no-color = ["colored/no-color"]
opentelemetry = [
"dep:opentelemetry",
"dep:opentelemetry-otlp",
"dep:opentelemetry_sdk",
]
rolling_file = ["dep:crossbeam-channel", "dep:parking_lot", "dep:time"]
rolling_file = ["dep:crossbeam-channel", "dep:parking_lot"]

[dependencies]
anyhow = { version = "1.0" }
colored = { version = "2.1" }
humantime = { version = "2.1" }
jiff = { version = "0.1.5" }
log = { version = "0.4", features = ["std", "kv_unstable"] }
paste = { version = "1.0" }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
rand = "0.8.5"
tempfile = "3.3"
rand = "0.8"
tempfile = "3.12"

## Rolling file dependencies
[dependencies.crossbeam-channel]
Expand All @@ -65,11 +65,6 @@ version = "0.5"
optional = true
version = "0.12"

[dependencies.time]
features = ["formatting", "parsing", "macros"]
optional = true
version = "0.3"

## Fastrace dependencies
[dependencies.fastrace]
optional = true
Expand Down
2 changes: 1 addition & 1 deletion examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
.dispatch(
Dispatch::new()
.filter(LevelFilter::Trace)
.layout(JsonLayout)
.layout(JsonLayout::default())
.append(append::Stdout),
)
.apply()
Expand Down
2 changes: 1 addition & 1 deletion examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() {
.dispatch(
Dispatch::new()
.filter(LevelFilter::Trace)
.layout(JsonLayout)
.layout(JsonLayout::default())
.append(RollingFile::new(writer)),
)
.dispatch(Dispatch::new().layout(TextLayout::default()).append(Stdout))
Expand Down
5 changes: 2 additions & 3 deletions src/append/fastrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::time::SystemTime;

use jiff::Zoned;
use log::Record;

use crate::append::Append;
Expand All @@ -27,7 +26,7 @@ impl Append for FastraceEvent {
fn append(&self, record: &Record) -> anyhow::Result<()> {
let message = format!(
"{} {:>5} {}{}",
humantime::format_rfc3339_micros(SystemTime::now()),
Zoned::now(),
record.level(),
record.args(),
KvDisplay::new(record.key_values()),
Expand Down
1 change: 0 additions & 1 deletion src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub use self::opentelemetry::OpentelemetryLog;
pub use self::rolling_file::RollingFile;
pub use self::stdio::Stderr;
pub use self::stdio::Stdout;

use crate::layout::IdenticalLayout;
use crate::layout::Layout;

Expand Down
38 changes: 19 additions & 19 deletions src/append/rolling_file/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use time::OffsetDateTime;
use jiff::Zoned;

#[derive(Debug)]
pub enum Clock {
Expand All @@ -22,18 +22,18 @@ pub enum Clock {
}

impl Clock {
pub fn now(&self) -> OffsetDateTime {
pub fn now(&self) -> Zoned {
match self {
Clock::DefaultClock => OffsetDateTime::now_utc(),
Clock::DefaultClock => Zoned::now(),
#[cfg(test)]
Clock::ManualClock(clock) => clock.now(),
}
}

#[cfg(test)]
pub fn set_now(&mut self, new_time: OffsetDateTime) {
pub fn set_now(&mut self, now: Zoned) {
if let Clock::ManualClock(clock) = self {
clock.set_now(new_time);
clock.set_now(now);
}
}
}
Expand All @@ -42,38 +42,38 @@ impl Clock {
#[derive(Debug)]
#[cfg(test)]
pub struct ManualClock {
fixed_time: OffsetDateTime,
now: Zoned,
}

#[cfg(test)]
impl ManualClock {
pub fn new(fixed_time: OffsetDateTime) -> ManualClock {
ManualClock { fixed_time }
pub fn new(now: Zoned) -> ManualClock {
ManualClock { now }
}

fn now(&self) -> OffsetDateTime {
self.fixed_time
fn now(&self) -> Zoned {
self.now.clone()
}

pub fn set_now(&mut self, new_time: OffsetDateTime) {
self.fixed_time = new_time;
pub fn set_now(&mut self, now: Zoned) {
self.now = now;
}
}

#[cfg(test)]
mod tests {
use time::macros::datetime;
use std::str::FromStr;

use super::*;

#[test]
fn test_manual_clock_adjusting() {
let mut clock = ManualClock {
fixed_time: datetime!(2023-01-01 12:00:00 UTC),
};
assert_eq!(clock.now(), datetime!(2023-01-01 12:00:00 UTC));
let now = Zoned::from_str("2024-08-10T17:12:52+08[+08]").unwrap();
let mut clock = ManualClock { now: now.clone() };
assert_eq!(clock.now(), now);

clock.set_now(datetime!(2024-01-01 12:00:00 UTC));
assert_eq!(clock.now(), datetime!(2024-01-01 12:00:00 UTC));
let now = Zoned::from_str("2024-01-01T12:00:00+08[+08]").unwrap();
clock.set_now(now.clone());
assert_eq!(clock.now(), now);
}
}
Loading

0 comments on commit 8f7d8c7

Please sign in to comment.