Skip to content

Commit

Permalink
util/logging.ts: Add LogHandler interface, LogLevel enum, and Console…
Browse files Browse the repository at this point in the history
…LogHandler.
  • Loading branch information
David Griffin authored and ptpaterson committed Nov 26, 2024
1 parent a7ffe6d commit 8578593
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/client-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HTTPClient, HTTPStreamClient } from "./http-client";
import type { ValueFormat } from "./wire-protocol";
import { LogHandler } from "./util/logging";

/**
* Configuration for a client. The options provided are used as the
Expand Down Expand Up @@ -66,6 +67,8 @@ export interface ClientConfiguration {
*/
fetch_keepalive?: boolean;

logger?: LogHandler;

/**
* A secret for your Fauna DB, used to authorize your queries.
* @see https://docs.fauna.com/fauna/current/security/keys
Expand Down
29 changes: 29 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ import {
type QueryValue,
FeedError,
} from "./wire-protocol";
import {
ConsoleLogHandler,
faunaDebugEnabled,
LogHandler,
LogLevel,
} from "./util/logging";

type RequiredClientConfig = ClientConfiguration &
Required<
Expand Down Expand Up @@ -126,6 +132,7 @@ export class Client {
...clientConfiguration,
secret: this.#getSecret(clientConfiguration),
endpoint: this.#getEndpoint(clientConfiguration),
logger: this.#getLogger(clientConfiguration),
};

this.#validateConfiguration();
Expand Down Expand Up @@ -556,6 +563,28 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\
return partialClientConfig?.endpoint ?? env_endpoint ?? endpoints.default;
}

#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);
}

async #query<T extends QueryValue>(
queryRequest: QueryRequest,
queryOptions?: QueryOptions,
Expand Down
21 changes: 13 additions & 8 deletions src/http-client/fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@ export class FetchClient implements HTTPClient, HTTPStreamClient {
};

async function* reader() {
const response = await fetch(request, options).catch((error) => {
throw new NetworkError(
"The network connection encountered a problem.",
{
cause: error,
},
);
});
const response = await fetch(request, options)
.then((resp) => {
console.log(resp);
return resp;
})
.catch((error) => {
throw new NetworkError(
"The network connection encountered a problem.",
{
cause: error,
},
);
});
const status = response.status;
if (!(status >= 200 && status < 400)) {
const failure: QueryFailure = await response.json();
Expand Down
77 changes: 77 additions & 0 deletions src/util/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export enum LogLevel {
TRACE = 0,
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4,
}

/**
* Converts the FAUNA_DEBUG environment variable (string) into a LogLevel.
* The intended use is to set FAUNA_DEBUG=0|1.
*
* This function will convert null, undefined, empty, or or any string
* starting with "0" to false.
*
* @param fauna_debug - The String value of FAUNA_DEBUG.
*/
export function faunaDebugEnabled(fauna_debug: string | undefined): boolean {
if (fauna_debug === undefined || fauna_debug === null) {
return false;
} else {
if (fauna_debug.length == 0) {
return false;
} else {
if (fauna_debug.startsWith("0")) {
return false;
} else {
return true;
}
}
}
}

export interface LogHandler {
trace(msg?: string, args?: string[]): void;
debug(msg?: string, args?: string[]): void;
info(msg?: string, args?: string[]): void;
warn(msg?: string, args?: string[]): void;
error(msg?: string, args?: string[]): void;
}

export class ConsoleLogHandler implements LogHandler {
constructor(private readonly level: LogLevel) {
if (level === null) {
this.level = LogLevel.ERROR;
}
}

trace(msg?: string, args?: string[]): void {
if (this.level >= LogLevel.TRACE) {
console.trace(msg, args);
}
}

debug(msg?: string, args?: string[]): void {
if (this.level >= LogLevel.DEBUG) {
console.debug(msg, args);
}
}

info(msg?: string, args?: string[]): void {
if (this.level >= LogLevel.INFO) {
console.info(msg, args);
}
}

warn(msg?: string, args?: string[]): void {
if (this.level >= LogLevel.WARN) {
console.warn(msg, args);
}
}
error(msg?: string, args?: string[]): void {
if (this.level >= LogLevel.ERROR) {
console.error(msg, args);
}
}
}

0 comments on commit 8578593

Please sign in to comment.