Skip to content

Commit

Permalink
refactor: Logger.redact + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahochsteger committed May 16, 2024
1 parent 8cbe776 commit 72b54dc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 82 deletions.
168 changes: 89 additions & 79 deletions src/lib/utils/Logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,94 +16,104 @@ afterEach(() => {
spy?.mockReset()
})

it("should log a trace message", () => {
spy = jest.spyOn(console, "log").mockImplementation()
spy.mockClear()
logger.trace(mocks.processingContext, {
location: "Logger.spec",
message: "Log message",
describe("log levels", () => {
it("should log a trace message", () => {
spy = jest.spyOn(console, "log").mockImplementation()
spy.mockClear()
logger.trace(mocks.processingContext, {
location: "Logger.spec",
message: "Log message",
})
expect(spy.mock.calls?.[0]?.[0]).toMatch(
/^[^ ]+ TRACE \[Logger\.spec\] Log message$/,
)
})
expect(spy.mock.calls?.[0]?.[0]).toMatch(
/^[^ ]+ TRACE \[Logger\.spec\] Log message$/,
)
})

it("should log a debug message", () => {
spy = jest.spyOn(console, "log").mockImplementation()
spy.mockClear()
logger.debug("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ DEBUG Log message$/)
})
it("should log a debug message", () => {
spy = jest.spyOn(console, "log").mockImplementation()
spy.mockClear()
logger.debug("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ DEBUG Log message$/)
})

it("should log an info message", () => {
spy = jest.spyOn(console, "log").mockImplementation()
spy.mockClear()
logger.info("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ INFO Log message$/)
})
it("should log an info message", () => {
spy = jest.spyOn(console, "log").mockImplementation()
spy.mockClear()
logger.info("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ INFO Log message$/)
})

it("should log a warning message", () => {
const spy = jest.spyOn(console, "error").mockImplementation()
spy.mockClear()
logger.warn("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ WARN Log message$/)
})
it("should log a warning message", () => {
const spy = jest.spyOn(console, "error").mockImplementation()
spy.mockClear()
logger.warn("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ WARN Log message$/)
})

it("should log an error message", () => {
const spy = jest.spyOn(console, "error").mockImplementation()
spy.mockClear()
logger.error("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ ERROR Log message$/)
})
it("should log an error message", () => {
const spy = jest.spyOn(console, "error").mockImplementation()
spy.mockClear()
logger.error("Log message")
expect(spy.mock.calls[0][0]).toMatch(/^[^ ]+ ERROR Log message$/)
})

it("should filter log levels", () => {
logger = new Logger(LogLevel.WARN)
const spyLog = jest.spyOn(console, "log")
const spyError = jest.spyOn(console, "error")
spyLog.mockClear()
logger.debug("Log message")
expect(spyLog).not.toHaveBeenCalled()
spyLog.mockClear()
logger.info("Log message")
expect(spyLog).not.toHaveBeenCalled()
spyError.mockClear()
logger.warn("Log message")
expect(spyError).toHaveBeenCalled()
spyError.mockClear()
logger.error("Log message")
expect(spyError).toHaveBeenCalled()
it("should filter log levels", () => {
logger = new Logger(LogLevel.WARN)
const spyLog = jest.spyOn(console, "log")
const spyError = jest.spyOn(console, "error")
spyLog.mockClear()
logger.debug("Log message")
expect(spyLog).not.toHaveBeenCalled()
spyLog.mockClear()
logger.info("Log message")
expect(spyLog).not.toHaveBeenCalled()
spyError.mockClear()
logger.warn("Log message")
expect(spyError).toHaveBeenCalled()
spyError.mockClear()
logger.error("Log message")
expect(spyError).toHaveBeenCalled()
})
})

it("should trace to console and not logsheet", () => {
const config = ConfigMocks.newDefaultConfig()
config.settings.logSheetTracing = false
mocks = MockFactory.newMocks(config)
const ctx = mocks.processingContext
const logSpy = jest.spyOn(console, "log")
const sheetSpy = jest.spyOn(ctx.proc.spreadsheetAdapter, "log")
logSpy.mockClear()
sheetSpy.mockClear()
logger.trace(ctx, { location: "Logger.logTrace()", message: "Trace message" })
expect(logSpy.mock.calls[0][0]).toMatch(
/^[^ ]+ TRACE \[Logger\.logTrace\(\)\] Trace message/,
)
expect(sheetSpy).not.toHaveBeenCalled()
})
describe("logsheet", () => {
it("should trace to console and not logsheet", () => {
const config = ConfigMocks.newDefaultConfig()
config.settings.logSheetTracing = false
mocks = MockFactory.newMocks(config)
const ctx = mocks.processingContext
const logSpy = jest.spyOn(console, "log")
const sheetSpy = jest.spyOn(ctx.proc.spreadsheetAdapter, "log")
logSpy.mockClear()
sheetSpy.mockClear()
logger.trace(ctx, {
location: "Logger.logTrace()",
message: "Trace message",
})
expect(logSpy.mock.calls[0][0]).toMatch(
/^[^ ]+ TRACE \[Logger\.logTrace\(\)\] Trace message/,
)
expect(sheetSpy).not.toHaveBeenCalled()
})

it("should trace to console and logsheet", () => {
const config = ConfigMocks.newDefaultConfig()
config.settings.logSheetTracing = true
mocks = MockFactory.newMocks(config)
const ctx = mocks.processingContext
const logSpy = jest.spyOn(console, "log")
const sheetSpy = jest.spyOn(ctx.proc.spreadsheetAdapter, "log")
logSpy.mockClear()
sheetSpy.mockClear()
logger.trace(ctx, { location: "Logger.logTrace()", message: "Trace message" })
expect(logSpy.mock.calls[0][0]).toMatch(
/^[^ ]+ TRACE \[Logger\.logTrace\(\)\] Trace message/,
)
expect(sheetSpy).toHaveBeenCalled()
it("should trace to console and logsheet", () => {
const config = ConfigMocks.newDefaultConfig()
config.settings.logSheetTracing = true
mocks = MockFactory.newMocks(config)
const ctx = mocks.processingContext
const logSpy = jest.spyOn(console, "log")
const sheetSpy = jest.spyOn(ctx.proc.spreadsheetAdapter, "log")
logSpy.mockClear()
sheetSpy.mockClear()
logger.trace(ctx, {
location: "Logger.logTrace()",
message: "Trace message",
})
expect(logSpy.mock.calls[0][0]).toMatch(
/^[^ ]+ TRACE \[Logger\.logTrace\(\)\] Trace message/,
)
expect(sheetSpy).toHaveBeenCalled()
})
})

describe("redact", () => {
Expand Down
4 changes: 1 addition & 3 deletions src/lib/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export class Logger {
}
}
redact(ctx: ProcessingContext, value?: string | null): string {
if (value === null || value === undefined) {
return ""
}
if (!value) return ""
switch (ctx.proc.config.settings.logSensitiveRedactionMode) {
case LogRedactionMode.ALL:
value = "(redacted)"
Expand Down

0 comments on commit 72b54dc

Please sign in to comment.