feat: replace log crate with tracing

This commit is contained in:
Aravinth Manivannan 2022-11-11 14:56:36 +05:30
parent 58bb606879
commit 0b2db58483
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
7 changed files with 90 additions and 31 deletions

68
Cargo.lock generated
View File

@ -1468,7 +1468,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "975de676448231fcde04b9149d2543077e166b78fc29eae5aa219e7928410da2"
dependencies = [
"uuid",
"uuid 0.8.2",
]
[[package]]
@ -1601,7 +1601,6 @@ dependencies = [
"futures",
"git2",
"lazy_static",
"log",
"mime",
"mime_guess",
"mktemp",
@ -1615,6 +1614,8 @@ dependencies = [
"sqlx",
"tera",
"tokio",
"tracing",
"tracing-actix-web",
"url",
"urlencoding",
]
@ -1767,6 +1768,26 @@ dependencies = [
"siphasher",
]
[[package]]
name = "pin-project"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.2.9"
@ -2554,21 +2575,45 @@ dependencies = [
[[package]]
name = "tracing"
version = "0.1.36"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fce9567bd60a67d08a16488756721ba392f24f29006402881e43b19aac64307"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.29"
name = "tracing-actix-web"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeea4303076558a00714b823f9ad67d58a3bbda1df83d8827d21193156e22f7"
checksum = "d725b8fa6ef307b3f4856913523337de45c47cc79271bafd7acfb39559e3a2da"
dependencies = [
"actix-web",
"pin-project",
"tracing",
"uuid 1.2.1",
]
[[package]]
name = "tracing-attributes"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
dependencies = [
"once_cell",
]
@ -2726,6 +2771,15 @@ dependencies = [
"getrandom",
]
[[package]]
name = "uuid"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83"
dependencies = [
"getrandom",
]
[[package]]
name = "validator"
version = "0.15.0"

View File

@ -29,7 +29,6 @@ serde = { version = "1", features = ["derive", "rc"]}
serde_json = "1"
pretty_env_logger = "0.4"
log = "0.4"
lazy_static = "1.4"
@ -47,7 +46,8 @@ mime_guess = "2.0.4"
mime = "0.3.16"
rust-embed = "6.3.0"
rand = "0.8.5"
tracing = { version = "0.1.37", features = ["log"]}
tracing-actix-web = "0.6.2"
[dependencies.cache-buster]
git = "https://github.com/realaravinth/cache-buster"

View File

@ -20,6 +20,7 @@ use std::thread;
use crate::db::*;
use crate::settings::Settings;
use argon2_creds::{Config as ArgonConfig, ConfigBuilder as ArgonConfigBuilder, PasswordPolicy};
use tracing::info;
pub mod api;
@ -51,9 +52,9 @@ impl Ctx {
#[allow(unused_variables)]
let init = thread::spawn(move || {
log::info!("Initializing credential manager");
info!("Initializing credential manager");
c.init();
log::info!("Initialized credential manager");
info!("Initialized credential manager");
});
let db = get_db(&settings).await;

View File

@ -22,6 +22,7 @@ use sqlx::types::time::OffsetDateTime;
//use sqlx::types::Json;
use sqlx::ConnectOptions;
use sqlx::PgPool;
use tracing::error;
use url::quirks::hostname;
use crate::errors::*;
@ -490,7 +491,7 @@ fn map_register_err(e: sqlx::Error) -> ServiceError {
} else if msg.contains("librepages_users_email_key") {
ServiceError::EmailTaken
} else {
log::error!("{}", msg);
error!("{}", msg);
ServiceError::InternalServerError
}
} else {

View File

@ -22,8 +22,9 @@ use actix_web::{
web::JsonConfig, App, HttpServer,
};
use clap::{Parser, Subcommand};
use log::info;
use static_assets::FileMap;
use tracing::info;
use tracing_actix_web::TracingLogger;
mod api;
mod ctx;
@ -80,10 +81,11 @@ enum Commands {
#[actix_web::main]
#[cfg(not(tarpaulin_include))]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "info");
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info");
}
pretty_env_logger::init();
let cli = Cli::parse();
info!(
@ -111,7 +113,7 @@ async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
info!("Starting server on: http://{}", ip);
HttpServer::new(move || {
App::new()
.wrap(actix_middleware::Logger::default())
.wrap(TracingLogger::default())
.wrap(actix_middleware::Compress::default())
.app_data(ctx.clone())
.app_data(get_json_err())

View File

@ -14,16 +14,18 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use git2::{build::CheckoutBuilder, BranchType, Direction, Oid, Remote, Repository};
#[cfg(not(test))]
use log::info;
#[cfg(test)]
use std::println as info;
#[cfg(test)]
use std::println as error;
#[cfg(test)]
use std::println as debug;
use git2::{build::CheckoutBuilder, BranchType, Direction, Oid, Remote, Repository};
use serde::Deserialize;
use serde::Serialize;
#[cfg(not(test))]
use tracing::{debug, error, info};
use crate::db::Site;
use crate::errors::*;
@ -95,7 +97,7 @@ impl Page {
branch: &str,
) -> ServiceResult<git2::AnnotatedCommit<'a>> {
let mut remote = repo.find_remote("origin")?;
log::info!("Fetching {} for repo", remote.name().unwrap());
info!("Fetching {} for repo", remote.name().unwrap());
remote.fetch(&[branch], None, None)?;
let fetch_head = repo.find_reference("FETCH_HEAD")?;
Ok(repo.reference_to_annotated_commit(&fetch_head)?)
@ -112,20 +114,19 @@ impl Page {
// 2. Do the appropriate merge
if analysis.0.is_fast_forward() {
//log::debug!("Doing a fast forward");
log::debug!("Doing a fast forward");
debug!("Doing a fast forward");
// do a fast forward
let refname = format!("refs/heads/{}", branch);
match repo.find_reference(&refname) {
Ok(mut r) => {
log::debug!("fast forwarding");
debug!("fast forwarding");
Self::fast_forward(repo, &mut r, &fetch_commit).unwrap();
}
Err(_) => {
// The branch doesn't exist so just set the reference to the
// commit directly. Usually this is because you are pulling
// into an empty repository.
log::error!("Error in find ref");
error!("Error in find ref");
repo.reference(
&refname,
fetch_commit.id(),
@ -151,7 +152,7 @@ impl Page {
.unwrap();
Self::normal_merge(repo, &head_commit, &fetch_commit).unwrap();
} else {
log::info!("Nothing to do...");
info!("Nothing to do...");
}
Ok(())
}
@ -178,7 +179,7 @@ impl Page {
) -> Result<(), git2::Error> {
let local_tree = repo.find_commit(local.id())?.tree().unwrap();
let remote_tree = repo.find_commit(remote.id())?.tree().unwrap();
println!("{} {}", local.id(), remote.id());
debug!("{} {}", local.id(), remote.id());
let ancestor = repo
.find_commit(repo.merge_base(local.id(), remote.id()).unwrap())
.unwrap()
@ -189,7 +190,7 @@ impl Page {
.unwrap();
if idx.has_conflicts() {
log::debug!("Merge conflicts detected...");
debug!("Merge conflicts detected...");
repo.checkout_index(Some(&mut idx), None)?;
return Ok(());
}
@ -223,7 +224,7 @@ impl Page {
None => String::from_utf8_lossy(lb.name_bytes()).to_string(),
};
let msg = format!("Fast-Forward: Setting {} to id: {}", name, rc.id());
log::debug!("{}", msg);
debug!("{}", msg);
lb.set_target(rc.id(), &msg)?;
repo.set_head(&name)?;
repo.checkout_head(Some(git2::build::CheckoutBuilder::default().force()))?;

View File

@ -21,7 +21,7 @@ use std::sync::Arc;
use config::{Config, ConfigError, Environment, File};
use derive_more::Display;
#[cfg(not(test))]
use log::{error, warn};
use tracing::{error, warn};
#[cfg(test)]
use std::{println as warn, println as error};