-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tee.js
47 lines (39 loc) · 1.51 KB
/
tee.js
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
// Ensure Bun compatibility. [It currently lacks support for TextEncoderStream](https://github.com/oven-sh/bun/issues/5648)
import "@denosaurs/log/transforms/text_encoder_stream";
import { ConsoleReadableStream } from "@denosaurs/log";
import {
StderrWritableStream,
StdoutWritableStream,
} from "@denosaurs/log/writables/std";
import { OmitLogLevelStream } from "@denosaurs/log/transforms/omit";
import { PickLogLevelStream } from "@denosaurs/log/transforms/pick";
import { JsonStringifyStream } from "@std/json";
// Capture logs from the console
const stream = new ConsoleReadableStream();
// Split the stream in two
const [a, b] = stream.tee();
a
// Omit only the error logs
.pipeThrough(new OmitLogLevelStream("error"))
// Stringify the logs to JSON
.pipeThrough(new JsonStringifyStream())
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stdout
.pipeTo(new StdoutWritableStream());
b
// Pick only the error logs
.pipeThrough(new PickLogLevelStream("error"))
// Stringify the logs to JSON
.pipeThrough(new JsonStringifyStream())
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stderr
.pipeTo(new StderrWritableStream());
// Log some messages
console.error("This is going to stderr");
console.trace("This is going to stdout");
console.debug("This is going to stdout");
console.info("This is going to stdout");
console.warn("This is going to stdout");
console.log("This is going to stdout");