Skip to content

Commit cf9f0ad

Browse files
committed
refactor(deployer): new log structure
1 parent 2314c12 commit cf9f0ad

File tree

10 files changed

+215
-513
lines changed

10 files changed

+215
-513
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,9 @@ up: $(DOCKER_COMPOSE_FILES)
196196
$(SHUTTLE_DETACH)
197197

198198
down: $(DOCKER_COMPOSE_FILES)
199-
$(DOCKER_COMPOSE_ENV) $(DOCKER_COMPOSE) $(addprefix -f ,$(DOCKER_COMPOSE_FILES)) -p $(STACK) down
199+
$(DOCKER_COMPOSE_FILES)
200+
$(DOCKER_COMPOSE_ENV) \
201+
$(DOCKER_COMPOSE) \
202+
$(addprefix -f ,$(DOCKER_COMPOSE_FILES)) \
203+
-p $(STACK) \
204+
down

common/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ pub mod database;
66
#[cfg(feature = "service")]
77
pub mod deployment;
88
#[cfg(feature = "service")]
9+
use uuid::Uuid;
10+
#[cfg(feature = "service")]
11+
pub type DeploymentId = Uuid;
12+
#[cfg(feature = "service")]
913
pub mod log;
14+
#[cfg(feature = "service")]
15+
pub use log::Item as LogItem;
1016
#[cfg(feature = "models")]
1117
pub mod models;
1218
#[cfg(feature = "service")]
@@ -22,17 +28,11 @@ pub mod wasm;
2228
use std::collections::BTreeMap;
2329
use std::fmt::Debug;
2430
use std::fmt::Display;
25-
#[cfg(feature = "openapi")]
26-
use utoipa::openapi::{Object, ObjectBuilder};
2731

2832
use anyhow::bail;
29-
#[cfg(feature = "service")]
30-
pub use log::Item as LogItem;
31-
#[cfg(feature = "service")]
32-
pub use log::STATE_MESSAGE;
3333
use serde::{Deserialize, Serialize};
34-
#[cfg(feature = "service")]
35-
use uuid::Uuid;
34+
#[cfg(feature = "openapi")]
35+
use utoipa::openapi::{Object, ObjectBuilder};
3636

3737
#[cfg(debug_assertions)]
3838
pub const API_URL_DEFAULT: &str = "http://localhost:8001";
@@ -42,8 +42,6 @@ pub const API_URL_DEFAULT: &str = "https://api.shuttle.rs";
4242

4343
pub type ApiUrl = String;
4444
pub type Host = String;
45-
#[cfg(feature = "service")]
46-
pub type DeploymentId = Uuid;
4745

4846
#[derive(Clone, Serialize, Deserialize)]
4947
#[cfg_attr(feature = "persist", derive(sqlx::Type, PartialEq, Hash, Eq))]

common/src/log.rs

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,59 @@
1-
#[cfg(feature = "display")]
2-
use std::fmt::Write;
3-
41
use chrono::{DateTime, Utc};
52
#[cfg(feature = "display")]
63
use crossterm::style::{StyledContent, Stylize};
74
use serde::{Deserialize, Serialize};
5+
use strum::EnumString;
86
#[cfg(feature = "openapi")]
97
use utoipa::ToSchema;
108
use uuid::Uuid;
119

12-
use crate::deployment::State;
13-
14-
pub const STATE_MESSAGE: &str = "NEW STATE";
10+
#[derive(Clone, Debug, EnumString, Eq, PartialEq, Deserialize, Serialize)]
11+
#[cfg_attr(feature = "display", derive(strum::Display))]
12+
#[cfg_attr(feature = "openapi", derive(ToSchema))]
13+
pub enum InternalLogOrigin {
14+
Unknown,
15+
Deployer,
16+
// Builder,
17+
// ResourceRecorder,
18+
}
1519

20+
impl Default for InternalLogOrigin {
21+
fn default() -> Self {
22+
Self::Unknown
23+
}
24+
}
1625
#[derive(Clone, Debug, Deserialize, Serialize)]
1726
#[cfg_attr(feature = "openapi", derive(ToSchema))]
1827
#[cfg_attr(feature = "openapi", schema(as = shuttle_common::log::Item))]
1928
pub struct Item {
2029
#[cfg_attr(feature = "openapi", schema(value_type = KnownFormat::Uuid))]
2130
pub id: Uuid,
31+
#[cfg_attr(feature = "openapi", schema(value_type = shuttle_common::log::InternalLogOrigin))]
32+
pub internal_origin: InternalLogOrigin,
2233
#[cfg_attr(feature = "openapi", schema(value_type = KnownFormat::DateTime))]
2334
pub timestamp: DateTime<Utc>,
24-
#[cfg_attr(feature = "openapi", schema(value_type = shuttle_common::deployment::State))]
25-
pub state: State,
26-
#[cfg_attr(feature = "openapi", schema(value_type = shuttle_common::log::Level))]
27-
pub level: Level,
28-
pub file: Option<String>,
29-
pub line: Option<u32>,
30-
pub target: String,
31-
pub fields: Vec<u8>,
35+
pub line: String,
36+
// #[cfg_attr(feature = "openapi", schema(value_type = shuttle_common::deployment::State))]
37+
// pub state: State,
38+
// #[cfg_attr(feature = "openapi", schema(value_type = shuttle_common::log::Level))]
39+
// pub level: Level,
40+
// pub file: Option<String>,
41+
// pub line: Option<u32>,
42+
// pub target: String,
43+
// pub fields: Vec<u8>,
3244
}
3345

3446
#[cfg(feature = "display")]
3547
impl std::fmt::Display for Item {
3648
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3749
let datetime: chrono::DateTime<chrono::Local> = DateTime::from(self.timestamp);
3850

39-
let message = match serde_json::from_slice(&self.fields).unwrap() {
40-
serde_json::Value::String(str_value) if str_value == STATE_MESSAGE => {
41-
writeln!(f)?;
42-
format!("Entering {} state", self.state)
43-
.bold()
44-
.blue()
45-
.to_string()
46-
}
47-
serde_json::Value::Object(map) => {
48-
let mut simple = None;
49-
let mut extra = vec![];
50-
51-
for (key, value) in map.iter() {
52-
match key.as_str() {
53-
"message" => simple = value.as_str(),
54-
_ => extra.push(format!("{key}={value}")),
55-
}
56-
}
57-
58-
let mut output = if extra.is_empty() {
59-
String::new()
60-
} else {
61-
format!("{{{}}} ", extra.join(" "))
62-
};
63-
64-
if !self.target.is_empty() {
65-
let target = format!("{}:", self.target).dim();
66-
write!(output, "{target} ")?;
67-
}
68-
69-
if let Some(msg) = simple {
70-
write!(output, "{msg}")?;
71-
}
72-
73-
output
74-
}
75-
other => other.to_string(),
76-
};
77-
7851
write!(
7952
f,
80-
"{} {} {}",
53+
"{} [{}] {}",
8154
datetime.to_rfc3339().dim(),
82-
self.level.get_colored(),
83-
message
55+
self.internal_origin,
56+
self.line,
8457
)
8558
}
8659
}
@@ -139,16 +112,9 @@ mod tests {
139112
fn test_timezone_formatting() {
140113
let item = Item {
141114
id: Uuid::new_v4(),
115+
internal_origin: InternalLogOrigin::Deployer,
142116
timestamp: Utc::now(),
143-
state: State::Building,
144-
level: Level::Info,
145-
file: None,
146-
line: None,
147-
target: "shuttle::build".to_string(),
148-
fields: serde_json::to_vec(&serde_json::json!({
149-
"message": "Building",
150-
}))
151-
.unwrap(),
117+
line: r#"{"message": "Building"}"#.to_owned(),
152118
};
153119

154120
with_tz("CEST", || {

0 commit comments

Comments
 (0)