-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): allow controlling level with env var (#452)
- Loading branch information
Showing
14 changed files
with
506 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { expect } from "chai" | ||
|
||
import { getLogger } from "../../src/logger/logger" | ||
|
||
const logger = getLogger() | ||
|
||
beforeEach(() => { | ||
(<any>logger).children = [] | ||
}) | ||
|
||
describe("LogEntry", () => { | ||
it("should dedent placeholder log entries", () => { | ||
const ph1 = logger.placeholder() | ||
const ph2 = ph1.placeholder() | ||
const nonEmpty = ph1.info("foo") | ||
const nested = nonEmpty.info("foo") | ||
const nestedPh = nested.placeholder() | ||
const indents = [ | ||
ph1.opts.indent, | ||
ph2.opts.indent, | ||
nonEmpty.opts.indent, | ||
nested.opts.indent, | ||
nestedPh.opts.indent, | ||
] | ||
expect(indents).to.eql([-1, -1, 0, 1, 0]) | ||
}) | ||
it("should indent nested log entries", () => { | ||
const entry = logger.info("hello") | ||
const nested = entry.info("nested") | ||
const deepNested = nested.info("deep nested") | ||
const deepDeepNested = deepNested.info("deep deep inside") | ||
const deepDeepPh = deepDeepNested.placeholder() | ||
const deepDeepNested2 = deepDeepPh.info("") | ||
const indents = [ | ||
entry.opts.indent, | ||
nested.opts.indent, | ||
deepNested.opts.indent, | ||
deepDeepNested.opts.indent, | ||
deepDeepPh.opts.indent, | ||
deepDeepNested2.opts.indent, | ||
] | ||
expect(indents).to.eql([undefined, 1, 2, 3, 2, 3]) | ||
}) | ||
context("preserveLevel is set to true", () => { | ||
it("should create a log entry whose children inherit the parent level", () => { | ||
const verbose = logger.verbose({ preserveLevel: true }) | ||
const error = verbose.error("") | ||
const silly = verbose.silly("") | ||
const deepError = error.error("") | ||
const deepSillyError = silly.error("") | ||
const deepSillySilly = silly.silly("") | ||
const levels = [ | ||
verbose.warn("").level, | ||
verbose.info("").level, | ||
verbose.verbose("").level, | ||
verbose.debug("").level, | ||
verbose.silly("").level, | ||
deepError.level, | ||
deepSillyError.level, | ||
deepSillySilly.level, | ||
] | ||
expect(levels).to.eql([3, 3, 3, 4, 5, 3, 3, 5]) | ||
}) | ||
}) | ||
describe("setState", () => { | ||
it("should update entry state and optionally append new msg to previous msg", () => { | ||
const entry = logger.info("") | ||
entry.setState("new") | ||
expect(entry.opts.msg).to.equal("new") | ||
entry.setState({ msg: "new2", append: true }) | ||
expect(entry.opts.msg).to.eql(["new", "new2"]) | ||
}) | ||
}) | ||
describe("setState", () => { | ||
it("should preserve status", () => { | ||
const entry = logger.info("") | ||
entry.setSuccess() | ||
entry.setState("change text") | ||
expect(entry.opts.status).to.equal("success") | ||
}) | ||
}) | ||
describe("setDone", () => { | ||
it("should update entry state and set status to done", () => { | ||
const entry = logger.info("") | ||
entry.setDone() | ||
expect(entry.opts.status).to.equal("done") | ||
}) | ||
}) | ||
describe("setSuccess", () => { | ||
it("should update entry state and set status and symbol to success", () => { | ||
const entry = logger.info("") | ||
entry.setSuccess() | ||
expect(entry.opts.status).to.equal("success") | ||
expect(entry.opts.symbol).to.equal("success") | ||
}) | ||
}) | ||
describe("setError", () => { | ||
it("should update entry state and set status and symbol to error", () => { | ||
const entry = logger.info("") | ||
entry.setError() | ||
expect(entry.opts.status).to.equal("error") | ||
expect(entry.opts.symbol).to.equal("error") | ||
}) | ||
}) | ||
describe("setWarn", () => { | ||
it("should update entry state and set status and symbol to warn", () => { | ||
const entry = logger.info("") | ||
entry.setWarn() | ||
expect(entry.opts.status).to.equal("warn") | ||
expect(entry.opts.symbol).to.equal("warning") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect } from "chai" | ||
import { getLogger } from "../../src/logger/logger" | ||
|
||
const logger = getLogger() | ||
|
||
beforeEach(() => { | ||
(<any>logger).children = [] | ||
}) | ||
|
||
describe("LogNode", () => { | ||
describe("appendNode", () => { | ||
it("should add new child entries to the respective node", () => { | ||
logger.error("error") | ||
logger.warn("warn") | ||
logger.info("info") | ||
logger.verbose("verbose") | ||
logger.debug("debug") | ||
logger.silly("silly") | ||
|
||
const prevLength = logger.children.length | ||
const entry = logger.children[0] | ||
const nested = entry.info("nested") | ||
const deepNested = nested.info("deep") | ||
|
||
expect(logger.children[0].children).to.have.lengthOf(1) | ||
expect(logger.children[0].children[0]).to.eql(nested) | ||
expect(logger.children[0].children[0].children[0]).to.eql(deepNested) | ||
expect(logger.children).to.have.lengthOf(prevLength) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect } from "chai" | ||
|
||
import { LogLevel } from "../../src/logger/log-node" | ||
import { getLogger } from "../../src/logger/logger" | ||
|
||
const logger = getLogger() | ||
|
||
beforeEach(() => { | ||
(<any>logger).children = [] | ||
}) | ||
|
||
describe("Logger", () => { | ||
describe("findById", () => { | ||
it("should return the first log entry with a matching id and undefined otherwise", () => { | ||
logger.info({ msg: "0" }) | ||
logger.info({ msg: "a1", id: "a" }) | ||
logger.info({ msg: "a2", id: "a" }) | ||
expect(logger.findById("a")["opts"]["msg"]).to.eql("a1") | ||
expect(logger.findById("z")).to.be.undefined | ||
}) | ||
}) | ||
|
||
describe("filterBySection", () => { | ||
it("should return an array of all entries with the matching section name", () => { | ||
logger.info({ section: "s0" }) | ||
logger.info({ section: "s1", id: "a" }) | ||
logger.info({ section: "s2" }) | ||
logger.info({ section: "s1", id: "b" }) | ||
const s1 = logger.filterBySection("s1") | ||
const sEmpty = logger.filterBySection("s99") | ||
expect(s1.map(entry => entry.id)).to.eql(["a", "b"]) | ||
expect(sEmpty).to.eql([]) | ||
}) | ||
}) | ||
|
||
describe("getLogEntries", () => { | ||
it("should return an ordered list of log entries", () => { | ||
logger.error("error") | ||
logger.warn("warn") | ||
logger.info("info") | ||
logger.verbose("verbose") | ||
logger.debug("debug") | ||
logger.silly("silly") | ||
|
||
const entries = logger.getLogEntries() | ||
const levels = entries.map(e => e.level) | ||
|
||
expect(entries).to.have.lengthOf(6) | ||
expect(levels).to.eql([ | ||
LogLevel.error, | ||
LogLevel.warn, | ||
LogLevel.info, | ||
LogLevel.verbose, | ||
LogLevel.debug, | ||
LogLevel.silly, | ||
]) | ||
}) | ||
}) | ||
|
||
}) |
Oops, something went wrong.