Skip to content

Commit

Permalink
Move log into a proper module
Browse files Browse the repository at this point in the history
Implemented LogMessageId, LogMessage and LogMessageBuilder
Implemented Display for LogMessageId and LogMessage
  • Loading branch information
danielsanchezq committed Aug 3, 2020
1 parent a3ab2bf commit 756743a
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vit-servicing-station-lib/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ diesel = { version = "1.4.4", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.4.0"
dotenv = "0.9.0"
itertools = "0.9.0"
log = "0.4.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.53"
simplelog = "0.8.0"
Expand Down
1 change: 1 addition & 0 deletions vit-servicing-station-lib/src/lib.rs
Expand Up @@ -7,6 +7,7 @@ extern crate structopt;
extern crate diesel_migrations;

pub mod db;
pub mod logging;
pub mod server;
pub mod utils;
pub mod v0;
File renamed without changes.
95 changes: 95 additions & 0 deletions vit-servicing-station-lib/src/logging/messages.rs
@@ -0,0 +1,95 @@
use serde::export::Formatter;

pub enum LogMessageId {
None,
Other(String),
}

pub struct LogMessage {
id: LogMessageId,
tags: Vec<String>,
message: String,
timestamp: i64,
}

pub struct LogMessageBuilder {
id: LogMessageId,
tags: Vec<String>,
message: Option<String>,
}

impl std::fmt::Display for LogMessageId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let id = match self {
LogMessageId::None => "None",
LogMessageId::Other(id) => id,
};
write!(f, "{}", id)
}
}

impl LogMessageBuilder {
pub fn new() -> Self {
Self {
id: LogMessageId::None,
tags: vec![],
message: None,
}
}

pub fn with_id(self, id: LogMessageId) -> Self {
Self {
id,
tags: self.tags,
message: self.message,
}
}

pub fn with_tags(self, tags: Vec<String>) -> Self {
Self {
id: self.id,
tags,
message: self.message,
}
}

pub fn with_message(self, message: String) -> Self {
Self {
id: self.id,
tags: self.tags,
message: Some(message),
}
}

pub fn build(self) -> LogMessage {
LogMessage {
id: self.id,
tags: self.tags,
message: self.message.unwrap_or(Default::default()),
timestamp: chrono::Utc::now().timestamp(),
}
}
}

impl LogMessage {
pub fn new(id: LogMessageId, message: String, tags: Vec<String>) -> Self {
Self {
id,
tags,
message,
timestamp: chrono::Utc::now().timestamp(),
}
}
}

impl std::fmt::Display for LogMessage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let tags = format!("[{}]", self.tags.join(":"));
let id = format!("[{}]", self.id);
write!(
f,
"{} - {} - {} - {}",
id, tags, self.timestamp, self.message
)
}
}
2 changes: 2 additions & 0 deletions vit-servicing-station-lib/src/logging/mod.rs
@@ -0,0 +1,2 @@
pub mod config;
pub mod messages;
1 change: 0 additions & 1 deletion vit-servicing-station-lib/src/server/mod.rs
@@ -1,6 +1,5 @@
pub mod bootstrapping;
pub mod exit_codes;
pub mod log;
pub mod settings;

pub use bootstrapping::start_server;
1 change: 1 addition & 0 deletions vit-servicing-station-lib/src/v0/endpoints/mod.rs
Expand Up @@ -38,6 +38,7 @@ pub async fn filter(
let playground_filter = graphql::playground::filter(playground_root.boxed());

let api_token_filter = api_token::api_token_filter(context).await;

root.and(
playground_filter.or(api_token_filter.and(
health_filter
Expand Down
6 changes: 3 additions & 3 deletions vit-servicing-station-server/src/main.rs
@@ -1,6 +1,6 @@
use structopt::StructOpt;
use vit_servicing_station_lib::{
db, server, server::exit_codes::ApplicationExitCode, server::log::config_log,
db, logging::config::config_log, server, server::exit_codes::ApplicationExitCode,
server::settings as server_settings, server::settings::ServiceSettings, v0,
};

Expand Down Expand Up @@ -29,14 +29,14 @@ async fn main() {
std::process::exit(0);
}

// setup log
// setup logging
config_log(
settings.log.log_level.into(),
settings.log.log_output_path.clone(),
None,
)
.unwrap_or_else(|e| {
log::error!("Error setting up log: {}", e);
log::error!("Error setting up logging: {}", e);
std::process::exit(ApplicationExitCode::LoadSettingsError.into())
});

Expand Down

0 comments on commit 756743a

Please sign in to comment.