Skip to content

Commit 551cb23

Browse files
committed
refactor(logger): utility functions
1 parent c07ff7a commit 551cb23

8 files changed

Lines changed: 28 additions & 17 deletions

File tree

packages/logger/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
"default": "./dist/public-api.js"
1414
},
1515
"./node": {
16-
"types": "./dist/node-api.d.ts",
17-
"default": "./dist/node-api.js"
18-
},
19-
"./transports/node-console": {
20-
"types": "./dist/transports/node-console-log-transport/public-api.d.ts",
21-
"default": "./dist/transports/node-console-log-transport/public-api.js"
16+
"types": "./dist/node/public-api.d.ts",
17+
"default": "./dist/node/public-api.js"
2218
},
2319
"./testing": {
2420
"types": "./dist/testing/public-api.d.ts",

packages/logger/src/console-json-log-transport/console-json-log-transport-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface ConsoleJsonLogTransportConfig {
1414

1515
/**
1616
* when true, the log output will be a JS object instead of a JSON string. {@jsonStringifyReplacer} is still used.
17+
* enable this option, when your lambda function is configured with `loggingFormat='JSON'`
18+
* @hint when loggingFormat='JSON' is set, you should also configure `applicationLogLevelV2` with `TRACE` - otherwise you won't see all logging output.
1719
*/
1820
logJsObject?: boolean
1921
}

packages/logger/src/console-json-log-transport/console-json-log.transport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ export class ConsoleJsonLogTransport extends LogTransport {
1919
/** Ring buffer for log events below the configured level, flushed on ERROR */
2020
private pendingBuffer: BufferedLogMessage[] = []
2121

22+
private readonly logJsObject: boolean
2223
private readonly jsonStringifyReplacer: (key: string, value: any) => any
2324

2425
constructor(config: ConsoleJsonLogTransportConfig) {
2526
super(config.logLevel)
27+
this.logJsObject = config.logJsObject ?? false
2628
this.bufferSize = config.belowLevelLogBufferSize ?? ConsoleJsonLogTransport.DEFAULT_BUFFER_SIZE
2729
this.jsonStringifyReplacer = config.jsonStringifyReplacer ?? jsonMapSetStringifyReplacer
2830
}
@@ -38,6 +40,7 @@ export class ConsoleJsonLogTransport extends LogTransport {
3840
// to cut any potential references to objects that could potentially change until the log is actually written.
3941
const message = JSON.stringify(logObject, getJsonStringifyReplacer(this.jsonStringifyReplacer))
4042

43+
4144
if (!this.isLevelEnabled(level)) {
4245
pushToRingBuffer(this.pendingBuffer, { level, message }, this.bufferSize)
4346
return
@@ -55,7 +58,7 @@ export class ConsoleJsonLogTransport extends LogTransport {
5558
this.logToConsole(level, message)
5659
}
5760

58-
protected logToConsole(level: LogLevel, toLog: unknown) {
61+
protected logToConsole(level: LogLevel, toLog: string) {
5962
/* eslint-disable no-console */
6063
switch (level) {
6164
case LogLevel.DEBUG:

packages/logger/src/node-api.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/logger/src/node/create-console-logger.function.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import { ConsoleJsonLogTransport } from '../console-json-log-transport/console-json-log.transport.js'
2-
import { LogLevel } from '../model/log-level.enum.js'
2+
import { ConsoleJsonLogTransportConfig } from '../console-json-log-transport/console-json-log-transport-config.js'
33
import { LogTransport } from '../model/log-transport.js'
44
import { Logger } from '../model/logger.js'
55
import { BaseLoggerService } from '../services/base-logger.service.js'
66
import { NodeConsoleLogTransport } from './node-console-log-transport/node-console-log.transport.js'
7+
import { NodeConsoleLogTransportConfig } from './node-console-log-transport/node-console-log-transport-config.js'
78

9+
type Config = {
10+
node: NodeConsoleLogTransportConfig
11+
json: ConsoleJsonLogTransportConfig
12+
}
813
/**
914
* Creates a simple {@link Logger} instance that logs to the console with the specified name and log level
1015
1116
* @param name The name of the logger
1217
* @param logLevel decides the minimum log level to be logged
1318
* @param type decides whether to use {@link NodeConsoleLogTransport} or {@link ConsoleJsonLogTransport}
19+
* @param config optional configuration for the selected transport
1420
*/
15-
export function createConsoleLogger(name: string, logLevel: LogLevel, type: 'node' | 'json'): Logger {
21+
export function createConsoleLogger(
22+
name: string,
23+
config: Config,
24+
type: 'node' | 'json',
25+
): Logger {
1626
let transport: LogTransport
1727
switch (type) {
1828
case 'node':
19-
transport = new NodeConsoleLogTransport({ logLevel })
29+
transport = new NodeConsoleLogTransport(config.node)
2030
break
2131
case 'json':
22-
transport = new ConsoleJsonLogTransport({ logLevel })
32+
transport = new ConsoleJsonLogTransport(config.json)
2333
break
2434
default:
2535
transport = type

packages/logger/src/node/node-console-log-transport/node-console-log.transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { LogLevel } from '../../model/log-level.enum.js'
66
import { LogTransport } from '../../model/log-transport.js'
77
import { NodeConsoleLogTransportConfig } from './node-console-log-transport-config.js'
88

9-
export const logLevelEmoji = ['🐞', '💬', '💣', '🔥']
9+
export const logLevelEmoji: Record<LogLevel, string> = ['🐞', '💬', '💣', '🔥', '']
1010

1111
const timeFormat = new Intl.DateTimeFormat('en-US', {
1212
hour: '2-digit',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './create-console-logger.function.js'
2+
export * from './node-console-log-transport/node-console-log.transport.js'
3+
export * from './node-console-log-transport/node-console-log-transport-config.js'
4+
export * from './simple-lambda-logger.function.js'

packages/logger/src/node/simple-lambda-logger.function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ function isAwsLambdaEnv(): boolean {
1010

1111
export function simpleLambdaLogger(name: string, logLevel: LogLevel = LogLevel.DEBUG) {
1212
const isLambda = isAwsLambdaEnv()
13-
return createConsoleLogger(name, logLevel, isLambda ? 'json' : 'node')
13+
return createConsoleLogger(name, { json: { logLevel }, node: { logLevel } }, isLambda ? 'json' : 'node')
1414
}

0 commit comments

Comments
 (0)