Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions .changesets/auto-detect-log-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
bump: patch
type: change
---

Detect the log format automatically. We now detect if a log line is in the JSON, Logfmt, or plaintext format. No further config needed when calling our logger, like so:

```javascript
const logger = Appsignal.logger("app");
logger.info("message");
```
19 changes: 11 additions & 8 deletions src/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ describe("BaseLogger", () => {
).toEqual(6)
})

it("defaults to a plaintext logger format", () => {
expect(logger.format).toEqual(0)
it("defaults to the autodetect logger format", () => {
expect(logger.format).toEqual(3)
expect(client.internalLogger.warn).not.toHaveBeenCalled()
})

it("sets a plaintext format level when the format is unknown and logs a warning", () => {
it("sets the autodetect format level when the format is unknown and logs a warning", () => {
logger = new BaseLogger(
client,
"groupname",
"trace" as LoggerLevel,
"bacon" as LoggerFormat
)
expect(logger.format).toEqual(0)
expect(logger.format).toEqual(3)
expect(client.internalLogger.warn).toHaveBeenCalledWith(
expect.stringContaining(`"bacon"`)
)
Expand All @@ -80,6 +80,9 @@ describe("BaseLogger", () => {
expect(new BaseLogger(client, "groupname", "trace", "json").format).toEqual(
2
)
expect(
new BaseLogger(client, "groupname", "trace", "autodetect").format
).toEqual(3)
})

it("logs to the extension if at or above the logger level", () => {
Expand All @@ -96,28 +99,28 @@ describe("BaseLogger", () => {
expect(client.extension.log).toHaveBeenCalledWith(
"groupname",
3,
0,
3,
"info message",
attributes
)
expect(client.extension.log).toHaveBeenCalledWith(
"groupname",
4,
0,
3,
"log message",
attributes
)
expect(client.extension.log).toHaveBeenCalledWith(
"groupname",
5,
0,
3,
"warn message",
attributes
)
expect(client.extension.log).toHaveBeenCalledWith(
"groupname",
6,
0,
3,
"error message",
attributes
)
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class Client {
static logger(
group: string,
level: LoggerLevel = "info",
format: LoggerFormat = "plaintext"
format: LoggerFormat = "autodetect"
): Logger {
if (this.client) {
return this.client.logger(group, level, format)
Expand Down
13 changes: 7 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ function severity(level: LoggerLevel) {
return LOGGER_LEVEL_SEVERITY[level] ?? UNKNOWN_SEVERITY
}

export type LoggerFormat = "plaintext" | "logfmt" | "json"
export type LoggerFormat = "plaintext" | "logfmt" | "json" | "autodetect"

export const LOGGER_FORMAT: Record<LoggerFormat, number> = {
plaintext: 0,
logfmt: 1,
json: 2
json: 2,
autodetect: 3
}

const UNKNOWN_FORMAT = -1
Expand Down Expand Up @@ -52,7 +53,7 @@ export class BaseLogger implements Logger {
client: Client,
group: string,
level: LoggerLevel = "info",
format: LoggerFormat = "plaintext"
format: LoggerFormat = "autodetect"
) {
if (typeof group != "string") {
throw new TypeError(
Expand All @@ -75,10 +76,10 @@ export class BaseLogger implements Logger {

if (this.format == UNKNOWN_FORMAT) {
this.#client.internalLogger.warn(
`Logger format must be "plaintext", "logfmt", or "json", ` +
`but "${format}" was given. Logger format set to "plaintext".`
`Logger format must be "plaintext", "logfmt", "json", or "autodetect", ` +
`but "${format}" was given. Logger format set to "autodetect".`
)
this.format = 0
this.format = 3
}
}

Expand Down
Loading