-
Notifications
You must be signed in to change notification settings - Fork 0
/
classic-api.ts
97 lines (87 loc) · 2.88 KB
/
classic-api.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
import Ratlog, { RatlogData, Stringable } from "./ratlog.ts";
/**
* Interface representing a stream or function passed to the ratlogger constructor.
*
* It's either itself a function that takes data and returns nothing, or an object
* with a `write()` function that takes data and returns nothing.
*/
type Writer<T> = ((data: T) => void) | { write: (data: T) => void };
/**
* Identify the single function to be called when outputting to a writer
* @param writer writer to identify output function for
*/
const getWriteFunc = <T>(writer: Writer<T>) =>
"write" in writer ? writer.write.bind(writer) : writer;
/**
* Take a function and an object and produce a function with properties.
* @param func the function to attach properties to
* @param obj an object containing properties to copy to the function
*/
// deno-lint-ignore ban-types (reason: F is supposed to be any function)
const enrichFunction = <F extends Function, O>(func: F, obj: O): F & O =>
Object.assign(func, obj);
/**
* A `Ratlogger` is a function that takes the components of a `RatlogData` and
* does something with it. It also exposes a `tag` property-function which produces
* an identical `Ratlogger` with the added tags.
*/
export type Ratlogger =
& ((
message: Stringable,
fields?: RatlogData["fields"],
...tags: Stringable[]
) => void)
& { tag: (...tags: Stringable[]) => Ratlogger };
/**
* Constructor for more customizable logger instances.
* @param writer a writable stream or function that can take RatlogData
* @param tags a list of tags to apply to every output from this logger
*/
const generateRatlogger = (
writer: Writer<RatlogData>,
...tags: Stringable[]
): Ratlogger => {
const originalTags = tags;
return enrichFunction(
(
message: Stringable,
fields?: RatlogData["fields"],
...tags: Stringable[]
): void => {
getWriteFunc(writer)({
message,
fields,
tags: originalTags.concat(tags),
});
},
{
tag: (
/** a list of tags to apply to every output from this logger */
...tags: Stringable[]
): Ratlogger => generateRatlogger(writer, ...originalTags.concat(tags)),
},
);
};
const ratlog = (() => {
return enrichFunction(
/**
* @param writer a writable stream or function
* @param tags a list of tags to apply to every output from this logger
* @returns a logging function bound to the writer
*/
(writer: Writer<string>, ...tags: Stringable[]) =>
generateRatlogger(
(data: RatlogData) => getWriteFunc(writer)(Ratlog.format(data)),
...tags,
),
{
/** Constructor for more customizable logger instances. */
logger: generateRatlogger,
/** Exposure of the core Ratlog string formatter. */
stringify: Ratlog.format,
/** Exposure of the core Ratlog parser. */
parse: Ratlog.parse,
},
);
})();
export default ratlog;