-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(twitch-api): Add global file/console logger (#109)
- Loading branch information
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters