UNPKG

2.97 kBPlain TextView Raw
1import { type AsyncLocalStorage } from 'node:async_hooks';
2
3type JsonValue =
4 | JsonObject
5 | JsonValue[]
6 | boolean
7 | number
8 | string
9 | readonly JsonValue[]
10 | null
11 | undefined;
12
13/**
14 * @see https://stackoverflow.com/a/77390832/368691
15 * @public
16 */
17export type JsonObject = {
18 [k: string]: JsonValue;
19};
20
21export type LogWriter = (message: string) => void;
22
23export type MessageContext<T = {}> = JsonObject & T;
24
25export type TopLevelAsyncLocalContext = {
26 messageContext: MessageContext;
27 transforms: ReadonlyArray<TransformMessageFunction<MessageContext>>;
28};
29
30type NestedAsyncLocalContext = TopLevelAsyncLocalContext & {
31 sequence: number;
32 sequenceRoot: string;
33};
34
35export type AsyncLocalContext =
36 | NestedAsyncLocalContext
37 | TopLevelAsyncLocalContext;
38
39export type MessageSerializer = (message: Message<MessageContext>) => string;
40
41export type RoarrGlobalState = {
42 asyncLocalStorage?: AsyncLocalStorage<AsyncLocalContext>;
43 onceLog: Set<string>;
44 sequence: number;
45 serializeMessage?: MessageSerializer;
46 versions: readonly string[];
47 write: LogWriter;
48};
49
50type SprintfArgument = boolean | number | string | null;
51
52export type Message<T = MessageContext> = {
53 readonly context: T;
54 readonly message: string;
55 readonly sequence: string;
56 readonly time: number;
57 readonly version: string;
58};
59
60export type TransformMessageFunction<T> = (
61 message: Message<T>,
62) => Message<MessageContext>;
63
64type LogMethod<Z> = {
65 <T extends string = string>(
66 context: Z,
67 message: T,
68 c?: T extends `${string}%${string}` ? SprintfArgument : never,
69 d?: SprintfArgument,
70 e?: SprintfArgument,
71 f?: SprintfArgument,
72 g?: SprintfArgument,
73 h?: SprintfArgument,
74 index?: SprintfArgument,
75 index_?: SprintfArgument,
76 ): void;
77 <T extends string = string>(
78 message: T,
79 b?: T extends `${string}%${string}` ? SprintfArgument : never,
80 c?: SprintfArgument,
81 d?: SprintfArgument,
82 e?: SprintfArgument,
83 f?: SprintfArgument,
84 g?: SprintfArgument,
85 h?: SprintfArgument,
86 index?: SprintfArgument,
87 index_?: SprintfArgument,
88 ): void;
89};
90
91type Child<Z> = {
92 <T = Z>(context: TransformMessageFunction<MessageContext<T>>): Logger<T | Z>;
93 (context: MessageContext): Logger<Z>;
94};
95
96export type Logger<Z = MessageContext> = LogMethod<Z> & {
97 adopt: <T>(
98 routine: () => T,
99 context?: MessageContext | TransformMessageFunction<MessageContext>,
100 ) => Promise<T>;
101 child: Child<Z>;
102 debug: LogMethod<Z>;
103 debugOnce: LogMethod<Z>;
104 error: LogMethod<Z>;
105 errorOnce: LogMethod<Z>;
106 fatal: LogMethod<Z>;
107 fatalOnce: LogMethod<Z>;
108 getContext: () => MessageContext;
109 info: LogMethod<Z>;
110 infoOnce: LogMethod<Z>;
111 trace: LogMethod<Z>;
112 traceOnce: LogMethod<Z>;
113 warn: LogMethod<Z>;
114 warnOnce: LogMethod<Z>;
115};
116
117export type MessageEventHandler = (message: Message<MessageContext>) => void;
118
119export type LogLevelName =
120 | 'debug'
121 | 'error'
122 | 'fatal'
123 | 'info'
124 | 'trace'
125 | 'warn';