Skip to content

Commit

Permalink
util/logging.ts: Add support for logging (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
findgriffin authored Dec 10, 2024
1 parent a294236 commit 2527871
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 19 deletions.
6 changes: 6 additions & 0 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
},

close() {},
getURL(): string {
return "http://foo.com/bar";
},
};

const client = getClient(
Expand Down Expand Up @@ -147,6 +150,9 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
},

close() {},
getURL(): string {
return "https://foo.com/bar";
},
};

const client = getClient(
Expand Down
2 changes: 2 additions & 0 deletions __tests__/functional/feed-client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FeedClient,
} from "../../src";
import { getDefaultHTTPClientOptions } from "../client";
import { defaultLogHandler } from "../../src/util/logging";

const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions());
const defaultConfig: FeedClientConfiguration = {
Expand All @@ -14,6 +15,7 @@ const defaultConfig: FeedClientConfiguration = {
max_backoff: 20,
query_timeout_ms: 5000,
client_timeout_buffer_ms: 5000,
logger: defaultLogHandler(),
httpClient: defaultHttpClient,
};
const dummyStreamToken = new StreamToken("dummy");
Expand Down
7 changes: 7 additions & 0 deletions __tests__/functional/feed-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
EventSource,
StreamToken,
ThrottlingError,
HTTPClient,
HTTPRequest,
HTTPResponse,
} from "../../src";
import { defaultLogHandler } from "../../src/util/logging";

const mockHttpResponse = {
status: 200,
Expand All @@ -27,14 +31,17 @@ const mockHttpClient = {
.fn()
.mockImplementation(() => Promise.resolve({ ...mockHttpResponse })),
close: jest.fn(),
getURL: jest.fn(() => "bar"),
};

const defaultConfig: FeedClientConfiguration = {
secret: "secret",
long_type: "number",
max_attempts: 3,
max_backoff: 20,
query_timeout_ms: 5000,
client_timeout_buffer_ms: 5000,
logger: defaultLogHandler(),
httpClient: mockHttpClient,
};
const testEventSource: EventSource = new StreamToken("dummy");
Expand Down
3 changes: 3 additions & 0 deletions __tests__/functional/stream-client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import {
StreamToken,
getDefaultHTTPClient,
StreamClientConfiguration,
ConsoleLogHandler,
} from "../../src";
import { getDefaultHTTPClientOptions } from "../client";
import { defaultLogHandler } from "../../src/util/logging";

const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions());
const defaultConfig: StreamClientConfiguration = {
secret: "secret",
long_type: "number",
max_attempts: 3,
max_backoff: 20,
logger: defaultLogHandler(),
httpStreamClient: defaultHttpClient,
};
const dummyStreamToken = new StreamToken("dummy");
Expand Down
14 changes: 10 additions & 4 deletions __tests__/integration/client-last-txn-tracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ describe("last_txn_ts tracking in client", () => {
},

close() {},
getURL(): string {
return "http://foo.com/bar";
},
};

const myClient = getClient(
{
query_timeout_ms: 60_000,
},
httpClient
httpClient,
);

const resultOne = await myClient.query(fql`
Expand All @@ -35,7 +38,7 @@ describe("last_txn_ts tracking in client", () => {
fql`
if (Collection.byName('Orders') == null) {
Collection.create({ name: 'Orders' })
}`
}`,
);
expect(resultTwo.txn_ts).not.toBeUndefined();
expect(resultTwo.txn_ts).not.toEqual(resultOne.txn_ts);
Expand Down Expand Up @@ -63,13 +66,16 @@ describe("last_txn_ts tracking in client", () => {
},

close() {},
getURL(): string {
return "http://foo.com/bar";
},
};

const myClient = getClient(
{
query_timeout_ms: 60_000,
},
httpClient
httpClient,
);

const resultOne = await myClient.query(fql`
Expand All @@ -85,7 +91,7 @@ describe("last_txn_ts tracking in client", () => {
if (Collection.byName('Orders') == null) {\
Collection.create({ name: 'Orders' })\
}
`
`,
);
expect(resultTwo.txn_ts).not.toBeUndefined();
expect(resultTwo.txn_ts).not.toEqual(resultOne.txn_ts);
Expand Down
2 changes: 2 additions & 0 deletions __tests__/integration/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getDefaultHTTPClientOptions,
getDefaultSecretAndEndpoint,
} from "../client";
import { defaultLogHandler } from "../../src/util/logging";

const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions());
const { secret } = getDefaultSecretAndEndpoint();
Expand All @@ -29,6 +30,7 @@ const defaultFeedConfig: FeedClientConfiguration = {
max_attempts: 3,
max_backoff: 20,
httpClient: defaultHttpClient,
logger: defaultLogHandler(),
client_timeout_buffer_ms: 5000,
query_timeout_ms: 5000,
};
Expand Down
15 changes: 15 additions & 0 deletions __tests__/integration/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ describe("query", () => {
return dummyResponse;
},
close() {},
getURL(): string {
return "http://foo.com/bar";
},
};
const clientConfiguration: Partial<ClientConfiguration> = {
linearized: true,
Expand All @@ -163,6 +166,9 @@ describe("query", () => {
return dummyResponse;
},
close() {},
getURL() {
return "http://foo.com/bar";
},
};

let clientConfiguration: Partial<ClientConfiguration> = {
Expand Down Expand Up @@ -271,6 +277,9 @@ describe("query", () => {
);
},
close() {},
getURL() {
return "http://foo.com/bar";
},
};
badClient = getClient({}, httpClient);
await badClient.query(fql`"dummy"`);
Expand Down Expand Up @@ -379,6 +388,9 @@ describe("query", () => {
return httpClient.request(badRequest);
},
close() {},
getURL() {
return "http://foo.com/bar";
},
};

const badClient = getClient({}, badHTTPClient);
Expand All @@ -400,6 +412,9 @@ describe("query", () => {
throw new Error("boom!");
},
close() {},
getURL(): string {
return "http://foo.com/bar";
},
};
const badClient = getClient(
{
Expand Down
6 changes: 6 additions & 0 deletions __tests__/integration/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ describe("SetIterator", () => {
close() {
return;
},
getURL(): string {
return "http://foo.com/bar";
},
};
const testClient = getClient({}, httpClient);

Expand Down Expand Up @@ -248,6 +251,9 @@ describe("SetIterator", () => {
close() {
return;
},
getURL(): string {
return "http://foo.com/bar";
},
};
const testClient = getClient({}, httpClient);

Expand Down
2 changes: 2 additions & 0 deletions __tests__/integration/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getDefaultHTTPClientOptions,
getDefaultSecretAndEndpoint,
} from "../client";
import { defaultLogHandler } from "../../src/util/logging";

const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions());
const { secret } = getDefaultSecretAndEndpoint();
Expand All @@ -31,6 +32,7 @@ const defaultStreamConfig: StreamClientConfiguration = {
long_type: "number",
max_attempts: 3,
max_backoff: 20,
logger: defaultLogHandler(),
httpStreamClient: defaultHttpClient,
};

Expand Down
128 changes: 128 additions & 0 deletions __tests__/unit/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { ConsoleLogHandler, LOG_LEVELS, parseDebugLevel } from "../../src";
import { LogHandler } from "../../src/util/logging";

describe("logging", () => {
describe("parseDebugLevel", () => {
it.each`
testName | input | result
${"'0'"} | ${"0"} | ${"0"}
${"'1'"} | ${"1"} | ${"1"}
${"'2'"} | ${"2"} | ${"2"}
${"'3'"} | ${"3"} | ${"3"}
${"'4'"} | ${"4"} | ${"4"}
${"'5'"} | ${"5"} | ${"5"}
${"'6'"} | ${"6"} | ${"6"}
${"LOG_LEVELS.TRACE"} | ${LOG_LEVELS.TRACE} | ${LOG_LEVELS.TRACE}
${"LOG_LEVELS.DEBUG"} | ${LOG_LEVELS.DEBUG} | ${LOG_LEVELS.DEBUG}
${"LOG_LEVELS.INFO"} | ${LOG_LEVELS.INFO} | ${LOG_LEVELS.INFO}
${"LOG_LEVELS.WARN"} | ${LOG_LEVELS.WARN} | ${LOG_LEVELS.WARN}
${"LOG_LEVELS.ERROR"} | ${LOG_LEVELS.ERROR} | ${LOG_LEVELS.ERROR}
${"LOG_LEVELS.FATAL"} | ${LOG_LEVELS.FATAL} | ${LOG_LEVELS.FATAL}
${"LOG_LEVELS.OFF"} | ${LOG_LEVELS.OFF} | ${LOG_LEVELS.OFF}
${"empty"} | ${""} | ${"6"}
${"null"} | ${null} | ${"6"}
${"undefined"} | ${undefined} | ${"6"}
${"unkown number string"} | ${"42"} | ${"6"}
${"unknown string"} | ${"asdf"} | ${"6"}
${"number"} | ${42} | ${"6"}
`(
"correctly parses $testName to log level '$result'",
({ input, result }) => {
expect(parseDebugLevel(input)).toBe(result);
},
);
});
});

describe("levels", () => {
describe("TRACE log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.TRACE);
it("always log", () => {
logger.trace("Should log trace");
logger.debug("Should log debug");
logger.warn("Should log warnings");
logger.error("Should log errors");
logger.fatal("Should log fatal");
});
});
describe("DEBUG log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.DEBUG);
it("skipped", () => {
logger.trace("Should not log", "foo");
});
it("debug", () => {
logger.debug("Should log something");
});
it("warn", () => {
logger.warn("Should also log (%s substitution!)", "with");
});
});
describe("ERROR log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.ERROR);
it("skipped", () => {
logger.trace("Should not log", "foo");
logger.debug("Should not log", "bar");
logger.warn("Should not log");
});
it("logged", () => {
logger.error("Should log (%s substitution!)", "with");
});
});
});
describe("FATAL log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.FATAL);
it("skipped", () => {
logger.trace("Should not log", "trace");
logger.debug("Should not log", "debug");
logger.info("Should not log", "info");
logger.warn("Should not log", "warn");
logger.error("Should not log", "error");
});
it("logged", () => {
logger.fatal("Should log (%s substitution!)", "with");
logger.fatal("Should log without substitution!");
});
});

describe("OFF log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.OFF);
it("skipped", () => {
logger.trace("Should not log", "trace");
logger.debug("Should not log", "debug");
logger.info("Should not log", "info");
logger.warn("Should not log", "warn");
logger.error("Should not log", "error");
logger.fatal("Shoiuld not log", "fatal");
});
});

describe("Log messages", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.TRACE);
describe("Log message construction", () => {
it("trace", () => {
logger.trace("hello %s world", "foo", "bar"); // hello foo world bar
logger.trace("hello %s %s world", "foo", "bar"); // hello foo bar world
logger.trace("hello %s %s %s world", "foo", "bar"); // hello foo bar %s world
});
it("debug", () => {
logger.debug("hello %s world", "foo", "bar"); // hello foo world bar
logger.debug("hello %s %s world", "foo", "bar"); // hello foo bar world
logger.debug("hello %s %s %s world", "foo", "bar"); // hello foo bar %s world
});
it("info", () => {
logger.info("hello %s world", "foo", "bar"); // hello foo world bar
logger.info("hello %s %s world", "foo", "bar"); // hello foo bar world
logger.info("hello %s %s %s world", "foo", "bar"); // hello foo bar %s world
});
it("warn", () => {
logger.warn("hello %s world", "foo", "bar"); // hello foo world bar
logger.warn("hello %s %s world", "foo", "bar"); // hello foo bar world
logger.warn("hello %s %s %s world", "foo", "bar"); // hello foo bar %s world
});
it("error", () => {
logger.error("hello %s world", "foo", "bar"); // hello foo world bar
logger.error("hello %s %s world", "foo", "bar"); // hello foo bar world
logger.error("hello %s %s %s world", "foo", "bar"); // hello foo bar %s world
});
});
});
Loading

0 comments on commit 2527871

Please sign in to comment.