CF Workers logging

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
ゆめ 2024-11-15 00:03:16 -06:00
parent ad4a2beab4
commit 8e683d0fdb
No known key found for this signature in database
3 changed files with 43 additions and 2 deletions

35
src/cf_utils.rs Normal file
View file

@ -0,0 +1,35 @@
use log::Log;
#[allow(unsafe_code)]
mod bindings {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}
}
pub struct CfLogger;
impl Log for CfLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
if metadata
.target()
.starts_with("yumechi_no_kuni_proxy_worker")
{
return metadata.level() <= log::Level::Debug;
} else if metadata.target().starts_with("usvg") || metadata.target().starts_with("resvg") {
return metadata.level() <= log::Level::Error;
}
metadata.level() <= log::Level::Info
}
fn log(&self, record: &log::Record) {
bindings::log(&format!("[{}] {}", record.level(), record.args()));
}
fn flush(&self) {}
}

View file

@ -48,6 +48,10 @@ pub mod config;
/// Cross platform timing utilities
pub mod timing;
/// Utilities for Cloudflare Workers
#[cfg(feature = "cf-worker")]
mod cf_utils;
const MAX_SIZE: usize = 32 << 20;
#[cfg(all(not(feature = "cf-worker"), not(feature = "reqwest")))]
@ -72,6 +76,8 @@ async fn fetch(
use sandbox::NoSandbox;
use tower_service::Service;
log::set_logger(&cf_utils::CfLogger).ok();
let config = match Config::load_from_cf_env(env) {
Ok(config) => config,
Err(e) => {

View file

@ -1,4 +1,4 @@
use std::{io::Cursor, sync::Arc};
use std::io::Cursor;
use image::{
codecs::{png::PngDecoder, webp::WebPDecoder},
@ -115,7 +115,7 @@ pub fn postprocess_svg_image(
db.load_font_data(font_data);
}
Arc::new(db)
std::sync::Arc::new(db)
},
..Default::default()
},