Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
"email": "[email protected]"
},
"devDependencies": {
"@types/jest": "^29.2.5",
"@types/jest": "^29.5.14",
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"eslint": "^8.34.0",
"jest": "^29.4.3",
"ts-jest": "^29.0.4",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-mockito": "^2.6.1",
"typescript": "^4.9.4"
}
}
}
28 changes: 28 additions & 0 deletions src/formats/json.format.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { JsonFormat, JsonFormatOptions } from './json.format'
import { Log } from '../log'
import { Level } from '../level'

describe('JsonFormat', () => {
describe('when log has timestamp set to epoch timestamp', () => {
it('it includes the timestamp attribute formatted as ISO-8601 with UTC timezone', () => {
const timestamp = 1731912859428
const log: Log = {
level: Level.INFO,
message: '',
timestamp,
}
const testee: JsonFormat = createTestee()

const result: string = testee.apply(log)

expect(result).toMatch('"timestamp":"2024-11-18T06:54:19.428Z"')
})
})
})

function createTestee(options: Partial<JsonFormatOptions> = {}): JsonFormat {
return new JsonFormat({
isPretty: false,
...options,
})
}
21 changes: 19 additions & 2 deletions src/formats/json.format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Format } from '../format'
import { Log } from '../log'
import { JsonEncoder } from '../services/json-encoder'
import { DateFormatter } from '../services/date-formatter'
import { IsoDateFormatter } from '../services/iso-date-formatter'

export interface JsonFormatOptions {
isPretty: boolean
Expand All @@ -9,14 +11,29 @@ export interface JsonFormatOptions {
export class JsonFormat extends Format {

private readonly jsonEncoder: JsonEncoder
private readonly dateFormatter: DateFormatter

constructor(options: JsonFormatOptions) {
super()
this.jsonEncoder = new JsonEncoder(options)
this.dateFormatter = new IsoDateFormatter()
}

apply(log: Log): string {
return this.jsonEncoder.encode(log)
public apply(log: Log): string {
return this.jsonEncoder.encode({
...log,
timestamp: this.getFormattedTimestamp(log.timestamp)
})
}

private getFormattedTimestamp(timestamp: unknown): string {
if (typeof timestamp === 'number') {
return this.dateFormatter.formatTimestamp(timestamp)
}
if (!timestamp) {
return this.dateFormatter.formatTimestamp(Date.now())
}
return `${timestamp}`
}

}
10 changes: 7 additions & 3 deletions src/formats/plain-text.format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export class PlainTextFormat extends Format {
}

protected getTimestamp(log: Log): string {
const timestamp = log.timestamp ?? Date.now()
const formattedTimestamp = this.dateFormatter.formatTimestamp(timestamp)
return `[${ formattedTimestamp }]`
if (typeof log.timestamp === 'number') {
return `[${this.dateFormatter.formatTimestamp(log.timestamp)}]`
}
if (!log.timestamp) {
return `[${this.dateFormatter.formatTimestamp(Date.now())}]`
}
return `[${ log.timestamp }]`
}

protected getSeverity(log: Log): string {
Expand Down
1 change: 0 additions & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import { Level } from './level'
export interface Log {
level: Level,
message: unknown,
timestamp?: number,
[key: string]: unknown,
}
1 change: 1 addition & 0 deletions src/logger-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class LoggerBase extends Logger {

private log(message: unknown, level: Level, metadata: object): void {
const log: Log = {
timestamp: Date.now(),
...metadata,
level,
message,
Expand Down
Loading
Loading