Improve logging

This commit is contained in:
Matt Isenhower
2022-09-10 12:07:33 -07:00
parent 400347e277
commit 231681bdac
7 changed files with 61 additions and 11 deletions

View File

@@ -0,0 +1,12 @@
import { Console } from 'node:console';
import consoleStamp from 'console-stamp';
export default function prefixedConsole(...prefixes) {
let result = new Console({ stdout: process.stdout, stderr: process.stderr });
let prefix = prefixes.map(s => `[${s}]`).join(' ');
let format = `:date(dd.mm.yyyy HH:MM:ss.l) :label(7) ${prefix}`.trim();
consoleStamp(result, { format });
return result;
}

View File

@@ -1,7 +1,9 @@
import dotenv from 'dotenv';
import consoleStamp from 'console-stamp';
import cron from './cron.mjs';
import { sendTweets } from './twitter/index.mjs';
consoleStamp(console);
dotenv.config();
const actions = {

View File

@@ -1,6 +1,7 @@
import CoralApi from 'nxapi/coral';
import { addUserAgent } from "nxapi";
import ValueCache from '../common/ValueCache.mjs';
import prefixedConsole from '../common/prefixedConsole.mjs';
let _nxapiInitialized = false;
@@ -17,6 +18,7 @@ export default class NsoClient
constructor(nintendoToken = null) {
initializeNxapi();
this.console = prefixedConsole('NSO');
this.nintendoToken = nintendoToken || process.env.NINTENDO_TOKEN;
}
@@ -52,11 +54,11 @@ export default class NsoClient
}
async _createCoralSession() {
console.debug('Creating Coral session');
this.console.info('Creating Coral session...');
let { data } = await CoralApi.createWithSessionToken(this.nintendoToken);
let expires = this._calculateCacheExpiry(data.credential.expiresIn);
console.debug(`Caching Coral session until: ${expires}`);
this.console.debug(`Caching Coral session until: ${expires}`);
await this._getCoralCache().setData(data, expires);
return data;
@@ -84,11 +86,11 @@ export default class NsoClient
async _createWebServiceToken(id, tokenCache) {
let coral = await this.getCoralApi();
console.debug(`Creating web service token for ID ${id}`);
this.console.info(`Creating web service token for ID ${id}...`);
let { result } = await coral.getWebServiceToken(id);
let expires = this._calculateCacheExpiry(result.expiresIn);
console.debug(`Caching web service token for ID ${id} until: ${expires}`);
this.console.debug(`Caching web service token for ID ${id} until: ${expires}`);
await tokenCache.setData(result, expires);
return result;

View File

@@ -1,5 +1,6 @@
import ValueCache from "../common/ValueCache.mjs";
import NsoClient from "./NsoClient.mjs";
import prefixedConsole from '../common/prefixedConsole.mjs';
export const SPLATNET3_WEB_SERVICE_ID = '4834290508791808';
@@ -10,6 +11,7 @@ export default class SplatNet3Client
bulletToken = null;
constructor(nsoClient = null) {
this.console = prefixedConsole('SplatNet');
this.nsoClient = nsoClient ?? new NsoClient;
}
@@ -52,7 +54,7 @@ export default class SplatNet3Client
}
async _createBulletToken(webServiceToken, bulletTokenCache) {
console.debug('Creating bullet token');
this.console.info('Creating bullet token...');
let response = await fetch(this.baseUrl + '/api/bullet_tokens', {
method: 'POST',
@@ -73,7 +75,7 @@ export default class SplatNet3Client
let expiry = this._calculateCacheExpiry(7200);
await bulletTokenCache.setData(bulletToken, expiry);
console.debug(`Caching bullet token until: ${expiry}`);
this.console.debug(`Caching bullet token until: ${expiry}`);
return bulletToken;
}

View File

@@ -8,7 +8,7 @@ import SplatNet3Client, { SPLATNET3_WEB_SERVICE_ID } from "./SplatNet3Client.mjs
* even if e.g. the imink API has intermittent downtime.
*/
export async function warmCache() {
console.debug('Warming caches...');
console.info('Warming caches...');
let nso = new NsoClient;
let splatnet = new SplatNet3Client;

39
package-lock.json generated
View File

@@ -8,6 +8,7 @@
"name": "splatoon3.ink",
"version": "0.0.0",
"dependencies": {
"console-stamp": "^3.0.6",
"cron": "^2.1.0",
"dotenv": "^16.0.2",
"ecstatic": "^4.1.4",
@@ -710,7 +711,6 @@
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dev": true,
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
@@ -824,6 +824,18 @@
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
},
"node_modules/console-stamp": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/console-stamp/-/console-stamp-3.0.6.tgz",
"integrity": "sha512-j4tP+1shVIUjSnvrtv5nJ5uVzLeNOTweVHkcEXB2ej4NJdlRp14w0hOzQiF+iQvOTjz4jafmdhd1CdYSwNzM8Q==",
"dependencies": {
"chalk": "^4.1.2",
"dateformat": "^4.6.3"
},
"engines": {
"node": ">=10"
}
},
"node_modules/content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
@@ -911,6 +923,14 @@
"node": ">= 12"
}
},
"node_modules/dateformat": {
"version": "4.6.3",
"resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.6.3.tgz",
"integrity": "sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==",
"engines": {
"node": "*"
}
},
"node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@@ -3902,7 +3922,6 @@
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true,
"dependencies": {
"has-flag": "^4.0.0"
},
@@ -4912,7 +4931,6 @@
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dev": true,
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
@@ -4996,6 +5014,15 @@
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
},
"console-stamp": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/console-stamp/-/console-stamp-3.0.6.tgz",
"integrity": "sha512-j4tP+1shVIUjSnvrtv5nJ5uVzLeNOTweVHkcEXB2ej4NJdlRp14w0hOzQiF+iQvOTjz4jafmdhd1CdYSwNzM8Q==",
"requires": {
"chalk": "^4.1.2",
"dateformat": "^4.6.3"
}
},
"content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
@@ -5062,6 +5089,11 @@
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz",
"integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA=="
},
"dateformat": {
"version": "4.6.3",
"resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.6.3.tgz",
"integrity": "sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA=="
},
"debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@@ -7069,7 +7101,6 @@
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true,
"requires": {
"has-flag": "^4.0.0"
}

View File

@@ -10,6 +10,7 @@
"twitter": "node app/index.mjs twitter"
},
"dependencies": {
"console-stamp": "^3.0.6",
"cron": "^2.1.0",
"dotenv": "^16.0.2",
"ecstatic": "^4.1.4",