Skip to content

Commit

Permalink
Override Notion Client logger
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Nov 22, 2023
1 parent bb455e1 commit 880109c
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions connectors/src/connectors/notion/lib/notion_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isFullBlock,
isFullDatabase,
isFullPage,
LogLevel,
} from "@notionhq/client";
import {
BlockObjectResponse,
Expand All @@ -30,6 +31,14 @@ import mainLogger from "@connectors/logger/logger";

const logger = mainLogger.child({ provider: "notion" });

const notionClientLogger = (
level: LogLevel,
message: string,
extraInfo: Record<string, unknown>
) => {
console.log(`[${level}]: ${message}`, extraInfo);
};

/**
* @param notionAccessToken the access token to use to access the Notion API
* @param sinceTs a millisecond timestamp representing the minimum last edited time of
Expand Down Expand Up @@ -58,7 +67,10 @@ export async function getPagesAndDatabasesEditedSince(
}> {
const localLogger = logger.child(loggerArgs);

const notionClient = new Client({ auth: notionAccessToken });
const notionClient = new Client({
auth: notionAccessToken,
logger: notionClientLogger,
});
const editedPages: Record<string, number> = {};
const editedDbs: Record<string, number> = {};
let resultsPage: SearchResponse | null = null;
Expand Down Expand Up @@ -217,7 +229,10 @@ export async function getDatabaseChildPages({
}> {
const localLogger = logger.child(loggerArgs);

const notionClient = new Client({ auth: notionAccessToken });
const notionClient = new Client({
auth: notionAccessToken,
logger: notionClientLogger,
});
let resultsPage: QueryDatabaseResponse | null = null;
const pages: Record<string, number> = {};

Expand Down Expand Up @@ -294,7 +309,10 @@ export async function isAccessibleAndUnarchived(
objectType: "page" | "database",
localLogger?: Logger
): Promise<boolean> {
const notionClient = new Client({ auth: notionAccessToken });
const notionClient = new Client({
auth: notionAccessToken,
logger: notionClientLogger,
});
const maxTries = 5;
let tries = 0;

Expand Down Expand Up @@ -373,7 +391,10 @@ async function getBlockParent(
const max_depth = 8;
const max_transient_errors = 5;

const notionClient = new Client({ auth: notionAccessToken });
const notionClient = new Client({
auth: notionAccessToken,
logger: notionClientLogger,
});
let depth = 0;
let transient_errors = 0;

Expand Down Expand Up @@ -442,7 +463,10 @@ export async function getParsedDatabase(
): Promise<ParsedNotionDatabase | null> {
const localLogger = logger.child({ ...loggerArgs, databaseId });

const notionClient = new Client({ auth: notionAccessToken });
const notionClient = new Client({
auth: notionAccessToken,
logger: notionClientLogger,
});

let database: GetDatabaseResponse | null = null;

Expand Down Expand Up @@ -533,7 +557,10 @@ export async function retrievePage({
}): Promise<PageObjectResponse | null> {
const localLogger = logger.child({ ...loggerArgs, pageId });

const notionClient = new Client({ auth: accessToken });
const notionClient = new Client({
auth: accessToken,
logger: notionClientLogger,
});

let page: GetPageResponse | null = null;
try {
Expand Down Expand Up @@ -582,7 +609,10 @@ export async function retrieveBlockChildrenResultPage({
}): Promise<ListBlockChildrenResponse | null> {
const localLogger = logger.child(loggerArgs);

const notionClient = new Client({ auth: accessToken });
const notionClient = new Client({
auth: accessToken,
logger: notionClientLogger,
});

try {
localLogger.info(
Expand Down Expand Up @@ -668,7 +698,10 @@ export function getPageOrBlockParent(
}

export async function validateAccessToken(notionAccessToken: string) {
const notionClient = new Client({ auth: notionAccessToken });
const notionClient = new Client({
auth: notionAccessToken,
logger: notionClientLogger,
});
try {
await notionClient.search({ page_size: 1 });
} catch (e) {
Expand Down Expand Up @@ -768,7 +801,10 @@ export async function retrieveDatabaseChildrenResultPage({
}) {
const localLogger = logger.child({ ...loggerArgs, databaseId });

const notionClient = new Client({ auth: accessToken });
const notionClient = new Client({
auth: accessToken,
logger: notionClientLogger,
});

localLogger.info("Fetching database children result page from Notion API.");
try {
Expand Down Expand Up @@ -848,7 +884,10 @@ export async function getUserName(
return nameFromCache;
}

const notionClient = new Client({ auth: accessToken });
const notionClient = new Client({
auth: accessToken,
logger: notionClientLogger,
});

try {
pageLogger.info({ user_id: userId }, "Fetching user name from Notion API.");
Expand Down

0 comments on commit 880109c

Please sign in to comment.