Skip to content

Commit

Permalink
add support for multiple log format
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sebastianwebber committed Jul 19, 2023
1 parent 7bdb4e5 commit 899dcc7
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 92 deletions.
82 changes: 82 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ tokio-test = "0.4.2"
serde_json = "1"
itertools = "0.10"
clap = { version = "4.3.1", features = ["derive", "env"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["json"]}

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.5.0"
Expand Down
22 changes: 16 additions & 6 deletions src/cmd_args.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use clap::Parser;
use log::LevelFilter;
use clap::{Parser, ValueEnum};
use tracing::Level;

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

#[arg(short, long, default_value_t = LevelFilter::Info, env)]
pub log_level: log::LevelFilter,
#[arg(short, long, default_value_t = tracing::Level::INFO, env)]
pub log_level: Level,

#[clap(short='F', long, value_enum, default_value_t=LogFormat::Text, env)]
pub log_format: LogFormat,
}

pub(crate) fn parse() -> Args {
pub fn parse() -> Args {
return Args::parse();
}

#[derive(ValueEnum, Clone, Debug)]
pub enum LogFormat {
Text,
Structured,
Debug
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod dns_cache;
pub mod errors;
pub mod messages;
pub mod mirrors;
pub mod multi_logger;
pub mod plugins;
pub mod pool;
pub mod prometheus;
Expand All @@ -17,6 +16,7 @@ pub mod server;
pub mod sharding;
pub mod stats;
pub mod tls;
pub mod cmd_args;

/// Format chrono::Duration to be more human-friendly.
///
Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ use pgcat::messages::configure_socket;
use pgcat::pool::{ClientServerMap, ConnectionPool};
use pgcat::prometheus::start_metric_server;
use pgcat::stats::{Collector, Reporter, REPORTER};
use pgcat::cmd_args;

mod cmd_args;
use tracing_subscriber;
use pgcat::cmd_args::LogFormat;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = cmd_args::parse();
pgcat::multi_logger::MultiLogger::init(args.log_level).unwrap();
match args.log_format {
LogFormat::Structured => tracing_subscriber::fmt().json().with_max_level(args.log_level).init(),
LogFormat::Debug => tracing_subscriber::fmt().pretty().with_max_level(args.log_level).init(),
_ => tracing_subscriber::fmt().with_max_level(args.log_level).init()
};

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

Expand Down
83 changes: 0 additions & 83 deletions src/multi_logger.rs

This file was deleted.

0 comments on commit 899dcc7

Please sign in to comment.