Skip to content

Commit 19cb8a3

Browse files
add --no-color option to disable colors in the terminal (#518)
add --no-color option to disable colors this commit adds a new option to disable colors in the terminal and also moves the logger configuration to a different crate. Signed-off-by: Sebastian Webber <sebastian@swebber.me>
1 parent f85e5bd commit 19cb8a3

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

src/cmd_args.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ pub struct Args {
1313

1414
#[clap(short='F', long, value_enum, default_value_t=LogFormat::Text, env)]
1515
pub log_format: LogFormat,
16+
17+
#[arg(
18+
short,
19+
long,
20+
default_value_t = false,
21+
env,
22+
help = "disable colors in the log output"
23+
)]
24+
pub no_color: bool,
1625
}
1726

1827
pub fn parse() -> Args {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod config;
66
pub mod constants;
77
pub mod dns_cache;
88
pub mod errors;
9+
pub mod logger;
910
pub mod messages;
1011
pub mod mirrors;
1112
pub mod plugins;

src/logger.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::cmd_args::{Args, LogFormat};
2+
use tracing_subscriber;
3+
4+
pub fn init(args: &Args) {
5+
let trace_sub = tracing_subscriber::fmt()
6+
.with_max_level(args.log_level)
7+
.with_ansi(!args.no_color);
8+
9+
match args.log_format {
10+
LogFormat::Structured => trace_sub.json().init(),
11+
LogFormat::Debug => trace_sub.pretty().init(),
12+
_ => trace_sub.init(),
13+
};
14+
}

src/main.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,17 @@ use std::sync::Arc;
6262
use tokio::sync::broadcast;
6363

6464
use pgcat::cmd_args;
65-
use pgcat::cmd_args::LogFormat;
6665
use pgcat::config::{get_config, reload_config, VERSION};
6766
use pgcat::dns_cache;
67+
use pgcat::logger;
6868
use pgcat::messages::configure_socket;
6969
use pgcat::pool::{ClientServerMap, ConnectionPool};
7070
use pgcat::prometheus::start_metric_server;
7171
use pgcat::stats::{Collector, Reporter, REPORTER};
72-
use tracing_subscriber;
7372

7473
fn main() -> Result<(), Box<dyn std::error::Error>> {
7574
let args = cmd_args::parse();
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-
};
75+
logger::init(&args);
8976

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

0 commit comments

Comments
 (0)