Skip to content

Commit

Permalink
feat: Add log optimization option and enhance logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Jan 2, 2025
1 parent bd59326 commit ebd90ca
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
6 changes: 6 additions & 0 deletions api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,12 @@ paths:
type: string
format: date-time
description: End time for log deletion
- name: optimize
in: query
schema:
type: boolean
default: true
description: Whether to optimize the log after clearing
responses:
"200":
description: OK
Expand Down
6 changes: 6 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type ConfigType = {
max_import_img_count: number;
enable_server_timing: boolean;
thumbnail_format: ThumbnailFormat;
/** Enable logging stack for all log levels */
logging_stack: boolean;
};

export enum ThumbnailMethod {
Expand Down Expand Up @@ -254,6 +256,9 @@ export class Config {
get enable_server_timing() {
return this._return_bool("enable_server_timing") ?? false;
}
get logging_stack() {
return this._return_bool("logging_stack") ?? false;
}
to_json(): ConfigType {
return {
cookies: typeof this.cookies === "string",
Expand Down Expand Up @@ -294,6 +299,7 @@ export class Config {
max_import_img_count: this.max_import_img_count,
enable_server_timing: this.enable_server_timing,
thumbnail_format: this.thumbnail_format,
logging_stack: this.logging_stack,
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ if (cmd == CMD.Unknown) {
}

const settings = await load_settings(args.config);
await base_logger.init(settings.db_path || settings.base);
await base_logger.init(settings);
if (!check_file_permissions(settings.base)) {
throw Error("Can not aceess download loaction.");
}
Expand Down
4 changes: 3 additions & 1 deletion routes/api/log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Handlers } from "$fresh/server.ts";
import { return_data, return_error } from "../../server/utils.ts";
import { User, UserPermission } from "../../db.ts";
import { get_string, parse_int } from "../../server/parse_form.ts";
import { get_string, parse_bool, parse_int } from "../../server/parse_form.ts";
import { base_logger, LogLevel } from "../../utils/logger.ts";

export const handler: Handlers = {
Expand Down Expand Up @@ -33,7 +33,9 @@ export const handler: Handlers = {
base_logger.log("Failed to parse end_time:", e);
return return_error(1, "Failed to parse end_time.");
}
const optimize = await parse_bool(form.get("optimize"), true);
base_logger.clear(typ, min_level, max_level, deleted_level, end_time);
if (optimize) base_logger.optimize();
return return_data(true);
},
async GET(req, ctx) {
Expand Down
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const renderFn: RenderFunction = (ctx, render) => {
export async function startServer(path: string) {
cfg_path = path;
const cfg = await load_settings(path);
await base_logger.init(cfg.db_path || cfg.base);
await base_logger.init(cfg);
task_manager = new TaskManager(cfg);
await task_manager.init();
task_manager.run(true).catch((e) => {
Expand Down
15 changes: 12 additions & 3 deletions utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join } from "@std/path";
import { format as format_ver, parse as parse_ver } from "@std/semver";
import type { Config } from "../config.ts";
import { parse_bool, stackTrace } from "../utils.ts";
import { Db, QueryParameterSet, SqliteMaster } from "./db_interface.ts";

Expand Down Expand Up @@ -60,10 +61,13 @@ export function format_message(

class BaseLogger {
db?: Db;
#cfg?: Config;
#exist_table: Set<string> = new Set();
#use_ffi = false;
readonly version = parse_ver("1.0.0-0");
async init(base_path: string) {
async init(cfg: Config) {
this.#cfg = cfg;
const base_path = cfg.db_path || cfg.base;
const db_path = join(base_path, "logs.db");
this.#use_ffi = parse_bool(Deno.env.get("DB_USE_FFI") ?? "false");
if (this.#use_ffi) {
Expand Down Expand Up @@ -133,9 +137,10 @@ class BaseLogger {
}
add(type: string, level: number, ...messages: unknown[]) {
this.#fallback(type, level, ...messages);
if (!this.db) return;
if (!this.db || !this.#cfg) return;
const message = format_message(messages);
const stack = (level >= LogLevel.Trace && level < LogLevel.Debug) ||
const stack = this.#cfg.logging_stack ||
(level >= LogLevel.Trace && level < LogLevel.Debug) ||
level >= LogLevel.Warn
? stackTrace(2)
: undefined;
Expand Down Expand Up @@ -360,6 +365,10 @@ class BaseLogger {
log(type: string, ...messages: unknown[]) {
this.add(type, LogLevel.Log, ...messages);
}
optimize() {
if (!this.db) return;
this.db.query("VACUUM;");
}
trace(type: string, ...messages: unknown[]) {
this.add(type, LogLevel.Trace, ...messages);
}
Expand Down

0 comments on commit ebd90ca

Please sign in to comment.