diff --git a/package-lock.json b/package-lock.json index d302dc7..edfc882 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "cron": "^2.4.3", "express": "^4.18.2", + "loglevel": "^1.8.1", "prom-client": "^14.2.0", "source-map-support": "^0.5.21", "ynab": "^1.32.0" @@ -1927,6 +1928,18 @@ "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", "dev": true }, + "node_modules/loglevel": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/loglevel/-/loglevel-1.8.1.tgz", + "integrity": "sha512-tCRIJM51SHjAayKwC+QAg8hT8vg6z7GSgLJKGvzuPb1Wc+hLzqtuVLxp6/HzSPOozuK+8ErAhy7U/sVzw8Dgfg==", + "engines": { + "node": ">= 0.6.0" + }, + "funding": { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/loglevel" + } + }, "node_modules/lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", diff --git a/package.json b/package.json index 45687a2..7bb184e 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "cron": "^2.4.3", "express": "^4.18.2", + "loglevel": "^1.8.1", "prom-client": "^14.2.0", "source-map-support": "^0.5.21", "ynab": "^1.32.0" diff --git a/src/collectors.ts b/src/collectors.ts index 0697b76..6cb8b02 100644 --- a/src/collectors.ts +++ b/src/collectors.ts @@ -1,3 +1,4 @@ +import log from 'loglevel'; import {Gauge, Registry} from "prom-client"; import {Account} from "ynab"; @@ -5,7 +6,7 @@ export class YNABCollector { accountBalances: Account[] = []; public async collectAccountBalanceMetrics(register: Registry) { - console.log('Collecting account balance metrics..'); + log.debug('Collecting account balance metrics..'); const accountLabels = ['account_name', 'type', 'closed']; const accountClearedBalanceGauge = new Gauge({ @@ -14,7 +15,7 @@ export class YNABCollector { help: "Account Cleared Balance amounts", labelNames: accountLabels, collect: async () => { - console.debug(`Collecting Cleared Balance for ${this.accountBalances.length} accounts`); + log.debug(`Collecting Cleared Balance for ${this.accountBalances.length} accounts`); this.accountBalances.forEach(a => { accountClearedBalanceGauge.labels({account_name: a.name, type: a.type, closed: String(a.closed)}).set(a.cleared_balance / 1000); }); @@ -27,14 +28,11 @@ export class YNABCollector { registers: [register], labelNames: accountLabels, collect: async () => { - console.debug(`Collecting Uncleared Balance for ${this.accountBalances.length} accounts`); + log.debug(`Collecting Uncleared Balance for ${this.accountBalances.length} accounts`); this.accountBalances.forEach(a => { accountUnClearedBalanceGauge.labels({account_name: a.name, type: a.type, closed: String(a.closed)}).set(a.uncleared_balance / 1000); }); } - }); - } - } diff --git a/src/index.ts b/src/index.ts index c1d9417..0c1191f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,8 +7,8 @@ import {Registry} from "prom-client"; import 'source-map-support/register'; import {YnabAPI} from "./api"; import {YNABCollector} from "./collectors"; -import {scheduledAccountBalanceUpdate} from "./jobs/accounts";; - +import {scheduledAccountBalanceUpdate} from "./jobs/accounts"; +import log, {LogLevelDesc} from 'loglevel'; async function main() { const ynab = new YnabAPI(); @@ -22,7 +22,7 @@ async function main() { cronTime: "*/15 * * * *", onTick: async () => { ynabCollector.accountBalances = (await scheduledAccountBalanceUpdate(ynab)).accounts; - console.log(`${ynabCollector.accountBalances.length} accounts refreshed`); + log.info(`${ynabCollector.accountBalances.length} accounts refreshed`); }, start: true, runOnInit: true @@ -35,17 +35,19 @@ async function main() { app.get('/metrics', async (req: Request, res: Response) => { res.setHeader('Content-Type', register.contentType); - console.debug('getting metrics'); + log.debug('getting metrics'); const results = await register.metrics(); res.send(results); }); app.listen(port, () => { - console.log(`🔊 Publishing metrics on port ${port}`); + log.info(`🔊 Publishing metrics on port ${port}`); }); } if (require.main === module) { - console.log('Starting YNAB Exporter 💰💸'); + const logLevel = (process.env.LOG_LEVEL) as LogLevelDesc || 'info'; + log.setLevel(logLevel); + log.info('Starting YNAB Exporter 💰💸'); main(); } diff --git a/src/jobs/accounts.ts b/src/jobs/accounts.ts index 4922708..25c9ff0 100644 --- a/src/jobs/accounts.ts +++ b/src/jobs/accounts.ts @@ -1,12 +1,13 @@ import {AccountsResponseData, TransactionsResponse} from "ynab"; import {YnabAPI} from "../api"; +import log from "loglevel"; export type ynabTransactionResponse = (TransactionsResponse & {rateLimit: string | null;}) | undefined; export async function scheduledAccountBalanceUpdate(ynab: YnabAPI): Promise { - console.log(`Starting scheduled account balance update at ${new Date().toLocaleString()} ...`); + log.info(`Starting scheduled account balance update at ${new Date().toLocaleString()} ...`); const accounts = await ynab.client.accounts.getAccounts(ynab.budgetId); - console.log(`Fetched balances for ${accounts.data.accounts.length} accounts.`); - console.log(`Rate limit: ${accounts.rateLimit}`); + log.info(`Fetched balances for ${accounts.data.accounts.length} accounts.`); + log.info(`Rate limit: ${accounts.rateLimit}`); return accounts.data; } \ No newline at end of file