mirror of
https://github.com/ClaytonWWilson/stokpile.git
synced 2025-12-15 23:48:50 +00:00
Add Buffered interface for outputs that maintain an internal buffer
This commit is contained in:
parent
2297f9928a
commit
8e1e988cea
@ -1,5 +1,5 @@
|
|||||||
import { LogContext, LogLevel, LogMeta } from "./types/logger";
|
import { LogContext, LogLevel, LogMeta } from "./types/logger";
|
||||||
import { Loggable, Storable } from "./interface/output";
|
import { Buffered, Loggable, Storable } from "./interface/output";
|
||||||
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
private outputs: Loggable[] = [];
|
private outputs: Loggable[] = [];
|
||||||
@ -17,8 +17,6 @@ export class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log(message: string, level: LogLevel, context: LogContext) {
|
log(message: string, level: LogLevel, context: LogContext) {
|
||||||
// const label = getLabel(level);
|
|
||||||
|
|
||||||
const meta: LogMeta = {
|
const meta: LogMeta = {
|
||||||
level,
|
level,
|
||||||
time: new Date(),
|
time: new Date(),
|
||||||
@ -55,4 +53,18 @@ export class Logger {
|
|||||||
fatal(message: string, context: LogContext = {}) {
|
fatal(message: string, context: LogContext = {}) {
|
||||||
this.log(message, LogLevel.FATAL, context);
|
this.log(message, LogLevel.FATAL, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async flush() {
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
|
for (const output of this.outputs) {
|
||||||
|
// BUG: Typescript doesn't realize that outputs can be Storables or Buffered as well
|
||||||
|
// @ts-ignore
|
||||||
|
if (output.flush) {
|
||||||
|
promises.push((output as Buffered).flush());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.allSettled(promises);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,3 +9,7 @@ export abstract class Storable extends Loggable {
|
|||||||
entries: number
|
entries: number
|
||||||
): Promise<{ message: string; meta: LogMeta; context: LogContext }[]>;
|
): Promise<{ message: string; meta: LogMeta; context: LogContext }[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export abstract class Buffered extends Storable {
|
||||||
|
abstract flush(): Promise<void>;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { Storable } from "../interface/output";
|
import { Buffered } from "../interface/output";
|
||||||
import {
|
import {
|
||||||
StrictTampermonkeyOutputConfig,
|
StrictTampermonkeyOutputConfig,
|
||||||
TampermonkeyBucketInfo,
|
TampermonkeyBucketInfo,
|
||||||
@ -7,7 +7,7 @@ import {
|
|||||||
import { gzip, randomString, ungzip } from "../lib/utilities";
|
import { gzip, randomString, ungzip } from "../lib/utilities";
|
||||||
import { LogContext, LogData, LogMeta } from "../types/logger";
|
import { LogContext, LogData, LogMeta } from "../types/logger";
|
||||||
|
|
||||||
export default class Tampermonkey implements Storable {
|
export default class Tampermonkey implements Buffered {
|
||||||
private buffer: LogData[];
|
private buffer: LogData[];
|
||||||
private bucketList: TampermonkeyBucketInfo[];
|
private bucketList: TampermonkeyBucketInfo[];
|
||||||
private bufferCapacity: number;
|
private bufferCapacity: number;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user