mirror of
https://github.com/j178/prek.git
synced 2026-04-03 17:34:03 +02:00
Add a global --log-file flag to specify the log file path (#817)
This commit is contained in:
23
docs/cli.md
23
docs/cli.md
@@ -68,6 +68,7 @@ prek install [OPTIONS] [HOOK|PROJECT]...
|
||||
</ul></dd><dt id="prek-install--config"><a href="#prek-install--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-install--help"><a href="#prek-install--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-install--hook-type"><a href="#prek-install--hook-type"><code>--hook-type</code></a>, <code>-t</code> <i>hook-type</i></dt><dt id="prek-install--install-hooks"><a href="#prek-install--install-hooks"><code>--install-hooks</code></a></dt><dd><p>Create hook environments for all hooks used in the config file</p>
|
||||
</dd><dt id="prek-install--log-file"><a href="#prek-install--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-install--no-progress"><a href="#prek-install--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-install--overwrite"><a href="#prek-install--overwrite"><code>--overwrite</code></a>, <code>-f</code></dt><dd><p>Overwrite existing hooks</p>
|
||||
@@ -133,6 +134,7 @@ prek install-hooks [OPTIONS] [HOOK|PROJECT]...
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-install-hooks--config"><a href="#prek-install-hooks--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-install-hooks--help"><a href="#prek-install-hooks--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-install-hooks--log-file"><a href="#prek-install-hooks--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-install-hooks--no-progress"><a href="#prek-install-hooks--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-install-hooks--quiet"><a href="#prek-install-hooks--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -216,6 +218,7 @@ prek run [OPTIONS] [HOOK|PROJECT]...
|
||||
<li><code>pre-rebase</code></li>
|
||||
<li><code>prepare-commit-msg</code></li>
|
||||
</ul></dd><dt id="prek-run--last-commit"><a href="#prek-run--last-commit"><code>--last-commit</code></a></dt><dd><p>Run hooks against the last commit. Equivalent to <code>--from-ref HEAD~1 --to-ref HEAD</code></p>
|
||||
</dd><dt id="prek-run--log-file"><a href="#prek-run--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-run--no-progress"><a href="#prek-run--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-run--quiet"><a href="#prek-run--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -317,7 +320,8 @@ prek list [OPTIONS] [HOOK|PROJECT]...
|
||||
<li><code>pygrep</code></li>
|
||||
<li><code>script</code></li>
|
||||
<li><code>system</code></li>
|
||||
</ul></dd><dt id="prek-list--no-progress"><a href="#prek-list--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
</ul></dd><dt id="prek-list--log-file"><a href="#prek-list--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-list--no-progress"><a href="#prek-list--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-list--output-format"><a href="#prek-list--output-format"><code>--output-format</code></a> <i>output-format</i></dt><dd><p>The output format</p>
|
||||
<p>[default: text]</p><p>Possible values:</p>
|
||||
@@ -366,7 +370,8 @@ prek uninstall [OPTIONS]
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-uninstall--config"><a href="#prek-uninstall--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-uninstall--help"><a href="#prek-uninstall--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-uninstall--hook-type"><a href="#prek-uninstall--hook-type"><code>--hook-type</code></a>, <code>-t</code> <i>hook-type</i></dt><dt id="prek-uninstall--no-progress"><a href="#prek-uninstall--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
</dd><dt id="prek-uninstall--hook-type"><a href="#prek-uninstall--hook-type"><code>--hook-type</code></a>, <code>-t</code> <i>hook-type</i></dt><dt id="prek-uninstall--log-file"><a href="#prek-uninstall--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-uninstall--no-progress"><a href="#prek-uninstall--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-uninstall--quiet"><a href="#prek-uninstall--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
<p>Repeating this option, e.g., <code>-qq</code>, will enable a silent mode in which prek will write no output to stdout.</p>
|
||||
@@ -401,6 +406,7 @@ prek validate-config [OPTIONS] [CONFIG]...
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-validate-config--config"><a href="#prek-validate-config--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-validate-config--help"><a href="#prek-validate-config--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-validate-config--log-file"><a href="#prek-validate-config--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-validate-config--no-progress"><a href="#prek-validate-config--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-validate-config--quiet"><a href="#prek-validate-config--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -436,6 +442,7 @@ prek validate-manifest [OPTIONS] [MANIFEST]...
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-validate-manifest--config"><a href="#prek-validate-manifest--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-validate-manifest--help"><a href="#prek-validate-manifest--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-validate-manifest--log-file"><a href="#prek-validate-manifest--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-validate-manifest--no-progress"><a href="#prek-validate-manifest--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-validate-manifest--quiet"><a href="#prek-validate-manifest--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -467,6 +474,7 @@ prek sample-config [OPTIONS]
|
||||
</ul></dd><dt id="prek-sample-config--config"><a href="#prek-sample-config--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-sample-config--file"><a href="#prek-sample-config--file"><code>--file</code></a>, <code>-f</code> <i>file</i></dt><dd><p>Write the sample config to a file (<code>.pre-commit-config.yaml</code> by default)</p>
|
||||
</dd><dt id="prek-sample-config--help"><a href="#prek-sample-config--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-sample-config--log-file"><a href="#prek-sample-config--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-sample-config--no-progress"><a href="#prek-sample-config--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-sample-config--quiet"><a href="#prek-sample-config--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -501,7 +509,8 @@ prek auto-update [OPTIONS]
|
||||
</dd><dt id="prek-auto-update--freeze"><a href="#prek-auto-update--freeze"><code>--freeze</code></a></dt><dd><p>Store "frozen" hashes in <code>rev</code> instead of tag names</p>
|
||||
</dd><dt id="prek-auto-update--help"><a href="#prek-auto-update--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-auto-update--jobs"><a href="#prek-auto-update--jobs"><code>--jobs</code></a>, <code>-j</code> <i>jobs</i></dt><dd><p>Number of threads to use</p>
|
||||
<p>[default: 3]</p></dd><dt id="prek-auto-update--no-progress"><a href="#prek-auto-update--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>[default: 3]</p></dd><dt id="prek-auto-update--log-file"><a href="#prek-auto-update--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-auto-update--no-progress"><a href="#prek-auto-update--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-auto-update--quiet"><a href="#prek-auto-update--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
<p>Repeating this option, e.g., <code>-qq</code>, will enable a silent mode in which prek will write no output to stdout.</p>
|
||||
@@ -549,6 +558,7 @@ prek cache dir [OPTIONS]
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-cache-dir--config"><a href="#prek-cache-dir--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-cache-dir--help"><a href="#prek-cache-dir--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-cache-dir--log-file"><a href="#prek-cache-dir--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-cache-dir--no-progress"><a href="#prek-cache-dir--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-cache-dir--quiet"><a href="#prek-cache-dir--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -579,6 +589,7 @@ prek cache gc [OPTIONS]
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-cache-gc--config"><a href="#prek-cache-gc--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-cache-gc--help"><a href="#prek-cache-gc--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-cache-gc--log-file"><a href="#prek-cache-gc--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-cache-gc--no-progress"><a href="#prek-cache-gc--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-cache-gc--quiet"><a href="#prek-cache-gc--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -609,6 +620,7 @@ prek cache clean [OPTIONS]
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-cache-clean--config"><a href="#prek-cache-clean--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-cache-clean--help"><a href="#prek-cache-clean--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-cache-clean--log-file"><a href="#prek-cache-clean--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-cache-clean--no-progress"><a href="#prek-cache-clean--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-cache-clean--quiet"><a href="#prek-cache-clean--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -657,7 +669,8 @@ prek init-template-dir [OPTIONS] <DIRECTORY>
|
||||
<li><code>pre-push</code></li>
|
||||
<li><code>pre-rebase</code></li>
|
||||
<li><code>prepare-commit-msg</code></li>
|
||||
</ul></dd><dt id="prek-init-template-dir--no-allow-missing-config"><a href="#prek-init-template-dir--no-allow-missing-config"><code>--no-allow-missing-config</code></a></dt><dd><p>Assume cloned repos should have a <code>pre-commit</code> config</p>
|
||||
</ul></dd><dt id="prek-init-template-dir--log-file"><a href="#prek-init-template-dir--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-init-template-dir--no-allow-missing-config"><a href="#prek-init-template-dir--no-allow-missing-config"><code>--no-allow-missing-config</code></a></dt><dd><p>Assume cloned repos should have a <code>pre-commit</code> config</p>
|
||||
</dd><dt id="prek-init-template-dir--no-progress"><a href="#prek-init-template-dir--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-init-template-dir--quiet"><a href="#prek-init-template-dir--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -727,6 +740,7 @@ prek try-repo [OPTIONS] [HOOK|PROJECT]...
|
||||
<li><code>pre-rebase</code></li>
|
||||
<li><code>prepare-commit-msg</code></li>
|
||||
</ul></dd><dt id="prek-try-repo--last-commit"><a href="#prek-try-repo--last-commit"><code>--last-commit</code></a></dt><dd><p>Run hooks against the last commit. Equivalent to <code>--from-ref HEAD~1 --to-ref HEAD</code></p>
|
||||
</dd><dt id="prek-try-repo--log-file"><a href="#prek-try-repo--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-try-repo--no-progress"><a href="#prek-try-repo--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-try-repo--quiet"><a href="#prek-try-repo--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
@@ -793,6 +807,7 @@ prek self update [OPTIONS] [TARGET_VERSION]
|
||||
<li><code>never</code>: Disables colored output</li>
|
||||
</ul></dd><dt id="prek-self-update--config"><a href="#prek-self-update--config"><code>--config</code></a>, <code>-c</code> <i>config</i></dt><dd><p>Path to alternate config file</p>
|
||||
</dd><dt id="prek-self-update--help"><a href="#prek-self-update--help"><code>--help</code></a>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>
|
||||
</dd><dt id="prek-self-update--log-file"><a href="#prek-self-update--log-file"><code>--log-file</code></a> <i>log-file</i></dt><dd><p>Write trace logs to the specified file. If not specified, trace logs will be written to <code>$PREK_HOME/prek.log</code></p>
|
||||
</dd><dt id="prek-self-update--no-progress"><a href="#prek-self-update--no-progress"><code>--no-progress</code></a></dt><dd><p>Hide all progress outputs.</p>
|
||||
<p>For example, spinners or progress bars.</p>
|
||||
</dd><dt id="prek-self-update--quiet"><a href="#prek-self-update--quiet"><code>--quiet</code></a>, <code>-q</code></dt><dd><p>Use quiet output.</p>
|
||||
|
||||
@@ -174,6 +174,15 @@ pub(crate) struct GlobalArgs {
|
||||
#[arg(global = true, short, long, action = ArgAction::Count)]
|
||||
pub(crate) verbose: u8,
|
||||
|
||||
/// Write trace logs to the specified file.
|
||||
/// If not specified, trace logs will be written to `$PREK_HOME/prek.log`.
|
||||
#[arg(global = true, long, value_name = "LOG_FILE", value_hint = ValueHint::FilePath)]
|
||||
pub(crate) log_file: Option<PathBuf>,
|
||||
|
||||
/// Do not write trace logs to a log file.
|
||||
#[arg(global = true, long, overrides_with = "log_file", hide = true)]
|
||||
pub(crate) no_log_file: bool,
|
||||
|
||||
/// Display the prek version.
|
||||
#[arg(global = true, short = 'V', long, action = ArgAction::Version)]
|
||||
version: (),
|
||||
|
||||
90
src/main.rs
90
src/main.rs
@@ -1,4 +1,5 @@
|
||||
use std::fmt::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::ExitCode;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Mutex;
|
||||
@@ -59,7 +60,29 @@ pub(crate) enum Level {
|
||||
TraceAll,
|
||||
}
|
||||
|
||||
fn setup_logging(level: Level) -> Result<()> {
|
||||
enum LogFile {
|
||||
Default,
|
||||
Path(PathBuf),
|
||||
Disabled,
|
||||
}
|
||||
|
||||
impl LogFile {
|
||||
fn from_args(log_file: Option<PathBuf>, no_log_file: bool) -> Self {
|
||||
if no_log_file {
|
||||
Self::Disabled
|
||||
} else if let Some(path) = log_file {
|
||||
Self::Path(path)
|
||||
} else {
|
||||
Self::Default
|
||||
}
|
||||
}
|
||||
|
||||
fn is_disabled(&self) -> bool {
|
||||
matches!(self, Self::Disabled)
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_logging(level: Level, log_file: LogFile) -> Result<()> {
|
||||
let directive = match level {
|
||||
Level::Default | Level::Verbose => LevelFilter::OFF.into(),
|
||||
Level::Debug => Directive::from_str("prek=debug")?,
|
||||
@@ -81,28 +104,35 @@ fn setup_logging(level: Level) -> Result<()> {
|
||||
.with_writer(anstream::stderr)
|
||||
.with_filter(stderr_filter);
|
||||
|
||||
let log_file_path = STORE.as_ref()?.log_file();
|
||||
let log_file = fs_err::OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(log_file_path)
|
||||
.context("Failed to open log file")?;
|
||||
let log_file = Mutex::new(StripStream::new(log_file.into_file()));
|
||||
let registry = tracing_subscriber::registry().with(stderr_layer);
|
||||
|
||||
let file_format = tracing_subscriber::fmt::format()
|
||||
.with_target(false)
|
||||
.with_ansi(false);
|
||||
let file_layer = tracing_subscriber::fmt::layer()
|
||||
.with_span_events(FmtSpan::CLOSE)
|
||||
.event_format(file_format)
|
||||
.with_writer(log_file)
|
||||
.with_filter(EnvFilter::new("prek=trace"));
|
||||
if log_file.is_disabled() {
|
||||
registry.init();
|
||||
} else {
|
||||
let log_file_path = match log_file {
|
||||
LogFile::Default => STORE.as_ref()?.log_file(),
|
||||
LogFile::Path(path) => path,
|
||||
LogFile::Disabled => unreachable!(),
|
||||
};
|
||||
let log_file = fs_err::OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(log_file_path)
|
||||
.context("Failed to open log file")?;
|
||||
let log_file = Mutex::new(StripStream::new(log_file.into_file()));
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(stderr_layer)
|
||||
.with(file_layer)
|
||||
.init();
|
||||
let file_format = tracing_subscriber::fmt::format()
|
||||
.with_target(false)
|
||||
.with_ansi(false);
|
||||
let file_layer = tracing_subscriber::fmt::layer()
|
||||
.with_span_events(FmtSpan::CLOSE)
|
||||
.event_format(file_format)
|
||||
.with_writer(log_file)
|
||||
.with_filter(EnvFilter::new("prek=trace"));
|
||||
|
||||
registry.with(file_layer).init();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -110,13 +140,17 @@ fn setup_logging(level: Level) -> Result<()> {
|
||||
async fn run(mut cli: Cli) -> Result<ExitStatus> {
|
||||
ColorChoice::write_global(cli.globals.color.into());
|
||||
|
||||
setup_logging(match cli.globals.verbose {
|
||||
0 => Level::Default,
|
||||
1 => Level::Verbose,
|
||||
2 => Level::Debug,
|
||||
3 => Level::Trace,
|
||||
_ => Level::TraceAll,
|
||||
})?;
|
||||
let log_file = LogFile::from_args(cli.globals.log_file.clone(), cli.globals.no_log_file);
|
||||
setup_logging(
|
||||
match cli.globals.verbose {
|
||||
0 => Level::Default,
|
||||
1 => Level::Verbose,
|
||||
2 => Level::Debug,
|
||||
3 => Level::Trace,
|
||||
_ => Level::TraceAll,
|
||||
},
|
||||
log_file,
|
||||
)?;
|
||||
|
||||
let printer = if cli.globals.quiet == 1 {
|
||||
Printer::Quiet
|
||||
|
||||
57
tests/run.rs
57
tests/run.rs
@@ -8,6 +8,7 @@ use assert_fs::prelude::*;
|
||||
use constants::env_vars::EnvVars;
|
||||
use constants::{ALT_CONFIG_FILE, CONFIG_FILE};
|
||||
use insta::assert_snapshot;
|
||||
use predicates::prelude::predicate;
|
||||
|
||||
mod common;
|
||||
|
||||
@@ -1736,6 +1737,7 @@ fn selectors_completion() -> Result<()> {
|
||||
--no-progress Hide all progress outputs
|
||||
--quiet Use quiet output
|
||||
--verbose Use verbose output
|
||||
--log-file Write trace logs to the specified file. If not specified, trace logs will be written to `$PREK_HOME/prek.log`
|
||||
--version Display the prek version
|
||||
|
||||
----- stderr -----
|
||||
@@ -2154,3 +2156,58 @@ fn run_quiet() {
|
||||
----- stderr -----
|
||||
");
|
||||
}
|
||||
|
||||
/// Test `prek run --log-file <file>` flag.
|
||||
#[test]
|
||||
fn run_log_file() {
|
||||
let context = TestContext::new();
|
||||
context.init_project();
|
||||
context.write_pre_commit_config(indoc::indoc! {r"
|
||||
repos:
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: fail
|
||||
name: fail
|
||||
entry: fail
|
||||
language: fail
|
||||
"});
|
||||
context.git_add(".");
|
||||
|
||||
// Run with `--no-log-file`, no `prek.log` is created.
|
||||
cmd_snapshot!(context.filters(), context.run().arg("--no-log-file"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
fail.....................................................................Failed
|
||||
- hook id: fail
|
||||
- exit code: 1
|
||||
fail
|
||||
|
||||
.pre-commit-config.yaml
|
||||
|
||||
----- stderr -----
|
||||
");
|
||||
context
|
||||
.home_dir()
|
||||
.child("prek.log")
|
||||
.assert(predicate::path::missing());
|
||||
|
||||
// Write log to `log`.
|
||||
cmd_snapshot!(context.filters(), context.run().arg("--log-file").arg("log"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
fail.....................................................................Failed
|
||||
- hook id: fail
|
||||
- exit code: 1
|
||||
fail
|
||||
|
||||
.pre-commit-config.yaml
|
||||
|
||||
----- stderr -----
|
||||
");
|
||||
context
|
||||
.work_dir()
|
||||
.child("log")
|
||||
.assert(predicate::path::exists());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user