Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
feat: log rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
funatsufumiya committed Sep 2, 2023
1 parent 074dcc4 commit de1bfe5
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::env;
use std::io::{self, BufRead};
use std::io::Write;
use std::process::Command;
use std::fs::File;
use std::io::prelude::*;

use serde::{Deserialize, Serialize};

use simple_home_dir::expand_tilde;
use file_rotate::{FileRotate, ContentLimit, suffix::AppendCount, compression::Compression};

// event json examples
// init
// { "event": "init", "operation": "download", "remote": "origin", "concurrent": true, "concurrenttransfers": 3 }
Expand Down Expand Up @@ -116,19 +118,26 @@ fn respond(obj: Response) {
}

fn get_log_file_path() -> String {
let script_dir = env::current_dir().unwrap();
let log_dir = script_dir.join("logs");
let home_dir = expand_tilde("~/.git-lfs-agent-rclone").unwrap();
let log_dir = home_dir.join("logs");
// create log dir if not exist
if !log_dir.exists() {
std::fs::create_dir(log_dir.clone()).unwrap();
std::fs::create_dir_all(log_dir.clone()).unwrap();
}
let log_file_path = log_dir.join("git-lfs-rs.error.log");
let log_file_path = log_dir.join("errors.log");
log_file_path.to_str().unwrap().to_string()
}

fn get_log_file() -> File {
fn get_log_file() -> FileRotate<AppendCount> {
let log_file_path = get_log_file_path();
let log_file = File::create(log_file_path).unwrap();
let log_file = FileRotate::new(
log_file_path.clone(),
AppendCount::new(3),
ContentLimit::Lines(1000),
Compression::None,
#[cfg(unix)]
None,
);
log_file
}

Expand Down Expand Up @@ -195,7 +204,7 @@ fn main() {

// error log
let mut log_file = get_log_file();
log_file.write_all(format!("upload failed: {:?} ( request json: {:?})", cmd, ev).as_bytes()).unwrap()
writeln!(log_file, "upload failed: {:?} ( request json: {:?})", cmd, ev).unwrap();
}
} else if event == "download" {
let ev = match obj {
Expand Down Expand Up @@ -233,7 +242,7 @@ fn main() {

// error log
let mut log_file = get_log_file();
log_file.write_all(format!("download failed: {:?} ( request json: {:?})", cmd, ev).as_bytes()).unwrap();
writeln!(log_file, "download failed: {:?} ( request json: {:?})", cmd, ev).unwrap();
}
}
}
Expand Down

0 comments on commit de1bfe5

Please sign in to comment.