UNPKG

1.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const cli_config_1 = require("./cli-config");
4const EventSource = require("eventsource");
5const auth_1 = require("./auth");
6const get_project_id_1 = require("./get-project-id");
7exports.streamLogs = (logType, prettyjson, filterBy, filterValue) => {
8 //console.log('Connecting to realtime log stream...')
9 const token = auth_1.auth.get('token');
10 const projectId = get_project_id_1.getProjectId();
11 const filters = filterBy && filterValue
12 ? `&filterBy=${filterBy}&filterValue=${filterValue}`
13 : '';
14 // if (filterBy && filterValue) {
15 // console.log()
16 // console.log(`Filtering ${filterBy} by ${filterValue}:`)
17 // console.log()
18 // }
19 const url = `${cli_config_1.REALTIME_LOGS_URL}?token=${token}&projectId=${projectId}&logType=${logType}${filters}`;
20 var source = new EventSource(url);
21 source.addEventListener('put', (message) => {
22 const logLines = JSON.parse(message.data);
23 if (logLines.path === '/' && message.data !== null && logLines.data) {
24 Object.values(logLines.data).forEach(line => {
25 if (prettyjson) {
26 console.log(JSON.stringify(line, null, 4));
27 console.log();
28 }
29 else
30 console.log(JSON.stringify(line));
31 });
32 }
33 else if (message.data !== null && logLines && logLines.data) {
34 if (prettyjson)
35 console.log(JSON.stringify(logLines.data, null, 4));
36 else
37 console.log(JSON.stringify(logLines.data));
38 }
39 });
40 source.onmessage = (e) => {
41 //console.log(e.data)
42 };
43 source.onerror = e => {
44 console.log(e);
45 process.exit();
46 };
47};