-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes.ts
249 lines (220 loc) · 7.17 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2020-2024 the optic authors. All rights reserved. MIT license.
import type { Level } from "./logger/levels.ts";
/**
* Defines the flow of log records to a logging endpoint
*/
export interface Stream {
/**
* Optional. If implemented, this method is called after the stream is
* setup in the logger and provides the opportunity for the stream to output
* log header records.
*
* @param meta Contains metadata from the Logger instance
*/
logHeader?(meta: LogMeta): void;
/**
* Optional. If implemented, this method is called after the stream is
* destroyed via the logger and provides the opportunity for the stream to
* output log footer records.
*
* @param meta Contains metadata from the Logger instance
*/
logFooter?(meta: LogMeta): void;
/**
* Optional. Provides the opportunity for the stream to perform any required
* setup. This function is called when the stream is added to the logger.
*/
setup?(): void;
/**
* Optional. Provides the opportunity for the stream to perform any required
* teardown. This function is called when the stream is removed from the
* logger or the module exits
*/
destroy?(): void;
/**
* Handle the populated log record. This will, for example, format the log
* record and publish the resulting record to the end point.
*
* @param logRecord
*/
handle(logRecord: LogRecord): boolean;
}
/**
* Interface for formatting log records
*/
export interface Formatter<T> {
/**
* Given a logRecord instance, format it for output to the stream endpoint
* @param logRecord
*/
format(logRecord: LogRecord): T;
}
/**
* Define a function type for formatting Date to string
*/
export type DateTimeFormatterFn = (dateTime: Date) => string;
/**
* Interface for defining a class to format a Date to string
*/
export interface DateTimeFormatter {
formatDateTime: DateTimeFormatterFn;
}
/**
* Define a monitor. Monitors spy on log records and do not interfere in any
* way with the log process. Monitors may take additional actions based on
* conditions met by the log record, collect stats, etc..
*/
export type MonitorFn = (logRecord: LogRecord) => void;
/**
* Interface for defining a class to monitor log records and optionally take
* action
*/
export interface Monitor {
check: MonitorFn;
/**
* Optional. Provides the opportunity for the monitor to perform any required
* setup. This function is called when the monitor is added to the logger.
*/
setup?(): void;
/**
* Optional. Provides the opportunity for the monitor to perform any required
* teardown. This function is called when the monitor is removed from the
* logger or the module exits
*/
destroy?(): void;
}
/**
* Define a filter which takes in a stream and logRecord and returns true
* if the log record should be filtered out for this stream.
*/
export type FilterFn = (stream: Stream, logRecord: LogRecord) => boolean;
/**
* Interface for defining a class to model logic for filtering out log
* records from streams
*/
export interface Filter {
shouldFilterOut: FilterFn;
/**
* Optional. Provides the opportunity for the filter to perform any required
* setup. This function is called when the filter is added to the logger.
*/
setup?(): void;
/**
* Optional. Provides the opportunity for the filter to perform any required
* teardown. This function is called when the filter is removed from the
* logger or the module exits
*/
destroy?(): void;
}
/**
* Define an transformer for transforming a log record. The output will
* be either the same log record, untouched, or a new log record based on the
* original, but with one or more change present.
*/
export type TransformerFn = (stream: Stream, logRecord: LogRecord) => LogRecord;
/**
* Interface for defining a class to model logic for transforming log records
*/
export interface Transformer {
transform: TransformerFn;
/**
* Optional. Provides the opportunity for the transformer to perform any required
* setup. This function is called when the transformer is added to the logger.
*/
setup?(): void;
/**
* Optional. Provides the opportunity for the transformer to perform any required
* teardown. This function is called when the transformer is removed from the
* logger or the module exits
*/
destroy?(): void;
}
/**
* The core data captured during a log event
*/
export interface LogRecord {
/** The primary log message */
readonly msg: unknown;
/** Supporting metadata for the log event */
readonly metadata: unknown[];
/** The date and time the log event was initiated */
readonly dateTime: Date;
/** The log level for this event */
readonly level: Level;
/** The name of the logger which created the log record */
readonly logger: string;
}
/**
* Metadata around the logger itself, used for outputting headers and footers
* in the logging endpoint
*/
export interface LogMeta {
/** The hostname the process is running on */
readonly hostname: string;
/** The date and time the logging session started */
readonly sessionStarted: Date;
/** The date and time the logging session ended (or undefined if still active)*/
sessionEnded?: Date;
/** The min log level of the logger */
minLogLevel: Level;
/** Where the min log level was sourced from */
minLogLevelFrom: string;
/** The name of the logger for this metadata */
readonly logger: string;
/** Count of handled (Map of Level -> count), filtered, transformed and duplicated records by stream */
readonly streamStats: Map<
Stream,
{
handled: Map<number, number>;
filtered: number;
transformed: number;
duplicated: number;
}
>;
/** Number of filters added. (Removed filters do not subtract from this total) */
readonly filters: number;
/** Number of transformers added. (Removed transformers do not subtract from this total) */
readonly transformers: number;
/** Number of monitors added. (Removed monitors do not subtract from this total) */
readonly monitors: number;
}
export class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "ValidationError";
}
}
export class IllegalStateError extends Error {
constructor(message: string) {
super(message);
this.name = "IllegalStateError";
}
}
export class TimeUnit {
public static MILLISECONDS: TimeUnit = new TimeUnit(1);
public static SECONDS: TimeUnit = new TimeUnit(1000);
public static MINUTES: TimeUnit = new TimeUnit(60000);
public static HOURS: TimeUnit = new TimeUnit(3600000);
public static DAYS: TimeUnit = new TimeUnit(86400000);
private constructor(private milliseconds: number) {}
getMilliseconds(): number {
return this.milliseconds;
}
}
/**
* Represents a point in time snapshot of application profiling
*/
export interface ProfileMark {
/** Number of ms since process start */
timestamp: number;
/** Details on current memory usage */
memory?: Deno.MemoryUsage;
/** Label for the profile mark */
label?: string;
}
/**
* Formatter of output of profiling information to logs
*/
export interface MeasureFormatter<T> {
format(startMark: ProfileMark, endMark: ProfileMark, label?: string): T;
}