Use winston for logging
This commit is contained in:
26
src/index.ts
26
src/index.ts
@ -3,8 +3,7 @@ import knexConfig from "./db";
|
||||
import { Sonar, gql } from "./sonar";
|
||||
import { SDK } from "@ringcentral/sdk";
|
||||
import { ticketize } from "./ticketize";
|
||||
|
||||
export const DEBUG = !!process.env.DEBUG;
|
||||
import { logger, DEBUG } from "./util";
|
||||
|
||||
function checkEnv() {
|
||||
[
|
||||
@ -60,7 +59,7 @@ async function initSonar() {
|
||||
}
|
||||
`
|
||||
);
|
||||
console.log(`Authenticated to Sonar as '${user.me.name}'.`);
|
||||
logger.info(`Authenticated to Sonar as '${user.me.name}'`);
|
||||
return sonar;
|
||||
}
|
||||
|
||||
@ -81,19 +80,19 @@ async function initRingCentralSDK() {
|
||||
if (DEBUG) {
|
||||
const client = sdk.client();
|
||||
client.on(client.events.beforeRequest, (req) => {
|
||||
console.log(req.url);
|
||||
logger.debug(req.url);
|
||||
});
|
||||
}
|
||||
|
||||
const platform = sdk.platform();
|
||||
platform.on(platform.events.refreshError, async (err) => {
|
||||
console.error("Refresh token error:", err);
|
||||
logger.error("Refresh token error:", err);
|
||||
await login();
|
||||
console.log("RingCentral re-authentication successful.");
|
||||
logger.info("RingCentral re-authentication successful");
|
||||
});
|
||||
|
||||
await login();
|
||||
console.log("Authenticated to RingCentral.");
|
||||
logger.info("Authenticated to RingCentral");
|
||||
|
||||
return sdk;
|
||||
}
|
||||
@ -102,7 +101,7 @@ async function initDB() {
|
||||
const db = knex(knexConfig);
|
||||
if (!process.env.DB_SKIP_MIGRATIONS) {
|
||||
await db.migrate.latest();
|
||||
console.log("Database migrations run successfully.");
|
||||
logger.info("Database migrations run successfully");
|
||||
}
|
||||
return db;
|
||||
}
|
||||
@ -115,12 +114,13 @@ async function main() {
|
||||
const rcsdk = await initRingCentralSDK();
|
||||
const db = await initDB();
|
||||
|
||||
console.log("Starting ticketizer...");
|
||||
logger.info("Starting");
|
||||
|
||||
const intervals = ticketize(sonar, rcsdk, db, getTicketizeConfig());
|
||||
|
||||
["SIGINT", "SIGTERM", "SIGQUIT"].forEach((sig) => {
|
||||
process.on(sig, async () => {
|
||||
console.log(`\nCaught ${sig}, shutting down...`);
|
||||
logger.info(`\nCaught ${sig}, shutting down...`);
|
||||
const results = await Promise.allSettled(
|
||||
intervals.map((interval) => interval.clear())
|
||||
);
|
||||
@ -128,15 +128,15 @@ async function main() {
|
||||
results.forEach((result) => {
|
||||
if (result.status === "rejected") {
|
||||
errors = true;
|
||||
console.error(result.reason);
|
||||
logger.error(result.reason);
|
||||
}
|
||||
});
|
||||
console.log("exiting now");
|
||||
await rcsdk.logout();
|
||||
process.exit(errors ? 1 : 0);
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import SDK from "@ringcentral/sdk";
|
||||
import path from "path";
|
||||
import { Knex } from "knex";
|
||||
import { getNationalNumber, setAsyncInterval } from "./util";
|
||||
import { logger, getNationalNumber, setAsyncInterval } from "./util";
|
||||
import { getTicketSubject, getTicketBody } from "./template";
|
||||
import { Sonar, gql } from "./sonar";
|
||||
import type {
|
||||
@ -320,7 +320,9 @@ export function ticketize(
|
||||
messages
|
||||
.filter((_, i) => !isStored[i])
|
||||
.map(async (message) => {
|
||||
console.log("Saving voicemail", message.id);
|
||||
logger.info(
|
||||
`New voicemail ${message.id} from ${message.from.phoneNumber} at ${message.creationTime}`
|
||||
);
|
||||
return storeVoicemail(
|
||||
extension,
|
||||
message,
|
||||
@ -338,6 +340,7 @@ export function ticketize(
|
||||
* @param firstRun whether this is the first run
|
||||
*/
|
||||
async function fetchAndStoreNewVoicemails(firstRun = false) {
|
||||
logger.info("Checking for new voicemails");
|
||||
const extensions = await getValidRCExtensions();
|
||||
return Promise.all(
|
||||
extensions.map((extension) =>
|
||||
@ -394,7 +397,7 @@ export function ticketize(
|
||||
voicemails.map(async (voicemail) => {
|
||||
const contact = await searchContactByPhoneNumber(voicemail.fromNumber);
|
||||
const ticketId = await createTicket(voicemail, contact);
|
||||
console.log(
|
||||
logger.info(
|
||||
`Created ticket ${ticketId} from voicemail ${voicemail.messageId}`
|
||||
);
|
||||
return updateStoredVoicemail({
|
||||
@ -408,9 +411,7 @@ export function ticketize(
|
||||
);
|
||||
}
|
||||
|
||||
function catchHandler(reason: any) {
|
||||
console.error(reason);
|
||||
}
|
||||
const catchHandler = logger.error;
|
||||
|
||||
let firstRun = true;
|
||||
return [
|
||||
|
14
src/util.ts
14
src/util.ts
@ -1,4 +1,18 @@
|
||||
import PhoneNumber from "awesome-phonenumber";
|
||||
import winston, { format } from "winston";
|
||||
|
||||
export const DEBUG = !!process.env.DEBUG;
|
||||
export const logger = winston.createLogger({
|
||||
level: DEBUG ? "debug" : process.env.LOG_LEVEL ?? "info",
|
||||
transports: [new winston.transports.Console()],
|
||||
format: format.combine(
|
||||
format.errors({ stack: true }),
|
||||
format.printf(
|
||||
({ level, message, stack }) =>
|
||||
`${level}: ${message}${stack ? "\n" + stack : ""}`
|
||||
)
|
||||
),
|
||||
});
|
||||
|
||||
export function getNationalNumber(input: string) {
|
||||
const number = new PhoneNumber(input);
|
||||
|
Reference in New Issue
Block a user