Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --logs flag to local command #2187

Merged
merged 9 commits into from
Dec 8, 2023
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ node_modules

# Benchmark data
bench_data/

# Logs
*.log
4 changes: 4 additions & 0 deletions crates/glaredb/src/args/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct LocalArgs {

/// Execute an SQL file.
pub file: Option<String>,

/// File for logs to be written to
#[clap(long, value_parser)]
pub log_file: Option<PathBuf>,
}

#[derive(Debug, Clone, Parser)]
Expand Down
8 changes: 6 additions & 2 deletions crates/glaredb/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ fn main() -> Result<()> {
// Disable logging when running locally since it'll clobber the repl
// _unless_ the user specified a logging related option.
match (&command, cli.log_mode, cli.verbose) {
(Commands::Local { .. }, None, 0) => (),
_ => logutil::init(cli.verbose, cli.log_mode.unwrap_or_default().into()),
(Commands::Local(args), None, 0) => {
if let Some(file) = &args.log_file {
logutil::init(1, LoggingMode::Full.into(), Some(file))
}
}
_ => logutil::init(cli.verbose, cli.log_mode.unwrap_or_default().into(), None),
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
}

command.run()
Expand Down
20 changes: 20 additions & 0 deletions crates/glaredb/tests/log_file_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod setup;

use crate::setup::{make_cli, DEFAULT_TIMEOUT};

#[test]
fn test_named_log_file() {
let mut cmd = make_cli();
let log_file_name = "test.log";
cmd.timeout(DEFAULT_TIMEOUT)
.arg("-q")
.arg("select 1;")
.arg("--log-file")
.arg(log_file_name)
.assert()
.success();

let mut log_file = std::env::current_dir().expect("failed to retrieve current dir");
log_file.push(log_file_name);
assert!(log_file.exists());
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 14 additions & 3 deletions crates/logutil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Utilities for logging and tracing.
use std::{fs::File, path::PathBuf, sync::Arc};

use tracing::{subscriber, trace, Level};
use tracing_subscriber::{
filter::EnvFilter,
Expand Down Expand Up @@ -71,7 +73,7 @@ pub fn init_test() {

/// Initialize a trace subsriber printing to the console using the given
/// verbosity count.
pub fn init(verbosity: impl Into<Verbosity>, mode: LoggingMode) {
pub fn init(verbosity: impl Into<Verbosity>, mode: LoggingMode, log_file: Option<&PathBuf>) {
let verbosity: Verbosity = verbosity.into();
let level: Level = verbosity.into();

Expand All @@ -84,8 +86,17 @@ pub fn init(verbosity: impl Into<Verbosity>, mode: LoggingMode) {
subscriber::set_global_default(subscriber)
}
LoggingMode::Full => {
let subscriber = full_fmt(level).with_env_filter(env_filter).finish();
subscriber::set_global_default(subscriber)
let subscriber = full_fmt(level).with_env_filter(env_filter);

if let Some(file) = log_file {
let debug_log = {
let file = File::create(file).expect("Failed to create log file");
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
Arc::new(file)
};
subscriber::set_global_default(subscriber.with_writer(debug_log).finish())
} else {
subscriber::set_global_default(subscriber.finish())
}
}
LoggingMode::Compact => {
let subscriber = compact_fmt(level).with_env_filter(env_filter).finish();
Expand Down
2 changes: 1 addition & 1 deletion crates/testing/src/slt/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Cli {
Verbosity::Debug => LoggingMode::Full,
Verbosity::Trace => LoggingMode::Full,
};
logutil::init(cli.verbose, log_mode);
logutil::init(cli.verbose, log_mode, None);

// Abort the program on panic. This will ensure that slt tests will
// never pass if there's a panic somewhere.
Expand Down
Loading