Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
misenhower committed Sep 10, 2022
1 parent 400347e commit 231681b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
12 changes: 12 additions & 0 deletions app/common/prefixedConsole.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions app/index.mjs
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
10 changes: 6 additions & 4 deletions app/splatnet/NsoClient.mjs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,6 +18,7 @@ export default class NsoClient
constructor(nintendoToken = null) {
initializeNxapi();

this.console = prefixedConsole('NSO');
this.nintendoToken = nintendoToken || process.env.NINTENDO_TOKEN;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions app/splatnet/SplatNet3Client.mjs
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,6 +11,7 @@ export default class SplatNet3Client
bulletToken = null;

constructor(nsoClient = null) {
this.console = prefixedConsole('SplatNet');
this.nsoClient = nsoClient ?? new NsoClient;
}

Expand Down Expand Up @@ -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',
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/splatnet/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 35 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 231681b

Please sign in to comment.