Skip to content

Commit 76612b9

Browse files
committed
Add logging
1 parent 4ffd7ac commit 76612b9

File tree

10 files changed

+99
-4
lines changed

10 files changed

+99
-4
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ tokio = { version = "1.44.1", features = ["full"] }
88
axum = { version = "0.8.1", features = ["macros"] }
99
tower-http = { version = "0.6.2", features = ["full"] }
1010
tracing = "0.1.41"
11-
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
11+
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "json"] }
1212
serde = { version = "1.0.219", features = ["derive"] }
1313
serde_json = "1.0.140"
14-
sqlx = { version = "0.8.3", features = ["postgres", "sqlite", "runtime-tokio"]}
14+
sqlx = { version = "0.8.3", features = ["postgres", "sqlite", "runtime-tokio"]}
15+
log = "0.4.26"

config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2+
"logging": {
3+
"level": "debug",
4+
"json": false
5+
},
26
"kvstorage_type": "sqlite",
37
"sqlite": {
4-
"path": "sqlite.db",
8+
"path": "kv.db",
59
"pool_size": 10
610
},
711
"locks_type": "memory"

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ use crate::kvstorage::postgres::PostgresConfig;
33
use crate::kvstorage::sqlite::SQLiteConfig;
44
use crate::locks::LocksType;
55
use std::error::Error;
6+
use crate::logging::LoggingConfig;
67

78
#[derive(Debug, serde::Deserialize, Clone)]
89
pub struct Config {
10+
pub logging: LoggingConfig,
11+
912
pub kvstorage_type: KVStorageType,
1013

1114
#[serde(default)]

src/kvstorage/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::config::Config;
22
use serde::Deserialize;
33
use std::error::Error;
4+
use tracing::{debug, info};
45

56
mod pooled;
67
pub mod postgres;
@@ -74,10 +75,12 @@ impl KVStorage {
7475
pub async fn new(config: &Config) -> Result<Box<Self>, Box<dyn Error>> {
7576
match config.kvstorage_type {
7677
KVStorageType::Postgres => {
78+
info!("Using Postgres as KV storage");
7779
let storage = postgres::Postgres::new(config).await?;
7880
Ok(Box::new(KVStorage::Postgres(*storage)))
7981
}
8082
KVStorageType::SQLite => {
83+
info!("Using SQLite as KV storage");
8184
let storage = sqlite::SQLite::new(config).await?;
8285
Ok(Box::new(KVStorage::SQLite(*storage)))
8386
}
@@ -92,6 +95,7 @@ impl KVStorage {
9295
}
9396

9497
pub async fn get_ref_count(&mut self, bucket: &str, hash: &str) -> Result<i32, Box<dyn Error>> {
98+
debug!("Getting ref count for bucket: {}, hash: {}", bucket, hash);
9599
match self {
96100
KVStorage::Postgres(storage) => storage.get_ref_count(bucket, hash).await,
97101
KVStorage::SQLite(storage) => storage.get_ref_count(bucket, hash).await,
@@ -104,6 +108,10 @@ impl KVStorage {
104108
hash: &str,
105109
ref_cnt: i32,
106110
) -> Result<(), Box<dyn Error>> {
111+
debug!(
112+
"Setting ref count for bucket: {}, hash: {} to {}",
113+
bucket, hash, ref_cnt
114+
);
107115
match self {
108116
KVStorage::Postgres(storage) => storage.set_ref_count(bucket, hash, ref_cnt).await,
109117
KVStorage::SQLite(storage) => storage.set_ref_count(bucket, hash, ref_cnt).await,
@@ -115,6 +123,7 @@ impl KVStorage {
115123
bucket: &str,
116124
hash: &str,
117125
) -> Result<(), Box<dyn Error>> {
126+
debug!("Incrementing ref count for bucket: {}, hash: {}", bucket, hash);
118127
match self {
119128
KVStorage::Postgres(storage) => storage.increment_ref_count(bucket, hash).await,
120129
KVStorage::SQLite(storage) => storage.increment_ref_count(bucket, hash).await,
@@ -126,13 +135,15 @@ impl KVStorage {
126135
bucket: &str,
127136
hash: &str,
128137
) -> Result<(), Box<dyn Error>> {
138+
debug!("Decrementing ref count for bucket: {}, hash: {}", bucket, hash);
129139
match self {
130140
KVStorage::Postgres(storage) => storage.decrement_ref_count(bucket, hash).await,
131141
KVStorage::SQLite(storage) => storage.decrement_ref_count(bucket, hash).await,
132142
}
133143
}
134144

135145
pub async fn get_modified(&mut self, bucket: &str, path: &str) -> Result<i64, Box<dyn Error>> {
146+
debug!("Getting modified time for bucket: {}, path: {}", bucket, path);
136147
match self {
137148
KVStorage::Postgres(storage) => storage.get_modified(bucket, path).await,
138149
KVStorage::SQLite(storage) => storage.get_modified(bucket, path).await,
@@ -145,6 +156,10 @@ impl KVStorage {
145156
path: &str,
146157
modified: i64,
147158
) -> Result<(), Box<dyn Error>> {
159+
debug!(
160+
"Setting modified time for bucket: {}, path: {} to {}",
161+
bucket, path, modified
162+
);
148163
match self {
149164
KVStorage::Postgres(storage) => storage.set_modified(bucket, path, modified).await,
150165
KVStorage::SQLite(storage) => storage.set_modified(bucket, path, modified).await,
@@ -156,6 +171,7 @@ impl KVStorage {
156171
bucket: &str,
157172
path: &str,
158173
) -> Result<(), Box<dyn Error>> {
174+
debug!("Deleting modified time for bucket: {}, path: {}", bucket, path);
159175
match self {
160176
KVStorage::Postgres(storage) => storage.delete_modified(bucket, path).await,
161177
KVStorage::SQLite(storage) => storage.delete_modified(bucket, path).await,
@@ -167,6 +183,7 @@ impl KVStorage {
167183
bucket: &str,
168184
path: &str,
169185
) -> Result<String, Box<dyn Error>> {
186+
debug!("Getting ref file for bucket: {}, path: {}", bucket, path);
170187
match self {
171188
KVStorage::Postgres(storage) => storage.get_ref_file(bucket, path).await,
172189
KVStorage::SQLite(storage) => storage.get_ref_file(bucket, path).await,
@@ -179,6 +196,10 @@ impl KVStorage {
179196
path: &str,
180197
hash: &str,
181198
) -> Result<(), Box<dyn Error>> {
199+
debug!(
200+
"Setting ref file for bucket: {}, path: {} to {}",
201+
bucket, path, hash
202+
);
182203
match self {
183204
KVStorage::Postgres(storage) => storage.set_ref_file(bucket, path, hash).await,
184205
KVStorage::SQLite(storage) => storage.set_ref_file(bucket, path, hash).await,
@@ -190,6 +211,7 @@ impl KVStorage {
190211
bucket: &str,
191212
path: &str,
192213
) -> Result<(), Box<dyn Error>> {
214+
debug!("Deleting ref file for bucket: {}, path: {}", bucket, path);
193215
match self {
194216
KVStorage::Postgres(storage) => storage.delete_ref_file(bucket, path).await,
195217
KVStorage::SQLite(storage) => storage.delete_ref_file(bucket, path).await,

src/kvstorage/postgres.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use serde::Deserialize;
55
use sqlx::PgPool;
66
use sqlx::postgres::PgPoolOptions;
77
use std::error::Error;
8+
use tracing::debug;
89

910
#[derive(Debug, Clone, Deserialize)]
1011
pub struct PostgresConfig {
@@ -28,6 +29,7 @@ impl KVStorageTrait for Postgres {
2829
"postgres://{}:{}@{}:{}/{}",
2930
pg_config.user, pg_config.password, pg_config.host, pg_config.port, pg_config.dbname
3031
);
32+
debug!("Connecting to Postgres database: {}", db_url);
3133
let pool = PgPoolOptions::new()
3234
.max_connections(pg_config.pool_size)
3335
.connect(&db_url)

src/kvstorage/sqlite.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::path::Path;
12
use crate::config::Config;
23
use crate::kvstorage::KVStorageTrait;
34
use crate::kvstorage::pooled::{RowModified, RowRefFile, RowRefcount};
45
use serde::Deserialize;
56
use sqlx::SqlitePool;
7+
use tracing::debug;
8+
use tracing::field::debug;
69

710
#[derive(Debug, Clone, Deserialize)]
811
pub struct SQLiteConfig {
@@ -18,7 +21,13 @@ pub struct SQLite {
1821
impl KVStorageTrait for SQLite {
1922
async fn new(config: &Config) -> Result<Box<Self>, Box<dyn std::error::Error>> {
2023
let sqlite_config = config.sqlite.as_ref().unwrap();
24+
25+
if !Path::new(&sqlite_config.path).exists() {
26+
std::fs::File::create(&sqlite_config.path)?;
27+
}
28+
2129
let db_url = format!("sqlite://{}", sqlite_config.path);
30+
debug!("Connecting to SQLite database: {}", db_url);
2231
let pool = SqlitePool::connect(&db_url).await?;
2332
Ok(Box::new(SQLite { pool }))
2433
}

src/locks/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::Deserialize;
2+
use tracing::{debug, info};
23

34
pub mod memory;
45

@@ -26,11 +27,15 @@ pub enum LocksStorage {
2627
impl LocksStorage {
2728
pub fn new(lock_type: &LocksType) -> Box<Self> {
2829
match lock_type {
29-
LocksType::Memory => Box::new(LocksStorage::Memory(*memory::MemoryLocks::new())),
30+
LocksType::Memory => {
31+
info!("Using memory as locks storage");
32+
Box::new(LocksStorage::Memory(*memory::MemoryLocks::new()))
33+
},
3034
}
3135
}
3236

3337
pub fn acquire_shared(&mut self, key: &str) {
38+
debug!("Acquiring shared lock for key: {}", key);
3439
match self {
3540
LocksStorage::Memory(lock) => {
3641
lock.acquire_shared(key);
@@ -39,6 +44,7 @@ impl LocksStorage {
3944
}
4045

4146
pub fn acquire_exclusive(&mut self, key: &str) {
47+
debug!("Acquiring exclusive lock for key: {}", key);
4248
match self {
4349
LocksStorage::Memory(lock) => {
4450
lock.acquire_exclusive(key);
@@ -47,6 +53,7 @@ impl LocksStorage {
4753
}
4854

4955
pub fn release(&mut self, key: &str) -> bool {
56+
debug!("Releasing lock for key: {}", key);
5057
match self {
5158
LocksStorage::Memory(lock) => lock.release(key),
5259
}

src/logging.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use tracing::dispatcher::SetGlobalDefaultError;
2+
use tracing_subscriber::{fmt, EnvFilter};
3+
4+
#[derive(Clone, Debug, serde::Deserialize)]
5+
pub struct LoggingConfig {
6+
level: String,
7+
json: bool,
8+
}
9+
10+
pub fn setup(logging_config: &LoggingConfig) -> Result<(), SetGlobalDefaultError> {
11+
let filter = EnvFilter::new(&logging_config.level);
12+
if logging_config.json {
13+
let subscriber = fmt::Subscriber::builder()
14+
.with_env_filter(filter)
15+
.json()
16+
.finish();
17+
tracing::subscriber::set_global_default(subscriber)
18+
} else {
19+
let subscriber = fmt::Subscriber::builder()
20+
.with_env_filter(filter)
21+
.finish();
22+
tracing::subscriber::set_global_default(subscriber)
23+
}
24+
}

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ use axum::routing::put;
55
use routes::ft::put_file::ft_put_file;
66
use std::error::Error;
77
use std::sync::Arc;
8+
use tower_http::trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer};
9+
use tracing::Level;
810

911
mod config;
1012
mod kvstorage;
1113
mod locks;
1214
mod routes;
15+
mod logging;
1316

1417
#[derive(Clone)]
1518
struct AppState {
@@ -33,11 +36,17 @@ impl AppState {
3336
#[tokio::main]
3437
async fn main() {
3538
let config = config::Config::new("config.json").unwrap();
39+
logging::setup(&config.logging).unwrap();
3640
let mut app_state = AppState::new(config).await.unwrap();
3741
app_state.kvstorage.setup().await.unwrap();
3842

3943
let app = Router::new()
4044
.route("/ft/files/{path}", put(ft_put_file))
45+
.layer(
46+
TraceLayer::new_for_http()
47+
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
48+
.on_response(DefaultOnResponse::new().level(Level::INFO)),
49+
)
4150
.with_state(Arc::new(app_state));
4251
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
4352
axum::serve(listener, app).await.unwrap();

0 commit comments

Comments
 (0)