Skip to content

Commit

Permalink
Merge pull request #92 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(driver): add lambda driver
  • Loading branch information
jlenon7 authored Feb 10, 2024
2 parents 14bb7db + aabc812 commit 5ccb616
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 24 deletions.
54 changes: 38 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "4.15.0",
"version": "4.16.0",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -61,13 +61,14 @@
"#tests": "./tests/index.js"
},
"dependencies": {
"telegraf": "^4.15.3",
"cls-rtracer": "^2.6.3"
"@aws-lambda-powertools/logger": "^1.18.0",
"cls-rtracer": "^2.6.3",
"telegraf": "^4.15.3"
},
"devDependencies": {
"@athenna/common": "^4.32.0",
"@athenna/config": "^4.14.0",
"@athenna/ioc": "^4.14.0",
"@athenna/common": "^4.33.0",
"@athenna/config": "^4.15.0",
"@athenna/ioc": "^4.15.0",
"@athenna/test": "^4.21.0",
"@athenna/tsconfig": "^4.12.0",
"@typescript-eslint/eslint-plugin": "^6.7.4",
Expand Down
5 changes: 5 additions & 0 deletions src/constants/VanillaChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export const VANILLA_CHANNELS = {
formatter: 'cli',
driver: 'console'
},
lambda: {
level: 'trace',
formatter: 'json',
driver: 'lambda'
},
request: {
level: 'trace',
formatter: 'request',
Expand Down
37 changes: 37 additions & 0 deletions src/drivers/LambdaDriver.ts
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)
}
}
2 changes: 2 additions & 0 deletions src/factories/DriverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FileDriver } from '#src/drivers/FileDriver'
import { NullDriver } from '#src/drivers/NullDriver'
import { SlackDriver } from '#src/drivers/SlackDriver'
import { StackDriver } from '#src/drivers/StackDriver'
import { LambdaDriver } from '#src/drivers/LambdaDriver'
import { ConsoleDriver } from '#src/drivers/ConsoleDriver'
import { DiscordDriver } from '#src/drivers/DiscordDriver'
import { FactoryHelper } from '#src/helpers/FactoryHelper'
Expand All @@ -31,6 +32,7 @@ export class DriverFactory {
.set('null', { Driver: NullDriver })
.set('slack', { Driver: SlackDriver })
.set('stack', { Driver: StackDriver })
.set('lambda', { Driver: LambdaDriver })
.set('console', { Driver: ConsoleDriver })
.set('discord', { Driver: DiscordDriver })
.set('telegram', { Driver: TelegramDriver })
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/AwsLogger.ts
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' })
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* file that was distributed with this source code.
*/

export * from '#src/types'
export * from '#src/facades/Log'
export * from '#src/logger/Logger'
export * from '#src/drivers/Driver'
export * from '#src/helpers/AwsLogger'
export * from '#src/formatters/Formatter'
export * from '#src/helpers/FactoryHelper'
export * from '#src/factories/DriverFactory'
Expand Down
17 changes: 17 additions & 0 deletions src/types/Level.ts
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'
10 changes: 10 additions & 0 deletions src/types/index.ts
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'
6 changes: 5 additions & 1 deletion tests/fixtures/config/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default {
| Here you may configure the log channels for your application.
|
| Available Drivers:
| "console", "discord", "file", "null", "slack", "telegram".
| "console", "discord", "file", "null", "slack", "telegram", "lambda".
| Available Formatters:
| "cli", "simple", "json", "request", "message", "none".
|
Expand All @@ -48,6 +48,10 @@ export default {
formatter: 'simple',
level: 'debug'
},
lambda: {
driver: 'lambda',
formatter: 'json'
},
request: {
driver: 'console',
formatter: 'request',
Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures/transporters/consoleLambda.ts
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())
27 changes: 27 additions & 0 deletions tests/unit/drivers/LambdaDriverTest.ts
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'))
})
}
}
2 changes: 1 addition & 1 deletion tests/unit/factories/DriverFactoryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class DriverFactoryTest {
public async shouldBeAbleToListAllAvailableDrivers({ assert }: Context) {
const drivers = DriverFactory.availableDrivers()

assert.deepEqual(drivers, ['file', 'null', 'slack', 'stack', 'console', 'discord', 'telegram'])
assert.deepEqual(drivers, ['file', 'null', 'slack', 'stack', 'lambda', 'console', 'discord', 'telegram'])
}

@Test()
Expand Down

0 comments on commit 5ccb616

Please sign in to comment.