Skip to content

Commit

Permalink
improve output on browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jul 3, 2024
1 parent d32caa8 commit cd23977
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-zoos-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix types of UnsafeConsole.group
5 changes: 1 addition & 4 deletions packages/effect/src/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ export interface UnsafeConsole {
dir(item: any, options?: any): void
dirxml(...args: ReadonlyArray<any>): void
error(...args: ReadonlyArray<any>): void
group(options?: {
readonly label?: string | undefined
readonly collapsed?: boolean | undefined
}): void
group(label?: string | undefined): void
groupEnd(): void
info(...args: ReadonlyArray<any>): void
log(...args: ReadonlyArray<any>): void
Expand Down
16 changes: 1 addition & 15 deletions packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,21 +1424,7 @@ export const logFmtLogger: Logger<unknown, void> = globalValue(
/** @internal */
export const prettyLogger: Logger<unknown, void> = globalValue(
Symbol.for("effect/Logger/prettyLogger"),
() => {
const logger = internalLogger.prettyLogger()
return internalLogger.makeLogger((opts) => {
const services = FiberRefs.getOrDefault(opts.context, defaultServices.currentServices)
const console = Context.get(services, consoleTag).unsafe
const groups = logger.log(opts)
console.log(...groups[0])
if (groups.length <= 1) return
for (let i = 1; i < groups.length; i++) {
console.group()
console.log(...groups[i])
console.groupEnd()
}
})
}
() => internalLogger.prettyLogger()
)

/** @internal */
Expand Down
48 changes: 36 additions & 12 deletions packages/effect/src/internal/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as Arr from "../Array.js"
import * as Context from "../Context.js"
import * as FiberRefs from "../FiberRefs.js"
import type { LazyArg } from "../Function.js"
import { constVoid, dual, pipe } from "../Function.js"
import * as HashMap from "../HashMap.js"
Expand All @@ -10,6 +12,8 @@ import * as LogSpan from "../LogSpan.js"
import * as Option from "../Option.js"
import { pipeArguments } from "../Pipeable.js"
import * as Cause from "./cause.js"
import * as defaultServices from "./defaultServices.js"
import { consoleTag } from "./defaultServices/console.js"
import * as _fiberId from "./fiberId.js"

/** @internal */
Expand Down Expand Up @@ -377,7 +381,7 @@ const withColor = (text: string, ...colors: ReadonlyArray<string>) => {
}
const withColorNoop = (text: string, ..._colors: ReadonlyArray<string>) => text
const colors = {
bright: "1",
bold: "1",

red: "31",
green: "32",
Expand Down Expand Up @@ -406,16 +410,18 @@ const logLevelColors: Record<LogLevel.LogLevel["_tag"], ReadonlyArray<string>> =

/** @internal */
export const prettyLogger = (options?: {
readonly colors?: "auto" | boolean
readonly colors?: "auto" | boolean | undefined
readonly stderr?: boolean | undefined
}) =>
makeLogger<unknown, Array<Array<unknown>>>(
({ annotations, cause, date, fiberId, logLevel, message: message_, spans }) => {
makeLogger<unknown, void>(
({ annotations, cause, context, date, fiberId, logLevel, message: message_, spans }) => {
const services = FiberRefs.getOrDefault(context, defaultServices.currentServices)
const console = Context.get(services, consoleTag).unsafe
const log = options?.stderr === true ? console.error : console.log
const showColors = typeof options?.colors === "boolean" ? options.colors : processStdoutIsTTY || hasWindow
const color = showColors ? withColor : withColorNoop

const message = Arr.ensure(message_)
const groups: Array<Array<unknown>> = []
const firstParams: Array<unknown> = []

let firstLine = color(
`[${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}:${
Expand All @@ -438,31 +444,49 @@ export const prettyLogger = (options?: {
if (message.length > 0) {
const firstMaybeString = structuredMessage(message[0])
if (typeof firstMaybeString === "string") {
firstLine += " " + color(firstMaybeString, colors.bright, colors.cyan)
firstLine += " " + color(firstMaybeString, colors.bold, colors.cyan)
messageIndex++
}
}
groups.push([firstLine, ...firstParams])

if (hasWindow) {
console.group(firstLine)
} else {
log(firstLine)
console.group()
}
let currentMessage = ""
const params: Array<unknown> = []

if (!Cause.isEmpty(cause)) {
const errors = Cause.prettyErrors(cause)
for (let i = 0; i < errors.length; i++) {
groups.push(hasWindow ? [errors[i]] : [errors[i].stack])
if (hasWindow) {
console.error(errors[i].stack)
} else {
currentMessage += "\n%s"
params.push(errors[i].stack)
}
}
}

if (messageIndex < message.length) {
for (; messageIndex < message.length; messageIndex++) {
groups.push([message[messageIndex]])
currentMessage += "\n%O"
params.push(message[messageIndex])
}
}

if (HashMap.size(annotations) > 0) {
for (const [key, value] of annotations) {
groups.push([color(`${key}:`, colors.bright, colors.white) + " %O", value])
currentMessage += "\n" + color(`${key}:`, colors.bold, colors.white) + " %O"
params.push(value)
}
}

return groups
if (currentMessage.length > 0) {
log(currentMessage.slice(1), ...params)
}
console.groupEnd()
}
)

0 comments on commit cd23977

Please sign in to comment.