Skip to content

Commit

Permalink
Detect environment in Logger.pretty using process.stdout (#3510)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
fubhy and tim-smart authored Aug 29, 2024
1 parent 8e64b1a commit e809286
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-hounds-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Detect environment in Logger.pretty using process.stdout
13 changes: 5 additions & 8 deletions packages/effect/src/internal/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,13 @@ const defaultDateFormat = (date: Date): string =>
date.getSeconds().toString().padStart(2, "0")
}.${date.getMilliseconds().toString().padStart(3, "0")}`

const processStdoutIsTTY = typeof process === "object" &&
const hasProcessStdout = typeof process === "object" &&
process !== null &&
typeof process.stdout === "object" &&
process.stdout !== null &&
process.stdout !== null
const processStdoutIsTTY = hasProcessStdout &&
process.stdout.isTTY === true
const hasWindow = typeof window === "object"
const isWorker = typeof self === "object" &&
self !== null &&
typeof self.constructor === "function" &&
self.constructor.name.includes("Worker")
const hasProcessStdoutOrDeno = hasProcessStdout || "Deno" in globalThis

/** @internal */
export const prettyLogger = (options?: {
Expand All @@ -435,7 +432,7 @@ export const prettyLogger = (options?: {
readonly mode?: "browser" | "tty" | "auto" | undefined
}) => {
const mode_ = options?.mode ?? "auto"
const mode = mode_ === "auto" ? (hasWindow || isWorker ? "browser" : "tty") : mode_
const mode = mode_ === "auto" ? (hasProcessStdoutOrDeno ? "tty" : "browser") : mode_
const isBrowser = mode === "browser"
const showColors = typeof options?.colors === "boolean" ? options.colors : processStdoutIsTTY || isBrowser
const formatDate = options?.formatDate ?? defaultDateFormat
Expand Down

0 comments on commit e809286

Please sign in to comment.