Skip to content

Commit 74891a9

Browse files
add support for multiple log format
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 74891a9

File tree

6 files changed

+114
-92
lines changed

6 files changed

+114
-92
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
@@ -7,7 +7,6 @@ pub mod dns_cache;
77
pub mod errors;
88
pub mod messages;
99
pub mod mirrors;
10-
pub mod multi_logger;
1110
pub mod plugins;
1211
pub mod pool;
1312
pub mod prometheus;
@@ -17,6 +16,7 @@ pub mod server;
1716
pub mod sharding;
1817
pub mod stats;
1918
pub mod tls;
19+
pub mod cmd_args;
2020

2121
/// Format chrono::Duration to be more human-friendly.
2222
///

src/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,23 @@ use pgcat::messages::configure_socket;
6767
use pgcat::pool::{ClientServerMap, ConnectionPool};
6868
use pgcat::prometheus::start_metric_server;
6969
use pgcat::stats::{Collector, Reporter, REPORTER};
70+
use pgcat::cmd_args;
7071

71-
mod cmd_args;
72+
use tracing_subscriber;
73+
use pgcat::cmd_args::LogFormat;
7274

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

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

src/multi_logger.rs

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

0 commit comments

Comments
 (0)