Introduce structured logging
We're enabling the log crate feature everywhere, but only using it on the server for now. Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
co-authored by
Antonio Scandurra
parent
206b0f0f8c
commit
7e5a3f9f6b
@@ -1,7 +1,8 @@
|
||||
use crate::{AppState, Request, RequestExt as _};
|
||||
use serde::Deserialize;
|
||||
use log::as_serde;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use tide::{http::mime, log, Server};
|
||||
use tide::{http::mime, Server};
|
||||
|
||||
pub fn add_routes(app: &mut Server<Arc<AppState>>) {
|
||||
app.at("/").get(get_home);
|
||||
@@ -18,7 +19,7 @@ async fn get_home(mut request: Request) -> tide::Result {
|
||||
}
|
||||
|
||||
async fn post_signup(mut request: Request) -> tide::Result {
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
struct Form {
|
||||
github_login: String,
|
||||
email_address: String,
|
||||
@@ -38,7 +39,7 @@ async fn post_signup(mut request: Request) -> tide::Result {
|
||||
.map(str::to_string)
|
||||
.unwrap_or(form.github_login);
|
||||
|
||||
log::info!("Signup submitted: {:?}", form);
|
||||
log::info!(form = as_serde!(form); "signup submitted");
|
||||
|
||||
// Save signup in the database
|
||||
request
|
||||
|
||||
@@ -27,7 +27,7 @@ use rust_embed::RustEmbed;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use surf::http::cookies::SameSite;
|
||||
use tide::{log, sessions::SessionMiddleware};
|
||||
use tide::sessions::SessionMiddleware;
|
||||
use tide_compress::CompressMiddleware;
|
||||
|
||||
type Request = tide::Request<Arc<AppState>>;
|
||||
@@ -138,7 +138,11 @@ struct LayoutData {
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> tide::Result<()> {
|
||||
log::start();
|
||||
if std::env::var("LOG_JSON").is_ok() {
|
||||
json_env_logger::init();
|
||||
} else {
|
||||
tide::log::start();
|
||||
}
|
||||
|
||||
if let Err(error) = env::load_dotenv() {
|
||||
log::error!(
|
||||
|
||||
@@ -11,6 +11,7 @@ use async_std::task;
|
||||
use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream};
|
||||
use collections::{HashMap, HashSet};
|
||||
use futures::{channel::mpsc, future::BoxFuture, FutureExt, SinkExt, StreamExt};
|
||||
use log::{as_debug, as_display};
|
||||
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use rpc::{
|
||||
proto::{self, AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage},
|
||||
@@ -25,7 +26,6 @@ use std::{
|
||||
};
|
||||
use store::{Store, Worktree};
|
||||
use surf::StatusCode;
|
||||
use tide::log;
|
||||
use tide::{
|
||||
http::headers::{HeaderName, CONNECTION, UPGRADE},
|
||||
Request, Response,
|
||||
@@ -218,16 +218,16 @@ impl Server {
|
||||
if let Some(message) = message {
|
||||
let start_time = Instant::now();
|
||||
let type_name = message.payload_type_name();
|
||||
log::info!("rpc message received. connection:{}, type:{}", connection_id, type_name);
|
||||
log::info!(connection_id = connection_id.0, type_name = type_name; "rpc message received");
|
||||
if let Some(handler) = this.handlers.get(&message.payload_type_id()) {
|
||||
let notifications = this.notifications.clone();
|
||||
let is_background = message.is_background();
|
||||
let handle_message = (handler)(this.clone(), message);
|
||||
let handle_message = async move {
|
||||
if let Err(err) = handle_message.await {
|
||||
log::error!("rpc message error. connection:{}, type:{}, error:{:?}", connection_id, type_name, err);
|
||||
log::error!(connection_id = connection_id.0, type = type_name, error = as_display!(err); "rpc message error");
|
||||
} else {
|
||||
log::info!("rpc message handled. connection:{}, type:{}, duration:{:?}", connection_id, type_name, start_time.elapsed());
|
||||
log::info!(connection_id = connection_id.0, type = type_name, duration = as_debug!(start_time.elapsed()); "rpc message handled");
|
||||
}
|
||||
if let Some(mut notifications) = notifications {
|
||||
let _ = notifications.send(()).await;
|
||||
@@ -242,7 +242,7 @@ impl Server {
|
||||
log::warn!("unhandled message: {}", type_name);
|
||||
}
|
||||
} else {
|
||||
log::info!("rpc connection closed {:?}", addr);
|
||||
log::info!(address = as_debug!(addr); "rpc connection closed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user