Skip to content

Commit

Permalink
feat(native-app): migrate to tracing library for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Aug 21, 2022
1 parent 834958b commit e4f522f
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 83 deletions.
376 changes: 311 additions & 65 deletions native-app/Cargo.lock

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions native-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ version = "0.1.0"
edition = "2021"

[dependencies]
async-graphql = { version="4.0.1", features=["log"] }
async-graphql = { version="4.0.1", features=["tracing"] }
async-graphql-axum = "4.0.1"
axum = { version = "0.5.4", default-features = false }
env_logger = "0.9.0"
log = "0.4.17"
mystic_light_sdk = { version="0.4.0", features=["async-graphql"] }
file-rotate = "0.7.0"
mystic_light_sdk = { version="0.4.1", features=["async-graphql"] }
portpicker = { version="0.1.1" }
serde = { version="1.0.138", features=["derive"] }
serde_json = "1.0.82"
tokio = { version="1.20.0", features=["rt-multi-thread"] }
tower-http = { version = "0.3.4", features = ["cors"] }
tower-http = { version = "0.3.4", features = ["cors", "trace"] }
tracing = "0.1.36"
tracing-subscriber = { version = "0.3.15", features=["env-filter"] }

[features]
default = []
9 changes: 9 additions & 0 deletions native-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Debug

### Enable additional logging

Enable these tracing modules:
- mystic_light_browser_cinema - logs from the app itself
- mystic_light_sdk - logs from the underlying sdk library
- tower_http - web-server implementation used under hood
- async_graphql - graphql library used in the app
6 changes: 5 additions & 1 deletion native-app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use async_graphql_axum::{GraphQLBatchRequest, GraphQLResponse};
use axum::{extract::Extension, routing::get, routing::post, Router};
use mystic_light_sdk::{CommonError, MysticLightSDK};
use tower_http::cors::{Any, CorsLayer};
use tower_http::{
cors::{Any, CorsLayer},
trace::TraceLayer,
};

use crate::graphql::{create_qraphql_schema, MysticLightSchema};

Expand Down Expand Up @@ -30,6 +33,7 @@ pub fn create_app() -> Result<Router, CommonError> {
Ok(Router::new()
.route("/mystic_light", post(graphql))
.route("/healthz", get(healthz))
.layer(TraceLayer::new_for_http())
.layer(Extension(schema))
.layer(
CorsLayer::new()
Expand Down
54 changes: 54 additions & 0 deletions native-app/src/bin/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use mystic_light_browser_cinema::start_server;
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::fmt::format::FmtSpan;

const LOG_DEFAULT_DIRECTIVES: &str = "mystic_light_sdk=debug,mystic_light_browser_cinema=debug";

fn main() {
let env_filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new(LOG_DEFAULT_DIRECTIVES));

#[cfg(debug_assertions)]
tracing_subscriber::fmt()
.pretty()
.with_env_filter(env_filter)
.with_span_events(FmtSpan::FULL)
.init();

#[cfg(not(debug_assertions))]
{
const LOG_FILES_LIMIT: usize = 5;
const LOG_FILE_BYTES_LIMIT: usize = 20 * 1024 * 1024;
const LOG_DIR_NAME: &str = "logs";
const LOG_FILE_NAME: &str = "log.txt";

use file_rotate::{
compression::Compression, suffix::AppendCount, ContentLimit, FileRotate,
};
use std::env;
use std::sync::Mutex;

let mut log_dir = env::current_exe().unwrap();

log_dir.pop();
log_dir.push(LOG_DIR_NAME);
log_dir.push(LOG_FILE_NAME);

let log_rotation = FileRotate::new(
log_dir,
AppendCount::new(LOG_FILES_LIMIT),
ContentLimit::Bytes(LOG_FILE_BYTES_LIMIT),
Compression::OnRotate(0),
);

tracing_subscriber::fmt()
.pretty()
.with_env_filter(env_filter)
.with_ansi(false)
.with_span_events(FmtSpan::FULL)
.with_writer(Mutex::new(log_rotation))
.init();
}

start_server();
}
6 changes: 3 additions & 3 deletions native-app/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs;

use async_graphql::{extensions::Logger, EmptySubscription, Schema};
use async_graphql::{extensions::Tracing, EmptySubscription, Schema};
use mystic_light_sdk::{
build_graphql_schema, MysticLightGraphqlMutation, MysticLightGraphqlQuery, MysticLightSDK,
};
Expand All @@ -12,11 +12,11 @@ pub fn create_qraphql_schema(sdk: MysticLightSDK) -> MysticLightSchema {
let (query, mutation) = build_graphql_schema(sdk);

let schema = Schema::build(query, mutation, EmptySubscription)
.extension(Logger)
.extension(Tracing)
.finish();

if let Err(error) = fs::write("./schema.graphql", schema.sdl()) {
log::warn!("{}", error);
tracing::warn!("{}", error);
}

schema
Expand Down
3 changes: 1 addition & 2 deletions native-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn resolve_port() -> u16 {

#[cfg(not(debug_assertions))]
fn resolve_port() -> u16 {
println!("random");
portpicker::pick_unused_port().expect("Cannot resolve free port")
}

Expand All @@ -37,7 +36,7 @@ pub fn start_server() {

let local_address = server.local_addr();

log::info!("Server started at {}", local_address);
tracing::info!("Server started at {}", local_address);

let props = Props::new(local_address);

Expand Down
7 changes: 0 additions & 7 deletions native-app/src/main.rs

This file was deleted.

0 comments on commit e4f522f

Please sign in to comment.