Skip to content

Commit

Permalink
clean logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Sep 6, 2023
1 parent dff9b3d commit 4068324
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
34 changes: 28 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ koit = "0.2.0"
twitter-v2 = "0.1.8"
serde_json = "1.0.105"
bigdecimal = "0.4.1"
log = "0.4.20"
chrono = "0.4.29"
31 changes: 31 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use log::{Level, Metadata, Record};

pub struct SimpleLogger;

impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
true //metadata.level() <= Level::Info
}

fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
println!(
"{} {:<10}:{} {:<6}{}",
date(),
record.file().unwrap(),
record.line().unwrap(),
record.level(),
record.args()
);
}
}

fn flush(&self) {}
}

fn date() -> String {
let offset = chrono::FixedOffset::east_opt(7200).unwrap();
let timezone: chrono::FixedOffset = chrono::offset::TimeZone::from_offset(&offset);
let current_time = chrono::Utc::now().with_timezone(&timezone);
current_time.format("%F %T").to_string()
}
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
use dotenv::dotenv;
use log::{debug, error, info, trace, warn};
use logger::SimpleLogger;
use std::error::Error;
use twitter::tweet;

mod api;
mod db;
mod logger;
mod twitter;

const COINCAP_API_KEY: &str = "COINCAP_API_KEY";
const NODE_PROVIDER_API_KEY: &str = "NODE_PROVIDER_API_KEY";
const TWITTER_OAUTH2_CLIENT_ID: &str = "TWITTER_OAUTH2_CLIENT_ID";
const TWITTER_OAUTH2_CLIENT_SECRET: &str = "TWITTER_OAUTH2_CLIENT_SECRET";

use log::{LevelFilter, SetLoggerError};

pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&SimpleLogger).map(|()| log::set_max_level(LevelFilter::Trace))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
init().unwrap();
check_valid_env();

tweet("Someteaeazzhing".to_string()).await;
info!("info");
trace!("trace");
warn!("warn");
debug!("debug");
error!("error");
// tweet("Someteaeazzhing".to_string()).await;
Ok(())
}

Expand Down
7 changes: 4 additions & 3 deletions src/twitter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use dotenv::dotenv;
use log::info;
use std::ops::Deref;
use tokio::sync::Mutex;
use twitter_v2::{
authorization::{Oauth2Client, Oauth2Token},
TwitterApi,
};

// TODO Update gitignore when all js gone
const PATH_TO_TOKEN_FILE : &str = "./db/token.json";
// TODO Update gitignore when all js gone
const PATH_TO_TOKEN_FILE: &str = "./db/token.json";
use crate::{TWITTER_OAUTH2_CLIENT_ID, TWITTER_OAUTH2_CLIENT_SECRET};
pub async fn tweet(text_to_tweet: String) {
dotenv().ok();
Expand Down Expand Up @@ -36,7 +37,7 @@ pub async fn tweet(text_to_tweet: String) {
.await
.unwrap()
{
println!("Refreshing token");
info!("Refreshing token");
serde_json::to_writer(
std::fs::File::create(PATH_TO_TOKEN_FILE).expect("token file not found"),
token.deref(),
Expand Down

0 comments on commit 4068324

Please sign in to comment.