Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/logging.ts: Add support for logging #308

Merged
merged 18 commits into from
Dec 10, 2024
Merged

util/logging.ts: Add support for logging #308

merged 18 commits into from
Dec 10, 2024

Conversation

findgriffin
Copy link
Contributor

@findgriffin findgriffin commented Nov 21, 2024

util/logging.ts: Add LogHandler interface, LogLevel enum, and ConsoleLogHandler.

Description

Add a log handler interface and implementation.

Motivation and context

Following a LSE where we couldn't get reliable information from the customer side, we decided to add a logging facility to all of the drivers.

I spent a few hours surveying log library options in JavaScript/TypeScript. The conclusion that I came to is that they are either bulkier than what we need (log4js-node) or so simple that it's not worth us taking a dependency (pino), hence the strategy I adopted in this PR.

TODO: This PR does not yet actually call our log function from the request/response path, I thought the tests were failing on Monday. They appear to pass in GitHub Actions, so maybe that's a "fails on my machine" type of thing.

How was the change tested?

Unit tests?

Screenshots (if appropriate):

Change types

    • Bug fix (non-breaking change that fixes an issue)
    • New feature (non-breaking change that adds functionality)
    • Breaking change (backwards-incompatible fix or feature)

Checklist:

    • My code follows the code style of this project.
    • My change requires a change to Fauna documentation.
    • My change requires a change to the README, and I have updated it accordingly.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ecooper ecooper requested review from ptpaterson and a team November 25, 2024 17:20
Copy link
Contributor

@ptpaterson ptpaterson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger looks good, but I don't think we can configure it correctly.

We should also have some unit tests

src/client.ts Outdated
Comment on lines 566 to 591
#getLogger(partialClientConfig?: ClientConfiguration): LogHandler {
if (
partialClientConfig &&
"logger" in partialClientConfig &&
partialClientConfig.logger === undefined
) {
throw new TypeError(`ClientConfiguration option logger must be defined.`);
}
if (
typeof process != "undefined" &&
process &&
typeof process === "object" &&
process.env &&
typeof process.env === "object"
) {
if (faunaDebugEnabled(process.env["FAUNA_DEBUG"])) {
return new ConsoleLogHandler(LogLevel.DEBUG);
}
}
return new ConsoleLogHandler(LogLevel.ERROR);
}
Copy link
Contributor

@ptpaterson ptpaterson Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything that the user provides in the logger option is ignored, other than checking that it is undefined. We should return the explicitly given object if provided and fall back on the env variables

Suggested change
#getLogger(partialClientConfig?: ClientConfiguration): LogHandler {
if (
partialClientConfig &&
"logger" in partialClientConfig &&
partialClientConfig.logger === undefined
) {
throw new TypeError(`ClientConfiguration option logger must be defined.`);
}
if (
typeof process != "undefined" &&
process &&
typeof process === "object" &&
process.env &&
typeof process.env === "object"
) {
if (faunaDebugEnabled(process.env["FAUNA_DEBUG"])) {
return new ConsoleLogHandler(LogLevel.DEBUG);
}
}
return new ConsoleLogHandler(LogLevel.ERROR);
}
#getLogger(partialClientConfig?: ClientConfiguration): LogHandler {
if (
partialClientConfig &&
"logger" in partialClientConfig &&
partialClientConfig.logger === undefined
) {
throw new TypeError(`ClientConfiguration option logger must be defined.`);
}
if (partialClientConfig?.logger) {
return partialClientConfig.logger;
}
if (
typeof process !== "undefined" &&
process &&
typeof process === "object" &&
process.env &&
typeof process.env === "object"
) {
const env_debug = parseDebugLevel(process.env["FAUNA_DEBUG"]);
return new ConsoleLogHandler(env_debug);
}
return new ConsoleLogHandler(LOG_LEVELS.ERROR);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also want to pass in the log level provided to the ConsoleLogHandler, rather than hardcoding DEBUG and ERROR.

}

export class ConsoleLogHandler implements LogHandler {
constructor(private readonly level: LogLevel) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer native private class properties over typescript magic ones. Declaring the field above the constructor will also ensure at a type level that it is assigned (which is currently not guaranteed).

Suggested change
constructor(private readonly level: LogLevel) {
readonly #level: LogLevel;
constructor(level: LogLevel) {

Comment on lines 44 to 54
if (level === null) {
this.level = LogLevel.ERROR;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should level be set if level is not provided or is null?

if (fauna_debug.length == 0) {
return false;
} else {
if (fauna_debug.startsWith("0")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If FAUNA_DEBUG is set to "0", e.g. with FAUNA_DEBUG=0, then how does one configure a log level of TRACE?

@@ -0,0 +1,77 @@
export enum LogLevel {
Copy link
Contributor

@ptpaterson ptpaterson Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer a constant and companion union type over enum magic. We can reuse the values if they are strings (e.g. TRACE = "0" instead of TRACE = 0), but string enums have type safety issues.

@ptpaterson ptpaterson self-requested a review November 26, 2024 20:54
@ptpaterson
Copy link
Contributor

We should put together some tests that demonstrate using logger stuff. But I've updated the things I have opinions and commented about.

@findgriffin findgriffin requested review from a team and removed request for a team December 4, 2024 20:27
src/client.ts Outdated Show resolved Hide resolved
@ptpaterson ptpaterson requested a review from ecooper December 6, 2024 15:44
@ptpaterson ptpaterson marked this pull request as ready for review December 6, 2024 16:10
@ptpaterson ptpaterson dismissed their stale review December 6, 2024 16:11

implemented suggestions.

src/client.ts Outdated
Comment on lines 138 to 145
logger: this.#getLogger(clientConfiguration),
};

if (clientConfiguration && clientConfiguration.logger) {
this.#logger = clientConfiguration.logger;
} else {
this.#logger = new ConsoleLogHandler(LOG_LEVELS.ERROR);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getLogger returns a logger that's turned off by default (sounds good). But then we set a logger on the client itself at error (sounds bad)? Am I reading that right?

By default we want zero logging to the console.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I missed that when adding the extra log levels. It ought to be OFF.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made logger a required configuration and updated calls to use that instead of keeping a copy in two places.

@ptpaterson ptpaterson requested a review from ecooper December 9, 2024 22:50
@ptpaterson ptpaterson merged commit 2527871 into main Dec 10, 2024
7 checks passed
@ptpaterson ptpaterson deleted the BT-5155-logging branch December 10, 2024 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants