-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ashwin Kumar
committed
Feb 7, 2024
1 parent
4840d0a
commit dfc2d2c
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/logging/src/providers/cloudwatch/utils/isLoggable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |