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

chore: effect-log.PrettyLogger -> Effect.Logger.pretty #997

Merged
merged 10 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/young-cars-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"uploadthing": minor
---

feat: allow different log formats

You can now set any of the [built-in log formats](https://effect.website/docs/guides/observability/logging#built-in-loggers) by passing in the `logFormat` config option.
9 changes: 9 additions & 0 deletions docs/src/app/(docs)/api-reference/server/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ Environment variables follows the naming convention of `UPLOADTHING_<NAME>`
Enable more verbose logging.
<Note>If using an older version of the SDK, levels might vary.</Note>
</Property>
<Property
name="logFormat"
type="json | logFmt | structured | pretty"
since="7.1"
defaultValue="pretty in development, else json"
>
What format log entries should be in. [Read more about the log formats
here](https://effect.website/docs/guides/observability/logging#built-in-loggers).
</Property>
<Property
name="isDev"
type="boolean"
Expand Down
9 changes: 9 additions & 0 deletions docs/src/app/(docs)/api-reference/ut-api/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ Environment variables follows the naming convention of `UPLOADTHING_<NAME>`
Enable more verbose logging.
<Note>If using an older version of the SDK, levels might vary.</Note>
</Property>
<Property
name="logFormat"
type="json | logFmt | structured | pretty"
since="7.1"
defaultValue="pretty in development, else json"
>
What format log entries should be in. [Read more about the log formats
here](https://effect.website/docs/guides/observability/logging#built-in-loggers).
</Property>
<Property
name="defaultKeyType"
type="'fileKey' | 'customId'"
Expand Down
1 change: 1 addition & 0 deletions examples/minimal-appdir/src/app/api/uploadthing/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const { GET, POST } = createRouteHandler({
router: uploadRouter,
config: {
logLevel: "Debug",
logFormat: "json",
// handleDaemonPromise: "await",
},
});
3 changes: 1 addition & 2 deletions packages/uploadthing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@
"@effect/schema": "0.72.2",
"@uploadthing/mime-types": "workspace:*",
"@uploadthing/shared": "workspace:*",
"effect": "3.7.2",
"effect-log": "0.32.0"
"effect": "3.7.2"
},
"devDependencies": {
"@remix-run/server-runtime": "^2.12.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/uploadthing/src/internal/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
HttpServerResponse,
} from "@effect/platform";
import * as S from "@effect/schema/Schema";
import { PrettyLogger } from "effect-log";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
Expand All @@ -32,7 +31,7 @@ import * as pkgJson from "../../package.json";
import { configProvider, IngestUrl, IsDevelopment, UTToken } from "./config";
import { formatError } from "./error-formatter";
import { handleJsonLineStream } from "./jsonl";
import { withMinimalLogLevel } from "./logger";
import { withLogFormat, withMinimalLogLevel } from "./logger";
import { getParseFn } from "./parser";
import { assertFilesMeetConfig, extractRouterConfig } from "./route-config";
import {
Expand Down Expand Up @@ -69,7 +68,7 @@ export const makeAdapterHandler = <Args extends any[]>(
): ((...args: Args) => Promise<Response>) => {
const layer = Layer.provide(
Layer.mergeAll(
PrettyLogger.layer({ showFiberId: false }),
withLogFormat,
withMinimalLogLevel,
HttpClient.layer,
Layer.succeed(
Expand Down
28 changes: 28 additions & 0 deletions packages/uploadthing/src/internal/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as LogLevel from "effect/LogLevel";

import { UploadThingError } from "@uploadthing/shared";

import { IsDevelopment } from "./config";

export const withMinimalLogLevel = Config.logLevel("logLevel").pipe(
Config.withDefault(LogLevel.Info),
Effect.andThen((level) => Logger.minimumLogLevel(level)),
Expand All @@ -23,3 +25,29 @@ export const withMinimalLogLevel = Config.logLevel("logLevel").pipe(
),
Layer.unwrapEffect,
);

export const LogFormat = Config.literal(
"json",
"logFmt",
"structured",
"pretty",
)("logFormat");

export const withLogFormat = Effect.gen(function* () {
const isDev = yield* IsDevelopment;
const logFormat = yield* LogFormat.pipe(
Config.withDefault(isDev ? "pretty" : "json"),
);
return Logger[logFormat];
}).pipe(
Effect.catchTag(
"ConfigError",
(e) =>
new UploadThingError({
code: "INVALID_SERVER_CONFIG",
message: "Invalid server configuration",
cause: e,
}),
),
Layer.unwrapEffect,
);
8 changes: 8 additions & 0 deletions packages/uploadthing/src/internal/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Schema } from "@effect/schema/Schema";
import type * as Config from "effect/Config";
import type * as LogLevel from "effect/LogLevel";

import type {
Expand All @@ -13,6 +14,7 @@ import type {
UploadThingError,
} from "@uploadthing/shared";

import type { LogFormat } from "./logger";
import type { JsonParser } from "./parser";
import type {
FileUploadDataWithCustomId,
Expand Down Expand Up @@ -173,6 +175,12 @@ export type FileRouter<TParams extends AnyParams = AnyParams> = Record<

export type RouteHandlerConfig = {
logLevel?: LogLevel.Literal;
/**
* What format log entries should be in
* @default "pretty" in development, else "json"
* @see https://effect.website/docs/guides/observability/logging#built-in-loggers
*/
logFormat?: Config.Config.Success<typeof LogFormat>;
callbackUrl?: string;
token?: string;
/**
Expand Down
24 changes: 12 additions & 12 deletions packages/uploadthing/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
HttpClientResponse,
} from "@effect/platform";
import * as S from "@effect/schema/Schema";
import { PrettyLogger } from "effect-log";
import * as Arr from "effect/Array";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
Expand All @@ -28,7 +27,7 @@ import {
UPLOADTHING_VERSION,
UTToken,
} from "../internal/config";
import { withMinimalLogLevel } from "../internal/logger";
import { withLogFormat, withMinimalLogLevel } from "../internal/logger";
import type {
ACLUpdateOptions,
DeleteFilesOptions,
Expand Down Expand Up @@ -96,17 +95,18 @@ export class UTApi {
program: Effect.Effect<A, E, HttpClient.HttpClient.Default>,
signal?: AbortSignal,
) => {
return program.pipe(
Effect.provide(PrettyLogger.layer({ showFiberId: false })),
Effect.provide(withMinimalLogLevel),
Effect.provide(HttpClient.layer),
Effect.provide(
Layer.effect(
HttpClient.Fetch,
Effect.succeed(this.fetch as typeof globalThis.fetch),
),
const layer = Layer.provide(
Layer.mergeAll(
withLogFormat,
withMinimalLogLevel,
HttpClient.layer,
Layer.succeed(HttpClient.Fetch, this.fetch as typeof globalThis.fetch),
),
Effect.provide(Layer.setConfigProvider(configProvider(this.opts))),
Layer.setConfigProvider(configProvider(this.opts)),
);

return program.pipe(
Effect.provide(layer),
Effect.withLogSpan("utapi.#executeAsync"),
(e) => Effect.runPromise(e, signal ? { signal } : undefined),
);
Expand Down
8 changes: 8 additions & 0 deletions packages/uploadthing/src/sdk/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-empty-interface */

import type { Blob as NodeBlob } from "buffer";
import type * as Config from "effect/Config";
import type * as LogLevel from "effect/LogLevel";

import type {
Expand All @@ -13,6 +14,7 @@ import type {
Time,
} from "@uploadthing/shared";

import type { LogFormat } from "../internal/logger";
import type { UploadedFileData } from "../types";

export interface UTApiOptions {
Expand All @@ -30,6 +32,12 @@ export interface UTApiOptions {
* @default "info"
*/
logLevel?: LogLevel.Literal;
/**
* What format log entries should be in
* @default "pretty" in development, else "json"
* @see https://effect.website/docs/guides/observability/logging#built-in-loggers
*/
logFormat?: Config.Config.Success<typeof LogFormat>;
/**
* Set the default key type for file operations. Allows you to set your preferred filter
* for file keys or custom identifiers without needing to specify it on every call.
Expand Down
Loading
Loading