Skip to content

Commit

Permalink
Merge pull request #16 from krauters/feature/coltenkrauter/2024-12-10/5
Browse files Browse the repository at this point in the history
Feat Support Log Prefix
  • Loading branch information
coltenkrauter committed Dec 11, 2024
2 parents 1726977 + 6a9499b commit 838dd11
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@krauters/logger",
"description": "TypeScript wrapper for Winston, optimized for structured Lambda logs and CloudWatch.",
"version": "1.3.1",
"version": "1.4.3",
"main": "dist/src/index.js",
"type": "commonjs",
"scripts": {
Expand Down Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"@aws-sdk/client-cloudwatch": "^3.709.0",
"@aws-sdk/client-s3": "^3.709.0",
"@krauters/environment": "^0.5.0",
"@krauters/environment": "^0.5.2",
"@krauters/structures": "^1.3.0",
"@types/aws-lambda": "^8.10.146",
"@types/uuid": "^10.0.0",
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export interface ConfigOptions {
LOG_FORMAT: string
LOG_FRIENDLY_FIELDS_HIDE?: string[]
LOG_LEVEL: LogLevel
LOG_PREFIX: string
LOG_SECTION_SEPARATOR: string
LOG_STRUCTURED_FIELDS_HIDE?: string[]
PACKAGE: string
STAGE: Stage
TIMESTAMP_FORMAT: string
VERSION: string
}

/**
Expand All @@ -30,21 +32,20 @@ export interface ConfigOptions {
* @returns The environment variable configuration.
*/
export function getConfig(options?: Partial<ConfigOptions>) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return EnvironmentBuilder.create(
'CODENAME',
'ENV',
'HOST',
'LOG_FORMAT',
'LOG_LEVEL',
'LOG_PREFIX',
'LOG_SECTION_SEPARATOR',
'PACKAGE',
'SIMPLE_LOGS',
'STAGE',
'TIMESTAMP_FORMAT',
'VERSION',
)

.optionals('REQUEST_ID', 'LOG_FRIENDLY_FIELDS_HIDE', 'LOG_STRUCTURED_FIELDS_HIDE')
.transform((value) => !isFalsy(value), 'SIMPLE_LOGS')
.transform(
Expand All @@ -62,6 +63,7 @@ export function getConfig(options?: Partial<ConfigOptions>) {
HOST: hostname(),
LOG_FORMAT: 'friendly',
LOG_LEVEL: LogLevel.Info,
LOG_PREFIX: '',
LOG_SECTION_SEPARATOR: ' | ',
PACKAGE: empty,
SIMPLE_LOGS: false,
Expand Down
11 changes: 6 additions & 5 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ export class Logger {
return this.log({ level: LogLevel.Error, message, metadata: data })
}

public formatLogMessage(info: Record<string, unknown>, separator: string): string {
public formatLogMessage(info: Record<string, unknown>, separator: string, prefix: string): string {
const { level, message, timestamp, ...rest } = info

return [
chalk.blue(String(timestamp)),
String(level),
String(message),
prefix + String(message),
...Object.entries(rest)
.filter(([, value]) => value !== undefined && value !== '')
.map(([key, value]) => `${key}: ${String(value)}`),
Expand All @@ -99,16 +99,17 @@ export class Logger {
}

public getFriendlyFormat(): Format {
const separator = this.config.LOG_SECTION_SEPARATOR
const separator: string = this.config.LOG_SECTION_SEPARATOR
const prefix: string = this.config.LOG_PREFIX ?? ''

return format.combine(
format.colorize({ all: true }),
format.timestamp({ format: this.config.TIMESTAMP_FORMAT }),
format.printf((info) =>
this.formatLogMessage(
this.getLogObject({ fieldsToHide: this.config.LOG_FRIENDLY_FIELDS_HIDE, info }),
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
separator,
prefix,
),
),
)
Expand All @@ -126,7 +127,6 @@ export class Logger {
}

public getRequestId(context?: LambdaContext): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (this.config.REQUEST_ID) return this.config.REQUEST_ID
if (context?.awsRequestId) return context.awsRequestId
if (this.config.SIMPLE_LOGS) return uuidv4().split('-')[0]
Expand Down Expand Up @@ -269,6 +269,7 @@ export class Logger {
version: this.config.VERSION,
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
return Object.fromEntries(Object.entries(base).filter(([, value]) => value !== empty && value !== Env.Unknown))
}
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export function isFalsy(text: string, additionalFalsyValues: string[] = []): boo
const defaultFalsyValues = ['', '0', 'false', 'no', 'nil', 'none', 'n/a', 'undefined', 'null', 'off']
const falsyValues = [...defaultFalsyValues, ...additionalFalsyValues.map((v) => v.toLowerCase())]

return falsyValues.includes(text.trim().toLowerCase())
return falsyValues.includes(String(text).trim().toLowerCase())
}
2 changes: 1 addition & 1 deletion test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Logger', () => {
timestamp: '2023-09-30 12:34:56',
}
const separator = ' | '
const formattedMessage = logger.formatLogMessage(info, separator)
const formattedMessage = logger.formatLogMessage(info, separator, '')
expect(formattedMessage).toContain('2023-09-30 12:34:56')
})

Expand Down

0 comments on commit 838dd11

Please sign in to comment.