feat: add file logging via LOG_FILE environment variable

This commit is contained in:
2025-02-23 02:49:35 -05:00
parent 6e75c4e586
commit b1dbfa6110
3 changed files with 43 additions and 9 deletions

View File

@@ -6,13 +6,27 @@ mod events;
mod http;
mod lua;
use azalea::{brigadier::prelude::CommandDispatcher, prelude::*};
use azalea::{
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
};
use bevy_app::PluginGroup;
use bevy_log::{
LogPlugin,
tracing_subscriber::{Layer, fmt::layer},
};
use clap::Parser;
use commands::{CommandSource, register};
use events::handle_event;
use futures::lock::Mutex;
use mlua::{Function, Lua};
use std::{collections::HashMap, net::SocketAddr, path::PathBuf, sync::Arc};
use std::{
collections::HashMap,
env,
fs::{OpenOptions, read_to_string},
net::SocketAddr,
path::PathBuf,
sync::Arc,
};
const DEFAULT_SCRIPT_PATH: &str = "errornowatcher.lua";
@@ -32,13 +46,15 @@ async fn main() -> anyhow::Result<()> {
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
let lua = Lua::new();
let globals = lua.globals();
globals.set("script_path", &*script_path)?;
lua::register_functions(&lua, &globals)?;
lua.load(
std::fs::read_to_string(&script_path)
read_to_string(script_path)
.expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")),
)
.exec()?;
let globals = lua.globals();
let server = globals
.get::<String>("Server")
.expect("Server should be in lua globals");
@@ -46,13 +62,27 @@ async fn main() -> anyhow::Result<()> {
.get::<String>("Username")
.expect("Username should be in lua globals");
globals.set("script_path", script_path)?;
lua::register_functions(&lua, &globals)?;
let mut commands = CommandDispatcher::new();
register(&mut commands);
let Err(error) = ClientBuilder::new()
let Err(error) = ClientBuilder::new_without_plugins()
.add_plugins(DefaultPlugins.set(LogPlugin {
custom_layer: |_| {
env::var("LOG_FILE").ok().map(|log_file| {
layer()
.with_writer(
OpenOptions::new()
.append(true)
.create(true)
.open(log_file)
.expect("should have been able to open log file"),
)
.boxed()
})
},
..Default::default()
}))
.add_plugins(DefaultBotPlugins)
.set_handler(handle_event)
.set_state(State {
lua,