add logger

This commit is contained in:
Zengtudor 2024-07-20 23:14:15 +08:00
parent 41fdf20598
commit 2279448436
6 changed files with 40 additions and 21 deletions

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
env_logger = "0.11.3"
log = "0.4.22"
sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] } sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] }
# serde = "1.0.204" # serde = "1.0.204"
# serde_json = "1.0.120" # serde_json = "1.0.120"

View File

@ -1,6 +1,6 @@
use std::error::Error; use std::error::Error;
use sea_orm::{ ActiveModelTrait, ActiveValue};
use start_db::Starter; use start_db::Starter;
use log::{error, info};
// use serde_json::json; // use serde_json::json;
use tokio; use tokio;
@ -9,20 +9,17 @@ pub mod start_db;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
let db_info = Starter::connect().await?; env_logger::init();
println!("The program is starting.\nIf you want to see the log info,please set env RUST_LOG=info");
info!("DataBase is starting");
let db_info = Starter::connect("sqlite://test.db?mode=rwc").await?;
db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{ db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{
println!("The table have been created.\nDon't care this Error:{}",e); info!("The table have been created. Don't care this Error!!!!!:{}",e);
});
word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e|{
error!("Cannot write data into database.The details are as follows:\n{}",e);
}); });
let insert_word = word_entity::ActiveModel{
id:ActiveValue::not_set(),
bsm: ActiveValue::set("test".to_string()),
hz: ActiveValue::set("test".to_string()),
py_js: ActiveValue::set("test".to_string()),
};
println!("inserting tests into words table in tests.db");
insert_word.insert(&db_info.db).await?;
Ok(()) Ok(())
} }

View File

@ -3,20 +3,20 @@ use std::error::Error;
use sea_orm::Schema; use sea_orm::Schema;
pub struct Starter { pub struct Starter {
pub db: DatabaseConnection, pub conn: DatabaseConnection,
} }
impl Starter { impl Starter {
pub async fn connect() -> Result<Self, Box<dyn Error>> { pub async fn connect(db_url:&str) -> Result<Self, Box<dyn Error>> {
let opt: ConnectOptions = ConnectOptions::new("sqlite://test.db?mode=rwc"); let opt: ConnectOptions = ConnectOptions::new(db_url);
let db_new: DatabaseConnection = Database::connect(opt).await?; let conn_new: DatabaseConnection = Database::connect(opt).await?;
Ok(Starter{db:db_new}) Ok(Starter{conn:conn_new})
} }
pub async fn create_table(&self) -> Result<(), Box<dyn Error>> { pub async fn create_table(&self) -> Result<(), Box<dyn Error>> {
let backend = self.db.get_database_backend(); let backend = self.conn.get_database_backend();
let schema = Schema::new(backend); let schema = Schema::new(backend);
let table_create_statement = schema.create_table_from_entity(super::word_entity::Entity); let table_create_statement = schema.create_table_from_entity(super::word_entity::Entity);
let table_create_result = self.db.execute(backend.build(&table_create_statement)).await?; let table_create_result = self.conn.execute(backend.build(&table_create_statement)).await?;
println!("{:?}",table_create_result); println!("{:?}",table_create_result);
Ok(()) Ok(())
} }

0
src/test_db.rs Normal file
View File

View File

@ -1,4 +1,5 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
pub mod tests;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "words")] #[sea_orm(table_name = "words")]

19
src/word_entity/tests.rs Normal file
View File

@ -0,0 +1,19 @@
use std::error::Error;
use log::info;
use sea_orm::{ActiveModelTrait, ActiveValue, DatabaseConnection};
use super::ActiveModel;
pub async fn insert_tests_value(conn:&DatabaseConnection)->Result<(),Box<dyn Error>>{
let insert_word = ActiveModel{
id:ActiveValue::not_set(),
bsm: ActiveValue::set("test".to_string()),
hz: ActiveValue::set("test".to_string()),
py_js: ActiveValue::set("test".to_string()),
};
info!("Inserting tests into words table in tests.db");
insert_word.insert(conn).await?;
info!("Insert done.Test done");
Ok(())
}