Skip to content

Commit 1a937e1

Browse files
add support for multiple log formats
this commit adds the tracing-subscriber crate and use its formatters to support multiple log formats. More details in #464 (comment) Signed-off-by: Sebastian Webber <sebastian@swebber.me>
1 parent 7bdb4e5 commit 1a937e1

File tree

6 files changed

+117
-93
lines changed

6 files changed

+117
-93
lines changed

Cargo.lock

Lines changed: 82 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ tokio-test = "0.4.2"
4747
serde_json = "1"
4848
itertools = "0.10"
4949
clap = { version = "4.3.1", features = ["derive", "env"] }
50+
tracing = "0.1.37"
51+
tracing-subscriber = { version = "0.3.17", features = ["json"]}
5052

5153
[target.'cfg(not(target_env = "msvc"))'.dependencies]
5254
jemallocator = "0.5.0"

src/cmd_args.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
use clap::Parser;
2-
use log::LevelFilter;
1+
use clap::{Parser, ValueEnum};
2+
use tracing::Level;
33

44
/// PgCat: Nextgen PostgreSQL Pooler
55
#[derive(Parser, Debug)]
66
#[command(author, version, about, long_about = None)]
7-
pub(crate) struct Args {
7+
pub struct Args {
88
#[arg(default_value_t = String::from("pgcat.toml"), env)]
99
pub config_file: String,
1010

11-
#[arg(short, long, default_value_t = LevelFilter::Info, env)]
12-
pub log_level: log::LevelFilter,
11+
#[arg(short, long, default_value_t = tracing::Level::INFO, env)]
12+
pub log_level: Level,
13+
14+
#[clap(short='F', long, value_enum, default_value_t=LogFormat::Text, env)]
15+
pub log_format: LogFormat,
1316
}
1417

15-
pub(crate) fn parse() -> Args {
18+
pub fn parse() -> Args {
1619
return Args::parse();
1720
}
21+
22+
#[derive(ValueEnum, Clone, Debug)]
23+
pub enum LogFormat {
24+
Text,
25+
Structured,
26+
Debug,
27+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pub mod admin;
22
pub mod auth_passthrough;
33
pub mod client;
4+
pub mod cmd_args;
45
pub mod config;
56
pub mod constants;
67
pub mod dns_cache;
78
pub mod errors;
89
pub mod messages;
910
pub mod mirrors;
10-
pub mod multi_logger;
1111
pub mod plugins;
1212
pub mod pool;
1313
pub mod prometheus;

src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,31 @@ use std::str::FromStr;
6161
use std::sync::Arc;
6262
use tokio::sync::broadcast;
6363

64+
use pgcat::cmd_args;
65+
use pgcat::cmd_args::LogFormat;
6466
use pgcat::config::{get_config, reload_config, VERSION};
6567
use pgcat::dns_cache;
6668
use pgcat::messages::configure_socket;
6769
use pgcat::pool::{ClientServerMap, ConnectionPool};
6870
use pgcat::prometheus::start_metric_server;
6971
use pgcat::stats::{Collector, Reporter, REPORTER};
70-
71-
mod cmd_args;
72+
use tracing_subscriber;
7273

7374
fn main() -> Result<(), Box<dyn std::error::Error>> {
7475
let args = cmd_args::parse();
75-
pgcat::multi_logger::MultiLogger::init(args.log_level).unwrap();
76+
match args.log_format {
77+
LogFormat::Structured => tracing_subscriber::fmt()
78+
.json()
79+
.with_max_level(args.log_level)
80+
.init(),
81+
LogFormat::Debug => tracing_subscriber::fmt()
82+
.pretty()
83+
.with_max_level(args.log_level)
84+
.init(),
85+
_ => tracing_subscriber::fmt()
86+
.with_max_level(args.log_level)
87+
.init(),
88+
};
7689

7790
info!("Welcome to PgCat! Meow. (Version {})", VERSION);
7891

src/multi_logger.rs

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)