feat: Improve logging

This commit is contained in:
Nathan McCarty 2023-03-17 19:18:23 -04:00
parent ca9d921327
commit 534c04bd11
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
6 changed files with 85 additions and 11 deletions

37
Cargo.lock generated
View File

@ -594,6 +594,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata",
]
[[package]]
name = "memchr"
version = "2.5.0"
@ -840,6 +849,30 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "rustc-demangle"
version = "0.1.21"
@ -1110,10 +1143,14 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]

View File

@ -16,4 +16,4 @@ serde = { version = "1.0.156", features = ["derive"] }
serde_dhall = { version = "0.12.1", default-features = false }
snafu = "0.7.4"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }

View File

@ -1,6 +1,8 @@
mod cli;
use cli::Cli;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
use clap::Parser;
use color_eyre::eyre::Result;
@ -8,7 +10,14 @@ use color_eyre::eyre::Result;
fn main() -> Result<()> {
// Setup color eyre and logging
color_eyre::install()?;
tracing_subscriber::fmt::init();
let fmt_layer = fmt::layer().pretty();
let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();
let args = Cli::parse();
args.command.run(args.global_args)

View File

@ -8,7 +8,7 @@ use std::{
};
use snafu::{ensure, ResultExt, Snafu};
use tracing::{debug, instrument};
use tracing::{debug, info, info_span};
use self::{config::Config, page::Page, post::Post};
@ -126,20 +126,25 @@ impl Site {
///
/// Will return an error if serialization fails, the user does not have write permissions for
/// the directory, or any other IO errors occur while writing out the config.
#[instrument(skip(site_dir))]
pub fn write(&self, site_dir: impl AsRef<Path>) -> Result<(), SiteError> {
let site_dir = site_dir.as_ref();
debug!(?site_dir);
// Setup logging
let span = info_span!("Site::write", ?site_dir);
let _enter = span.enter();
info!("Initializing/Updating site repository");
// Make sure the site_dir is a directory, if it exists, otherwise create it
if site_dir
.try_exists()
.context(ExistanceCheckSnafu { path: site_dir })?
{
debug!("Validating site directory");
ensure!(site_dir.is_dir(), NotADirectorySnafu { path: site_dir });
} else {
debug!("Creating site directory");
create_dir_all(site_dir).context(CreateDirectorySnafu { path: site_dir })?;
}
// Serialize the config
debug!(?self.config, "Writing out the config");
let config_path = site_dir.join("config.dhall");
let serialized_config = serde_dhall::serialize(&self.config)
.to_string()
@ -165,11 +170,13 @@ impl Site {
.try_exists()
.context(ExistanceCheckSnafu { path: &statics_dir })?
{
debug!(?statics_dir, "Validating statics directory");
ensure!(
statics_dir.is_dir(),
NotADirectorySnafu { path: &statics_dir }
);
} else {
debug!(?statics_dir, "Creating statics directory");
// Create the directory
create_dir_all(&statics_dir).context(CreateDirectorySnafu { path: &statics_dir })?;
// Touch a .gitkeep
@ -178,6 +185,7 @@ impl Site {
}
// Create the gitignore
let git_ignore_path = site_dir.join(".gitignore");
debug!(?git_ignore_path, "Creating gitignore");
let mut git_ignore = File::create(&git_ignore_path).context(WriteConfigSnafu {
path: &git_ignore_path,
})?;

View File

@ -8,7 +8,7 @@ use std::{
use serde::{Deserialize, Serialize};
use snafu::{ensure, ResultExt};
use tracing::{debug, instrument};
use tracing::{debug, info, info_span};
use super::{
ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError,
@ -66,7 +66,6 @@ impl Page {
///
/// Will return an error if serialization fails, the user does not have write permissions for
/// the directory, or any other IO errors occur while writing out the config.
#[instrument(skip(site_path, page_dir))]
pub fn write(
&self,
site_path: impl AsRef<Path>,
@ -74,18 +73,24 @@ impl Page {
) -> Result<(), SiteError> {
// build the page_dir
let page_dir = site_path.as_ref().join("pages").join(page_dir.as_ref());
debug!(?page_dir);
// Setup logging
let span = info_span!("Page::write", ?page_dir);
let _enter = span.enter();
info!("Writing out page");
// Make sure the page path is a dir if it exists, otherwise create it
if page_dir
.try_exists()
.context(ExistanceCheckSnafu { path: &page_dir })?
{
debug!("Validating page directory");
ensure!(page_dir.is_dir(), NotADirectorySnafu { path: &page_dir });
} else {
debug!("Creating page directory");
create_dir_all(&page_dir).context(CreateDirectorySnafu { path: &page_dir })?;
}
// Serialize the config
let config_path = page_dir.join("page.dhall");
debug!(?config_path, ?self.config, "Writing out page config");
let serialized_config = serde_dhall::serialize(&self.config)
.to_string()
.context(ConfigReifySnafu)?;
@ -96,16 +101,21 @@ impl Page {
.context(WriteConfigSnafu { path: &config_path })?;
// Create the page file if it doesn't exist
let page_path = page_dir.join(&self.file);
// Control flow is clearer this way since the `else` is just a log statement
#[allow(clippy::if_not_else)]
if !page_path
.try_exists()
.context(ExistanceCheckSnafu { path: &page_dir })?
{
debug!(?page_path, "Creating page file");
let contents = format!("# {}\n\n", self.config.title);
let mut page_file =
File::create(&page_path).context(WritePageStubSnafu { path: &page_path })?;
page_file
.write_all(contents.as_bytes())
.context(WritePageStubSnafu { path: &page_path })?;
} else {
debug!(?page_path, "Page file already exists");
}
Ok(())
}

View File

@ -9,7 +9,7 @@ use std::{
use chrono::{Local, NaiveDate};
use serde::{Deserialize, Serialize};
use snafu::{ensure, ResultExt};
use tracing::{debug, instrument};
use tracing::{debug, info, span, Level};
use super::{
ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError,
@ -71,7 +71,6 @@ impl Post {
///
/// Will return an error if serialization fails, the user does not have write permissions for
/// the directory, or any other IO errors occur while writing out the config.
#[instrument(skip(site_path, post_dir))]
pub fn write(
&self,
site_path: impl AsRef<Path>,
@ -79,18 +78,24 @@ impl Post {
) -> Result<(), SiteError> {
// build the post_dir
let post_dir = site_path.as_ref().join("posts").join(post_dir.as_ref());
debug!(?post_dir);
// Setup logging
let span = span!(Level::INFO, "Post::write", ?post_dir);
let _enter = span.enter();
info!("Writing out post");
// Make sure the post path is a dir if it exists, otherwise create it
if post_dir
.try_exists()
.context(ExistanceCheckSnafu { path: &post_dir })?
{
debug!("Validating post directory");
ensure!(post_dir.is_dir(), NotADirectorySnafu { path: &post_dir });
} else {
debug!("Creating post directory");
create_dir_all(&post_dir).context(CreateDirectorySnafu { path: &post_dir })?;
}
// Serialize the config
let config_path = post_dir.join("post.dhall");
debug!(?config_path, ?self.config, "Writing out config");
let serialized_config = serde_dhall::serialize(&self.config)
.to_string()
.context(ConfigReifySnafu)?;
@ -101,16 +106,21 @@ impl Post {
.context(WriteConfigSnafu { path: &config_path })?;
// Create the post file if it doesn't exist
let post_path = post_dir.join(&self.file);
// Control flow is clearer this way since the `else` is just a log statement
#[allow(clippy::if_not_else)]
if !post_path
.try_exists()
.context(ExistanceCheckSnafu { path: &post_dir })?
{
debug!(?post_path, "Creating post file");
let contents = format!("# {}\n\n", self.config.title);
let mut post_file =
File::create(&post_path).context(WritePostStubSnafu { path: &post_path })?;
post_file
.write_all(contents.as_bytes())
.context(WritePostStubSnafu { path: &post_path })?;
} else {
debug!(?post_path, "Post file already exists");
}
Ok(())
}