Skip to content

Commit

Permalink
refactor(logger): add dataFormat to LogEntry
Browse files Browse the repository at this point in the history
Add the possibility of passing a dataFormat field as LogEntryParams
to allow choosing the render methord for the data field.
Allowed options are "json" and "yaml". Default remains "yaml"
  • Loading branch information
10ko authored and eysi09 committed Jan 22, 2020
1 parent 542e205 commit bac4f74
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions garden-service/src/logger/log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface MessageBase {
symbol?: LogSymbol
append?: boolean
data?: any
dataFormat?: "json" | "yaml"
maxSectionWidth?: number
}

Expand All @@ -55,6 +56,7 @@ export interface UpdateLogEntryParams extends MessageBase {
export interface LogEntryParams extends UpdateLogEntryParams {
error?: GardenError
data?: any // to be rendered as e.g. YAML or JSON
dataFormat?: "json" | "yaml" // how to render the data object
indent?: number
childEntriesInheritLevel?: boolean
fromStdStream?: boolean
Expand Down Expand Up @@ -107,6 +109,7 @@ export class LogEntry extends LogNode {
symbol: params.symbol,
status: params.level === LogLevel.error ? "error" : params.status,
data: params.data,
dataFormat: params.dataFormat,
maxSectionWidth: params.maxSectionWidth,
})
}
Expand All @@ -130,6 +133,7 @@ export class LogEntry extends LogNode {
status: updateParams.status || messageState.status,
symbol: updateParams.symbol || messageState.symbol,
data: updateParams.data || messageState.data,
dataFormat: updateParams.dataFormat || messageState.dataFormat,
// Next state does not inherit the append field
append: updateParams.append,
timestamp: Date.now(),
Expand Down
9 changes: 6 additions & 3 deletions garden-service/src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,15 @@ export function renderMsg(entry: LogEntry): string {
}

export function renderData(entry: LogEntry): string {
const { data } = entry.getMessageState()
const { data, dataFormat } = entry.getMessageState()
if (!data) {
return ""
}
const asYaml = yaml.safeDump(data, { noRefs: true, skipInvalid: true })
return highlightYaml(asYaml)
if (!dataFormat || dataFormat === "yaml") {
const asYaml = yaml.safeDump(data, { noRefs: true, skipInvalid: true })
return highlightYaml(asYaml)
}
return JSON.stringify(data, null, 2)
}

export function renderSection(entry: LogEntry): string {
Expand Down
7 changes: 7 additions & 0 deletions garden-service/test/unit/src/logger/log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe("LogEntry", () => {
symbol: undefined,
status: undefined,
data: undefined,
dataFormat: undefined,
append: undefined,
maxSectionWidth: undefined,
}
Expand All @@ -86,6 +87,7 @@ describe("LogEntry", () => {
symbol: "info",
status: "done",
data: { some: "data" },
dataFormat: "json",
metadata: { task: taskMetadata },
maxSectionWidth: 8,
})
Expand All @@ -98,6 +100,7 @@ describe("LogEntry", () => {
symbol: "info",
status: "done",
data: { some: "data" },
dataFormat: "json",
append: undefined,
timestamp,
maxSectionWidth: 8,
Expand Down Expand Up @@ -126,6 +129,7 @@ describe("LogEntry", () => {
symbol: "info",
status: "done",
data: { some: "data" },
dataFormat: undefined,
append: undefined,
timestamp,
maxSectionWidth: 0,
Expand Down Expand Up @@ -162,6 +166,7 @@ describe("LogEntry", () => {
symbol: "info",
status: "done",
data: { some: "data" },
dataFormat: undefined,
append: undefined,
timestamp,
maxSectionWidth: 8,
Expand All @@ -173,6 +178,7 @@ describe("LogEntry", () => {
symbol: "info",
status: "done",
data: { some: "data_updated" },
dataFormat: undefined,
append: undefined,
timestamp,
maxSectionWidth: 10,
Expand All @@ -184,6 +190,7 @@ describe("LogEntry", () => {
symbol: "info",
status: "done",
data: { some: "data_updated" },
dataFormat: undefined,
append: undefined,
timestamp,
maxSectionWidth: 0,
Expand Down
34 changes: 34 additions & 0 deletions garden-service/test/unit/src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import {
formatForJson,
renderSection,
MAX_SECTION_WIDTH,
renderData,
} from "../../../../src/logger/renderers"
import { GardenError } from "../../../../src/exceptions"
import dedent = require("dedent")
import { TaskMetadata } from "../../../../src/logger/log-entry"
import logSymbols = require("log-symbols")
import stripAnsi = require("strip-ansi")
import yaml from "js-yaml"
import { highlightYaml } from "../../../../src/util/util"

const logger: any = getLogger()

Expand Down Expand Up @@ -234,4 +237,35 @@ describe("renderers", () => {
})
})
})
describe.only("renderData", () => {
const sampleData = {
key: "value",
key2: {
value: [
{
key1: "value",
key2: 3,
},
],
},
}
it("should render an empty string when no data is passed", () => {
const entry = logger.placeholder()
expect(renderData(entry)).to.eql("")
})
it("should render yaml by default if data is passed", () => {
const entry = logger.info({ data: sampleData })
const dataAsYaml = yaml.safeDump(sampleData, { noRefs: true, skipInvalid: true })
expect(renderData(entry)).to.eql(highlightYaml(dataAsYaml))
})
it('should render yaml if dataFormat is "yaml"', () => {
const entry = logger.info({ data: sampleData, dataFormat: "yaml" })
const dataAsYaml = yaml.safeDump(sampleData, { noRefs: true, skipInvalid: true })
expect(renderData(entry)).to.eql(highlightYaml(dataAsYaml))
})
it('should render json if dataFormat is "json"', () => {
const entry = logger.info({ data: sampleData, dataFormat: "json" })
expect(renderData(entry)).to.eql(JSON.stringify(sampleData, null, 2))
})
})
})

0 comments on commit bac4f74

Please sign in to comment.