generated from AthennaIO/Template
-
-
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.
Merge pull request #92 from AthennaIO/develop
feat(driver): add lambda driver
- Loading branch information
Showing
13 changed files
with
200 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,37 @@ | ||
/** | ||
* @athenna/logger | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { debug } from '#src/debug' | ||
import type { Level } from '#src/types' | ||
import { Driver } from '#src/drivers/Driver' | ||
import { logger } from '#src/helpers/AwsLogger' | ||
|
||
export class LambdaDriver extends Driver { | ||
public transport(level: Level, message: any): any { | ||
if (!this.couldBeTransported(level)) { | ||
return | ||
} | ||
|
||
const levelMap = { | ||
info: 'info', | ||
warn: 'warn', | ||
trace: 'debug', | ||
debug: 'debug', | ||
error: 'error', | ||
success: 'info', | ||
fatal: 'critical' | ||
} | ||
|
||
const formatted = this.format(levelMap[level], message) | ||
|
||
debug('[%s] Transporting logs using AWS Powertools.', LambdaDriver.name) | ||
|
||
return logger[levelMap[level]](formatted) | ||
} | ||
} |
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,18 @@ | ||
/** | ||
* @athenna/logger | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Logger } from '@aws-lambda-powertools/logger' | ||
|
||
/** | ||
* Expose the AWS logger to be able to make changes on it. | ||
* By default the log level is set to `debug` because the | ||
* responsible of deciding if the message should be logged | ||
* or not is from Athenna. | ||
*/ | ||
export const logger = new Logger({ logLevel: 'debug' }) |
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,17 @@ | ||
/** | ||
* @athenna/logger | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
export type Level = | ||
| 'trace' | ||
| 'debug' | ||
| 'info' | ||
| 'success' | ||
| 'warn' | ||
| 'error' | ||
| 'fatal' |
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,10 @@ | ||
/** | ||
* @athenna/logger | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
export * from '#src/types/Level' |
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,31 @@ | ||
/** | ||
* @athenna/logger | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Config } from '@athenna/config' | ||
import { Log, LoggerProvider } from '#src' | ||
import { Folder, Path } from '@athenna/common' | ||
|
||
await new Folder(Path.fixtures('config')).copy(Path.config()) | ||
await Config.safeLoad(Path.config('logging.ts')) | ||
|
||
new LoggerProvider().register() | ||
|
||
const simpleLogger = Log.channel('discard', 'lambda') | ||
const noneLogger = Log.config({ formatter: 'none' }).channel('discard', 'lambda') | ||
|
||
simpleLogger.trace('hello') | ||
simpleLogger.debug('%s', 'hello') | ||
simpleLogger.info('hello') | ||
noneLogger.success('%s', 'hello') | ||
noneLogger.warn('({yellow,notFound,bold} hello) hello') | ||
noneLogger.error('%s', 'hello') | ||
noneLogger.fatal('hello') | ||
|
||
await Folder.safeRemove(Path.config()) | ||
await Folder.safeRemove(Path.storage()) |
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,27 @@ | ||
/** | ||
* @athenna/logger | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Path, Exec } from '@athenna/common' | ||
import { Test, type Context } from '@athenna/test' | ||
|
||
export default class LambdaDriverTest { | ||
@Test() | ||
public async shouldBeAbleToLogInConsoleWithLambdaLib({ assert }: Context) { | ||
const { stdout, stderr } = await Exec.node(Path.fixtures('transporters/consoleLambda.ts')) | ||
|
||
const logs = [...stdout.split('\n').filter(l => l !== ''), ...stderr.split('\n').filter(l => l !== '')].filter( | ||
v => !v.startsWith('(') | ||
) | ||
|
||
logs.forEach(log => { | ||
assert.isFalse(log.includes('DEBUG')) | ||
assert.isTrue(log.includes('hello')) | ||
}) | ||
} | ||
} |
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