MCPcopy Create free account
hub / github.com/Effect-TS/effect / format

Function format

packages/effect/src/Logger.ts:404–440  ·  view source on GitHub ↗
(
  quoteValue: (s: string) => string,
  space?: number | string | undefined
)

Source from the content-addressed store, hash-verified

402 * @internal
403 */
404const format = (
405 quoteValue: (s: string) => string,
406 space?: number | string | undefined
407) =>
408({ cause, date, fiber, logLevel, message }: Options<unknown>): string => {
409 const formatUnknown = (value: unknown): string =>
410 typeof value === "string" ? value : Formatter.format(value, { space })
411 const formatValue = (value: string): string => value.match(textOnly) ? value : quoteValue(value)
412 const format = (label: string, value: string): string => `${effect.formatLabel(label)}=${formatValue(value)}`
413 const append = (label: string, value: string): string => " " + format(label, value)
414
415 let out = format("timestamp", date.toISOString())
416 out += append("level", logLevel.toUpperCase())
417 out += append("fiber", formatFiberId(fiber.id))
418
419 const messages = Array.ensure(message)
420 for (let i = 0; i < messages.length; i++) {
421 out += append("message", formatUnknown(messages[i]))
422 }
423
424 if (cause.reasons.length > 0) {
425 out += append("cause", effect.causePretty(cause))
426 }
427
428 const now = date.getTime()
429 const spans = fiber.getRef(CurrentLogSpans)
430 for (const span of spans) {
431 out += " " + effect.formatLogSpan(span, now)
432 }
433
434 const annotations = fiber.getRef(CurrentLogAnnotations)
435 for (const [label, value] of Object.entries(annotations)) {
436 out += append(label, formatUnknown(value))
437 }
438
439 return out
440}
441
442/**
443 * Creates a new `Logger` from a log function.

Callers 2

appendFunction · 0.70
Logger.tsFile · 0.70

Calls 5

formatFiberIdFunction · 0.85
formatUnknownFunction · 0.85
getRefMethod · 0.80
formatValueFunction · 0.70
appendFunction · 0.70

Tested by

no test coverage detected