Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(twitch-api): Add global file/console logger #109

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/server/libs/twitch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { ApiClient } = require("twitch");
const AuthProvider = require("./AuthProvider");
const { ChatClient } = require("twitch-chat-client");

require("./logger-hook");

const api = {
config,
authProvider: null,
Expand Down
61 changes: 61 additions & 0 deletions app/server/libs/twitch/logger-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { LogLevel, Logger } = require("@d-fischer/logger");
const { watch, logsPath } = require("../../../utils");
const chalk = require("chalk");
const fs = require("fs-extra");
const path = require("path");

let locked = false;
let queue = [];

const maxLines = 500;
const logFile = path.join(logsPath, "twitch.log");
const minLogLevel = watch ? LogLevel.DEBUG : LogLevel.WARNING;
const colors = [chalk.red, chalk.red, chalk.orange, chalk.blue, chalk.white];

fs.ensureFileSync(logFile);

function getLogs() {
const text = fs.readFileSync(logFile);
return text.toString().split("\n");
}

function writeLogs(logs) {
if (locked) {
queue.unshift(logs);
return;
}

locked = true;

fs.writeFileSync(logFile, logs.slice(0, maxLines).join("\n"));

if (queue.length) {
fs.writeFileSync(
logFile,
[...queue, ...logs].slice(0, maxLines).join("\n")
);
queue = [];
}

locked = false;
}

function push(line) {
writeLogs([line, ...getLogs()]);
}

Logger.prototype.log = function (level, message) {
if (level > minLogLevel) return;

const levelName = LogLevel[level];
const date = new Date().toISOString();
const line = `[${date}] [${levelName}] [${this._name}] ${message}`;

push(line);

if (watch && level < LogLevel.INFO) {
const color = colors[level] || colors[colors.length - 1];
// eslint-disable-next-line no-console
console.log(color(line));
}
};
21 changes: 19 additions & 2 deletions app/server/libs/twitch/pubsub/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
const { PubSubClient } = require("twitch-pubsub-client");
const { BasicPubSubClient, PubSubClient } = require("twitch-pubsub-client");
const onRedemption = require("./onRedemption");
const onBits = require("./onBits");
const twitch = require("../index");

const pubSubClient = new PubSubClient();
const reconnectDelay = 5;

const rootClient = new BasicPubSubClient();
const pubSubClient = new PubSubClient(rootClient);

function reconnect() {
console.log(`Reconnecting...`);
pubSubClient._rootClient.reconnect();
}

module.exports = async function init() {
const userId = await pubSubClient.registerUserListener(twitch.api);

await pubSubClient.onRedemption(userId, onRedemption);
await pubSubClient.onBits(userId, onBits);

pubSubClient._rootClient.onDisconnect((manually, reason) => {
// if (manually) return;
console.log("PUBSUB ERROR >>>", reason);
console.log(`Reconnecting in ${reconnectDelay} sec.`);
setTimeout(reconnect, reconnectDelay * 1000);
});

setTimeout(() => pubSubClient._rootClient.disconnect(), 5000);
};
7 changes: 6 additions & 1 deletion app/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ if (watch) {

const clientPath = path.join(appPath, "client");
const staticPath = path.join(appPath, "static");

const logsPath = path.join(userPath, "logs");
const uploadPath = path.join(userPath, "upload");
const storesPath = path.join(userPath, "stores");
const filesPath = path.join(uploadPath, "files");
const databasePath = path.join(userPath, "database");

const filesPath = path.join(uploadPath, "files");

const databaseFilename = "marv.sqlite";

const isFirstStart = !fs.existsSync(path.join(databasePath, databaseFilename));
Expand All @@ -30,6 +34,7 @@ module.exports = {
watch,
appPath,
userPath,
logsPath,
filesPath,
uploadPath,
storesPath,
Expand Down