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 5, 2023
1 parent
3340588
commit 86c3b13
Showing
15 changed files
with
380 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// PENDING: logger configs to be taken from singleton | ||
// PENDING: log filtering | ||
import { Amplify } from '../singleton/Amplify'; | ||
import { LogCallInputs, LogLevel } from './types'; | ||
|
||
const logLevelIndex = ['VERBOSE', 'DEBUG', 'INFO', 'WARN', 'ERROR']; | ||
let logLevel: LogLevel = 'INFO'; | ||
|
||
export const log = (...params: LogCallInputs) => { | ||
const loggers = Amplify.libraryOptions.Logger; | ||
if (loggers) Object.values(loggers).forEach(logger => logger.log(...params)); | ||
}; | ||
|
||
export const setLogLevel = (level: LogLevel) => { | ||
logLevel = level; | ||
}; | ||
|
||
export const getLogLevel = (): LogLevel => { | ||
if (typeof (<any>window) !== 'undefined' && (<any>window).LOG_LEVEL) { | ||
const windowLog = (<any>window).LOG_LEVEL; | ||
if (logLevelIndex.includes(windowLog)) return windowLog; | ||
} | ||
return logLevel; | ||
}; | ||
|
||
export const checkLogLevel = ( | ||
level: LogLevel, | ||
setLevel: LogLevel | undefined = undefined | ||
): boolean => { | ||
const targetLevel = setLevel ?? getLogLevel(); | ||
return logLevelIndex.indexOf(level) >= logLevelIndex.indexOf(targetLevel); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { ConsoleLogger } from './ConsoleLogger'; | ||
export { ConsoleLogger } from './ConsoleLogger'; // legacy | ||
export { generateExternalLogger, generateInternalLogger } from './logger'; | ||
export { logger as CloudWatchProvider } from './providers/CloudWatchProvider'; | ||
export { logger as ConsoleProvider } from './providers/ConsoleProvider'; |
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,83 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { LogLevel } from './types'; | ||
import { log as centralizedLog } from './AdministrateLogger'; | ||
import { AmplifyLoggingCategories } from '../types'; | ||
|
||
/** | ||
* Write logs | ||
* @class Logger | ||
**/ | ||
class Logger { | ||
namespaces: string[]; | ||
|
||
constructor(namespaces: string[]) { | ||
this.namespaces = namespaces; | ||
} | ||
|
||
/** | ||
* Write INFO log | ||
* @method | ||
* @memeberof Logger | ||
* @param {string|object} msg - Logging message or object | ||
*/ | ||
info(message: string, ...objects: any) { | ||
this._log('INFO', message, objects); | ||
} | ||
|
||
/** | ||
* Write WARN log | ||
* @method | ||
* @memeberof Logger | ||
* @param {string|object} msg - Logging message or object | ||
*/ | ||
warn(message: string, ...objects: any) { | ||
this._log('WARN', message, objects); | ||
} | ||
|
||
/** | ||
* Write ERROR log | ||
* @method | ||
* @memeberof Logger | ||
* @param {string|object} msg - Logging message or object | ||
*/ | ||
error(message: string, ...objects: any) { | ||
this._log('ERROR', message, objects); | ||
} | ||
|
||
/** | ||
* Write DEBUG log | ||
* @method | ||
* @memeberof Logger | ||
* @param {string|object} msg - Logging message or object | ||
*/ | ||
debug(message: string, ...objects: any) { | ||
this._log('DEBUG', message, objects); | ||
} | ||
|
||
/** | ||
* Write VERBOSE log | ||
* @method | ||
* @memeberof Logger | ||
* @param {string|object} msg - Logging message or object | ||
*/ | ||
verbose(message: string, ...objects: any) { | ||
this._log('VERBOSE', message, objects); | ||
} | ||
|
||
_log(logLevel: LogLevel, message: string, ...objects: any) { | ||
centralizedLog(this.namespaces, logLevel, message, objects); | ||
} | ||
} | ||
|
||
export const generateInternalLogger = ( | ||
forCategory: AmplifyLoggingCategories, | ||
...forNamespace: string[] | ||
) => { | ||
return new Logger([forCategory, ...forNamespace]); | ||
}; | ||
|
||
export const generateExternalLogger = (...forNamespace: string[]) => { | ||
return new Logger(forNamespace); | ||
}; |
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,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// PENDING: complete implementation of CloudWatchProvider | ||
import { fetchAuthSession } from '../../index'; | ||
import { | ||
CloudWatchLogsClient, | ||
PutLogEventsCommand, | ||
} from '@aws-sdk/client-cloudwatch-logs'; | ||
import { checkLogLevel } from '../AdministrateLogger'; | ||
import { LogLevel, Logger } from '../types'; | ||
|
||
const logGroupName = 'console-logger-poc-12-01'; | ||
const logStreamName = 'stream-12-01'; | ||
const REGION = 'us-west-2'; | ||
|
||
export const logger: Logger = { | ||
log: ( | ||
namespaces: string[], | ||
logLevel: LogLevel, | ||
message: string, | ||
objects: object[] | ||
) => { | ||
const timestamp = getTimestamp(); | ||
const prefix = `[${logLevel}] ${timestamp} ${namespaces.join(' - ')}`; | ||
|
||
// todo: add timestamp, object | ||
if (checkLogLevel(logLevel)) | ||
putLogEvents({ message: `${prefix} ${message}` }); | ||
}, | ||
}; | ||
|
||
async function putLogEvents({ | ||
message, | ||
timestamp, | ||
}: { | ||
message: string; | ||
timestamp?: number; | ||
}) { | ||
try { | ||
let session; | ||
try { | ||
session = await fetchAuthSession(); | ||
} catch (error) { | ||
return Promise.reject('No credentials'); | ||
} | ||
const client = new CloudWatchLogsClient({ | ||
region: REGION, | ||
credentials: session.credentials, | ||
}); | ||
|
||
const timestamp = new Date().getTime(); | ||
const params = { | ||
logEvents: [{ message, timestamp }], | ||
logGroupName, | ||
logStreamName, | ||
}; | ||
|
||
const data = await client.send(new PutLogEventsCommand(params)); | ||
} catch (error) { | ||
console.error('Error putting log events:', error); | ||
} | ||
} | ||
|
||
const getTimestamp = () => { | ||
const dt = new Date(); | ||
return ( | ||
[padding(dt.getMinutes()), padding(dt.getSeconds())].join(':') + | ||
'.' + | ||
dt.getMilliseconds() | ||
); | ||
}; | ||
|
||
const padding = (n: number) => { | ||
return n < 10 ? '0' + n : '' + n; | ||
}; |
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,54 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// PENDING: complete implementation of ConsoleProvider | ||
import { checkLogLevel } from '../AdministrateLogger'; | ||
import { LogLevel, Logger } from '../types'; | ||
export const logger: Logger = { | ||
log: ( | ||
namespaces: string[], | ||
logLevel: LogLevel, | ||
message: string, | ||
objects: object[] | ||
) => { | ||
const logFcn = getConsoleLogFcn(logLevel); | ||
const prefix = `[${logLevel}] ${timestamp()} ${namespaces.join(' - ')}`; | ||
if (checkLogLevel(logLevel)) logFcn(prefix, message, ...objects); | ||
}, | ||
}; | ||
|
||
const getConsoleLogFcn = (logLevel: LogLevel) => { | ||
let fcn = console.log.bind(console); | ||
switch (logLevel) { | ||
case 'DEBUG': { | ||
fcn = console.debug?.bind(console) ?? fcn; | ||
break; | ||
} | ||
case 'ERROR': { | ||
fcn = console.error?.bind(console) ?? fcn; | ||
break; | ||
} | ||
case 'INFO': { | ||
fcn = console.info?.bind(console) ?? fcn; | ||
break; | ||
} | ||
case 'WARN': { | ||
fcn = console.warn?.bind(console) ?? fcn; | ||
break; | ||
} | ||
} | ||
return fcn; | ||
}; | ||
|
||
const padding = (n: number) => { | ||
return n < 10 ? '0' + n : '' + n; | ||
}; | ||
|
||
const timestamp = () => { | ||
const dt = new Date(); | ||
return ( | ||
[padding(dt.getMinutes()), padding(dt.getSeconds())].join(':') + | ||
'.' + | ||
dt.getMilliseconds() | ||
); | ||
}; |
Oops, something went wrong.