forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Dec 18, 2023
1 parent
ac3c11e
commit 62c043d
Showing
16 changed files
with
240 additions
and
214 deletions.
There are no files selected for viewing
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,75 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// PENDING: complete implementation of cloudWatchProvider | ||
import { Amplify } from '../../singleton/Amplify'; | ||
import { fetchAuthSession } from '../../index'; | ||
import { LoggerProvider, CloudWatchConfig, LogParams } from '../types'; | ||
import { checkLogLevel, getTimestamp, DEFAULT_LOG_LEVEL } from '../utils'; | ||
import { | ||
CloudWatchLogsClient, | ||
PutLogEventsCommand, | ||
} from '@aws-sdk/client-cloudwatch-logs'; | ||
// import { generateRandomString } from '@aws-amplify/core/internals/utils'; | ||
|
||
let cloudWatchConfig: CloudWatchConfig; | ||
const defaultConfig = { | ||
enable: true, | ||
localStoreMaxSizeInMB: 5, | ||
flushIntervalInSeconds: 60, | ||
loggingConstraints: { | ||
defaultLogLevel: DEFAULT_LOG_LEVEL, | ||
}, | ||
}; | ||
|
||
export const cloudWatchProvider: LoggerProvider<CloudWatchConfig> = { | ||
initialize: (config: CloudWatchConfig) => { | ||
if (cloudWatchConfig) | ||
throw new Error('CloudWatch provider already initialised'); | ||
cloudWatchConfig = { ...defaultConfig, ...config }; | ||
}, | ||
log: (input: LogParams) => { | ||
const { namespaces, logLevel, message } = input; | ||
const date = new Date(); | ||
const timestamp = getTimestamp(date); | ||
const prefix = `[${logLevel}] ${timestamp} ${namespaces.join(' - ')}`; | ||
const currentLevel = | ||
cloudWatchConfig.loggingConstraints?.defaultLogLevel ?? DEFAULT_LOG_LEVEL; | ||
|
||
if (checkLogLevel(logLevel, currentLevel)) | ||
putLogEvents(`${prefix} ${message}`, date.getTime()); | ||
}, | ||
flushLogs: function (): Promise<void> { | ||
return Promise.resolve(); // todo | ||
}, | ||
}; | ||
|
||
async function putLogEvents(message: string, timestamp: number) { | ||
try { | ||
const { logGroupName, region } = cloudWatchConfig; | ||
// todo: {mm-dd-yyyy}.{deviceId}.{userId|guest} | ||
// const logStreamName = generateRandomString(10); | ||
const logStreamName = 'central-logger-12-17-2023'; | ||
|
||
let session; | ||
try { | ||
session = await fetchAuthSession(); | ||
} catch (error) { | ||
return Promise.reject('No credentials'); | ||
} | ||
const client = new CloudWatchLogsClient({ | ||
region, | ||
credentials: session.credentials, | ||
}); | ||
|
||
const params = { | ||
logEvents: [{ message, timestamp }], | ||
logGroupName, | ||
logStreamName, | ||
}; | ||
|
||
await client.send(new PutLogEventsCommand(params)); | ||
} catch (error) { | ||
console.error('Error putting log events:', error); | ||
} | ||
} |
Oops, something went wrong.