From b93e4d6372a4f433f249e1389d5f6c147abf743b Mon Sep 17 00:00:00 2001 From: jgaribsin <16392336+jgaribsin@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:26:08 -0700 Subject: [PATCH] #patch added newrelic logging --- src/api-wrapper/api.ts | 6 ++++-- src/events/onInteraction.ts | 16 +++++++++++++--- src/events/onReady.ts | 22 ++++++++++++++++------ src/index.ts | 12 +++++++++--- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/api-wrapper/api.ts b/src/api-wrapper/api.ts index f6c04d2..da88c3f 100644 --- a/src/api-wrapper/api.ts +++ b/src/api-wrapper/api.ts @@ -15,7 +15,7 @@ import { getSectorName, } from './mapping'; import {existsSync, mkdirSync, writeFileSync} from 'fs'; -import {compressFile, dayjs, writeGzipJson} from '../handlers'; +import {compressFile, dayjs, logger, writeGzipJson} from '../handlers'; import path from 'path'; import {getAllPlanets} from './planets'; import axios from 'axios'; @@ -68,7 +68,9 @@ for (const season of seasons.seasons) { export async function getData() { const season = seasons.current; const fileTimestamp = dayjs().format('DD_MM_YYYYTHH_mm_ssZZ[UTC]'); - console.log(`Fetching data for season ${season} at ${fileTimestamp}`); + logger.info(`Fetching data for season ${season} at ${fileTimestamp}`, { + type: 'info', + }); if (!existsSync(path.join('api_responses', String(season)))) mkdirSync(path.join('api_responses', String(season)), {recursive: true}); diff --git a/src/events/onInteraction.ts b/src/events/onInteraction.ts index e448831..4fa4c26 100644 --- a/src/events/onInteraction.ts +++ b/src/events/onInteraction.ts @@ -7,7 +7,12 @@ import { ownerCmds, planetAutoCmds, } from '../commands'; -import {checkPerms, commandErrorEmbed, ownerCommandEmbed} from '../handlers'; +import { + checkPerms, + commandErrorEmbed, + logger, + ownerCommandEmbed, +} from '../handlers'; import {go as search} from 'fuzzysort'; import {mappedNames} from '../api-wrapper'; @@ -75,7 +80,12 @@ const onInteraction = async (interaction: Interaction) => { await commandHash[command](interaction); const time = `${Date.now() - start}ms`; - console.log(`Executed command /${command} in ${time}`); + logger.info(`Executed command /${command} in ${time}`, { + time, + command, + type: 'command', + user: interaction.user.tag, + }); return; } catch (err) { @@ -84,7 +94,7 @@ const onInteraction = async (interaction: Interaction) => { // TODO: handle other error types explicitly eg. discordjs // Log error and additional info for debugging - console.log(error.message, { + logger.info(error.message, { command: interaction.commandName, args: interaction.options.data, user: interaction.user.tag, diff --git a/src/events/onReady.ts b/src/events/onReady.ts index 5f32b77..ca19c4d 100644 --- a/src/events/onReady.ts +++ b/src/events/onReady.ts @@ -11,7 +11,7 @@ import {commandHash, commandList, presenceCmds} from '../commands'; import {config, isProd} from '../config'; import {getData, mappedNames} from '../api-wrapper'; import {db, eq, persistentMessages} from '../db'; -import {warStatusEmbeds} from '../handlers'; +import {logger, warStatusEmbeds} from '../handlers'; // bot client token, for use with discord API const BOT_TOKEN = config.BOT_TOKEN; @@ -23,7 +23,9 @@ const onReady = async (client: Client) => { const rest = new REST().setToken(BOT_TOKEN); - console.log(`Serving ${serverCount} servers as ${client.user?.tag}`); + logger.info(`Serving ${serverCount} servers as ${client.user?.tag}`, { + type: 'startup', + }); // two non-constant value for timing functions let start = Date.now(); @@ -34,10 +36,14 @@ const onReady = async (client: Client) => { await rest.put(Routes.applicationCommands(clientId), { body: commandData, }); - console.log(`Commands loaded: ${Object.keys(commandHash).join(', ')}`); + logger.info(`Commands loaded: ${Object.keys(commandHash).join(', ')}`, { + type: 'startup', + }); time = `${Date.now() - start}ms`; - console.log(`Loaded ${commandData.length} commands in ${time}`); + logger.info(`Loaded ${commandData.length} commands in ${time}`, { + type: 'startup', + }); // get api data on startup await getData().then(data => { @@ -47,7 +53,9 @@ const onReady = async (client: Client) => { // retrieve encounters and load them as autocomplete suggestions time = `${Date.now() - start}ms`; - console.log(`Loaded ${mappedNames.planets.length} planets in ${time}`); + logger.info(`Loaded ${mappedNames.planets.length} planets in ${time}`, { + type: 'startup', + }); // cron schedule to update messages every hour schedule('0 * * * *', async () => { @@ -127,7 +135,9 @@ const onReady = async (client: Client) => { // await all message edits completion await Promise.all(promises); time = `${Date.now() - start}ms`; - console.log(`Updated ${messages.length} messages in ${time}`); + logger.info(`Updated ${messages.length} messages in ${time}`, { + type: 'info', + }); }); // cron schedule to update api data every 10 seconds diff --git a/src/index.ts b/src/index.ts index 34aeae3..3e5ac4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import {config} from './config'; import {onInteraction, onReady} from './events'; -import {client} from './handlers'; +import {client, logger} from './handlers'; const {BOT_TOKEN} = config; @@ -13,13 +13,19 @@ async function main() { if (guild.available) { // retrieve new server count const serverCount = (await client.guilds.fetch()).size; - console.log(`Client joined guild #${serverCount}: ${guild.name}`); + logger.info(`Client joined guild #${serverCount}: ${guild.name}`), + { + type: 'info', + }; } }); // log server kick client.on('guildDelete', guild => { - if (guild.available) console.log(`Client removed from: ${guild.name}`); + if (guild.available) + logger.info(`Client removed from: ${guild.name}`, { + type: 'info', + }); }); // handle user interactions (eg. commands)