Skip to content

Commit 6d312e7

Browse files
committed
Add timestamp to module logs
1 parent 4b9f30f commit 6d312e7

File tree

7 files changed

+21
-1
lines changed

7 files changed

+21
-1
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ spacetimedb-standalone = { path = "../standalone", version = "0.8.0", optional =
2424
anyhow.workspace = true
2525
base64.workspace = true
2626
cargo_metadata.workspace = true
27+
chrono.workspace = true
2728
clap = {workspace = true, features = ["derive", "env", "string"]}
2829
colored.workspace = true
2930
convert_case.workspace = true
@@ -43,6 +44,7 @@ reqwest.workspace = true
4344
rustyline.workspace = true
4445
serde = { workspace = true, features = ["derive"] }
4546
serde_json = { workspace = true, features = ["raw_value", "preserve_order"] }
47+
serde_with = { workspace = true, features = ["chrono_0_4"] }
4648
slab.workspace = true
4749
syntect.workspace = true
4850
tabled.workspace = true

crates/cli/src/subcommands/logs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ pub enum LogLevel {
5656
Panic,
5757
}
5858

59+
#[serde_with::serde_as]
5960
#[derive(serde::Deserialize)]
6061
struct Record<'a> {
62+
#[serde_as(as = "Option<serde_with::TimestampMicroSeconds>")]
63+
ts: Option<chrono::DateTime<chrono::Utc>>, // TODO: remove Option once 0.9 has been out for a while
6164
level: LogLevel,
6265
#[serde(borrow)]
6366
#[allow(unused)] // TODO: format this somehow
@@ -124,6 +127,9 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
124127
while rdr.read_line(&mut line).await? != 0 {
125128
let record = serde_json::from_str::<Record<'_>>(&line)?;
126129

130+
if let Some(ts) = record.ts {
131+
write!(out, "{ts} ")?;
132+
}
127133
let mut color = ColorSpec::new();
128134
let level = match record.level {
129135
LogLevel::Error => {

crates/core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ backtrace.workspace = true
3131
base64.workspace = true
3232
bytes.workspace = true
3333
bytestring.workspace = true
34+
chrono.workspace = true
3435
clap.workspace = true
3536
crossbeam-channel.workspace = true
3637
derive_more.workspace = true
@@ -67,7 +68,7 @@ sendgrid.workspace = true
6768
serde.workspace = true
6869
serde_json.workspace = true
6970
serde_path_to_error.workspace = true
70-
serde_with.workspace = true
71+
serde_with = { workspace = true, features = ["chrono_0_4"] }
7172
sha1.workspace = true
7273
slab.workspace = true
7374
sled.workspace = true

crates/core/src/database_logger.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ impl From<u8> for LogLevel {
3737
}
3838

3939
#[serde_with::skip_serializing_none]
40+
#[serde_with::serde_as]
4041
#[derive(serde::Serialize, Copy, Clone)]
4142
pub struct Record<'a> {
43+
#[serde_as(as = "serde_with::TimestampMicroSeconds")]
44+
pub ts: chrono::DateTime<chrono::Utc>,
4245
pub target: Option<&'a str>,
4346
pub filename: Option<&'a str>,
4447
pub line_number: Option<u32>,
@@ -227,6 +230,7 @@ impl SystemLogger {
227230

228231
fn record(message: &str) -> Record {
229232
Record {
233+
ts: chrono::Utc::now(),
230234
target: None,
231235
filename: Some("spacetimedb"),
232236
line_number: None,

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl<T: WasmModule> Module for WasmModuleHostActor<T> {
231231
self.database_instance_context.logger.write(
232232
log_level,
233233
&Record {
234+
ts: chrono::Utc::now(),
234235
target: None,
235236
filename: Some("external"),
236237
line_number: None,

crates/core/src/host/wasmtime/wasm_instance_env.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ impl WasmInstanceEnv {
344344
let line_number = (line_number != u32::MAX).then_some(line_number);
345345

346346
let record = Record {
347+
// TODO: figure out whether to use walltime now or logical reducer now (env.reducer_start)
348+
ts: chrono::Utc::now(),
347349
target: target.as_deref(),
348350
filename: filename.as_deref(),
349351
line_number,
@@ -754,6 +756,7 @@ impl WasmInstanceEnv {
754756
let message = format!("Timing span {:?}: {:?}", name, elapsed);
755757

756758
let record = Record {
759+
ts: chrono::Utc::now(),
757760
target: None,
758761
filename: None,
759762
line_number: None,

0 commit comments

Comments
 (0)