mirror of
https://github.com/ClaytonWWilson/stokpile.git
synced 2025-12-15 23:18:50 +00:00
Add callback output
This commit is contained in:
parent
abb548f5ec
commit
e094dd16a3
20
src/outputs/Callback.ts
Normal file
20
src/outputs/Callback.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { LogContext, LogMeta } from "../types/logger";
|
||||||
|
import { Loggable } from "../interface/output";
|
||||||
|
import { CallbackOutputConfig } from "../types/output";
|
||||||
|
|
||||||
|
export default class Callback implements Loggable {
|
||||||
|
private callback:
|
||||||
|
| ((message: string, meta: LogMeta, context: LogContext) => void)
|
||||||
|
| ((message: string, meta: LogMeta, context: LogContext) => Promise<void>)
|
||||||
|
| undefined;
|
||||||
|
enabled: boolean;
|
||||||
|
constructor(config: CallbackOutputConfig) {
|
||||||
|
this.enabled = config.enabled;
|
||||||
|
this.callback = config.callback;
|
||||||
|
}
|
||||||
|
write(message: string, meta: LogMeta, context: LogContext): void {
|
||||||
|
if (this.callback) {
|
||||||
|
this.callback(message, meta, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
export enum LogLevel {
|
export enum LogLevel {
|
||||||
TRACE = 10,
|
TRACE = 10,
|
||||||
DEBUG = 20,
|
DEBUG = 20,
|
||||||
@ -14,14 +16,16 @@ export enum LogLabel {
|
|||||||
FATAL = "fatal",
|
FATAL = "fatal",
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LogContext {
|
export const LogContext = z.record(z.any());
|
||||||
[key: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface LogMeta {
|
export type LogContext = z.infer<typeof LogContext>;
|
||||||
level: LogLevel;
|
|
||||||
time: Date;
|
export const LogMeta = z.object({
|
||||||
}
|
level: z.nativeEnum(LogLevel),
|
||||||
|
time: z.date(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type LogMeta = z.infer<typeof LogMeta>;
|
||||||
|
|
||||||
export interface LogData {
|
export interface LogData {
|
||||||
message: string;
|
message: string;
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import { LogContext, LogMeta } from "logger";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
export const ConsoleStyles = z
|
export const ConsoleStyles = z
|
||||||
@ -123,3 +124,11 @@ export interface TampermonkeyBucketInfo {
|
|||||||
size: number;
|
size: number;
|
||||||
createdAt: number;
|
createdAt: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CallbackOutputConfig {
|
||||||
|
enabled: boolean;
|
||||||
|
callback:
|
||||||
|
| ((message: string, meta: LogMeta, context: LogContext) => void)
|
||||||
|
| ((message: string, meta: LogMeta, context: LogContext) => Promise<void>)
|
||||||
|
| undefined;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user