Skip to content

Commit

Permalink
feat(logging): add isLoggable util
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Kumar committed Feb 7, 2024
1 parent 4840d0a commit dfc2d2c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/logging/src/providers/cloudwatch/utils/isLoggable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { fetchAuthSession } from '@aws-amplify/core';

export const isLoggable = async (
cloudWatchConfig: CloudWatchConfig,
): Promise<LogLevel> => {
const { loggingConstraints } = cloudWatchConfig;
const { defaultLogLevel, categoryLogLevel } =
await getLoggingConstraint(loggingConstraints);

let resolvedCategoryLogLevel;
if (!!categoryLogLevel) {
resolvedCategoryLogLevel = getCategoryLogLevel(log, categoryLogLevel);
}

return resolvedCategoryLogLevel ?? defaultLogLevel;
};

const getLoggingConstraint = async (
loggingConstraints: LoggingConstraints,
): Promise<LoggingConstraint> => {
const { defaultLogLevel, categoryLogLevel, userLogLevel } =
loggingConstraints;

// const { userSub } = await fetchAuthSession();
const userSub = 'userSub1';

if (!!userLogLevel?.[userSub]) {
return {
defaultLogLevel: userLogLevel[userSub].defaultLogLevel,
categoryLogLevel: userLogLevel[userSub]?.categoryLogLevel,
};
}

return {
defaultLogLevel,
categoryLogLevel,
};
};

const getCategoryLogLevel = (
log: LogParams,
categoryLogLevel: CategoryLogLevel,
): LogLevel | undefined => {
if (!log.category) {
return undefined;
}

const matchedKey = Object.keys(categoryLogLevel).find(
key => key.toLowerCase() === log.category.toLowerCase(),
);
return matchedKey ? categoryLogLevel[matchedKey] : undefined;
};

0 comments on commit dfc2d2c

Please sign in to comment.