Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Use console in default loggers (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored Aug 27, 2023
1 parent 29ce96d commit 5329ef0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-balloons-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/io": minor
---

Use Console for default Logger
31 changes: 31 additions & 0 deletions docs/modules/Console.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Added in v1.0.0
- [withConsole](#withconsole)
- [model](#model)
- [Console (interface)](#console-interface)
- [UnsafeConsole (interface)](#unsafeconsole-interface)
- [type ids](#type-ids)
- [TypeId](#typeid)
- [TypeId (type alias)](#typeid-type-alias)
Expand Down Expand Up @@ -295,6 +296,36 @@ export interface Console {
timeLog(label?: string, ...args: ReadonlyArray<any>): Effect<never, never, void>
trace(...args: ReadonlyArray<any>): Effect<never, never, void>
warn(...args: ReadonlyArray<any>): Effect<never, never, void>
readonly unsafe: UnsafeConsole
}
```

Added in v1.0.0

## UnsafeConsole (interface)

**Signature**

```ts
export interface UnsafeConsole {
assert(condition: boolean, ...args: ReadonlyArray<any>): void
clear(): void
count(label?: string): void
countReset(label?: string): void
debug(...args: ReadonlyArray<any>): void
dir(item: any, options?: any): void
dirxml(...args: ReadonlyArray<any>): void
error(...args: ReadonlyArray<any>): void
group(options?: { readonly label?: string; readonly collapsed?: boolean }): void
groupEnd(): void
info(...args: ReadonlyArray<any>): void
log(...args: ReadonlyArray<any>): void
table(tabularData: any, properties?: ReadonlyArray<string>): void
time(label?: string): void
timeEnd(label?: string): void
timeLog(label?: string, ...args: ReadonlyArray<any>): void
trace(...args: ReadonlyArray<any>): void
warn(...args: ReadonlyArray<any>): void
}
```

Expand Down
29 changes: 29 additions & 0 deletions src/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ export interface Console {
timeLog(label?: string, ...args: ReadonlyArray<any>): Effect<never, never, void>
trace(...args: ReadonlyArray<any>): Effect<never, never, void>
warn(...args: ReadonlyArray<any>): Effect<never, never, void>
readonly unsafe: UnsafeConsole
}

/**
* @since 1.0.0
* @category model
*/
export interface UnsafeConsole {
assert(condition: boolean, ...args: ReadonlyArray<any>): void
clear(): void
count(label?: string): void
countReset(label?: string): void
debug(...args: ReadonlyArray<any>): void
dir(item: any, options?: any): void
dirxml(...args: ReadonlyArray<any>): void
error(...args: ReadonlyArray<any>): void
group(options?: {
readonly label?: string
readonly collapsed?: boolean
}): void
groupEnd(): void
info(...args: ReadonlyArray<any>): void
log(...args: ReadonlyArray<any>): void
table(tabularData: any, properties?: ReadonlyArray<string>): void
time(label?: string): void
timeEnd(label?: string): void
timeLog(label?: string, ...args: ReadonlyArray<any>): void
trace(...args: ReadonlyArray<any>): void
warn(...args: ReadonlyArray<any>): void
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/internal/defaultServices/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ export const defaultConsole: Console.Console = {
return core.sync(() => {
console.warn(...args)
})
}
},
unsafe: console
}
16 changes: 12 additions & 4 deletions src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type * as RuntimeFlags from "@effect/io/Fiber/Runtime/Flags"
import * as RuntimeFlagsPatch from "@effect/io/Fiber/Runtime/Flags/Patch"
import * as FiberStatus from "@effect/io/Fiber/Status"
import type * as FiberRef from "@effect/io/FiberRef"
import type * as FiberRefs from "@effect/io/FiberRefs"
import * as FiberRefs from "@effect/io/FiberRefs"
import * as FiberRefsPatch from "@effect/io/FiberRefs/Patch"
import * as _RequestBlock from "@effect/io/internal/blockedRequests"
import * as internalCause from "@effect/io/internal/cause"
Expand All @@ -35,6 +35,7 @@ import * as concurrency from "@effect/io/internal/concurrency"
import { configProviderTag } from "@effect/io/internal/configProvider"
import * as core from "@effect/io/internal/core"
import * as defaultServices from "@effect/io/internal/defaultServices"
import { consoleTag } from "@effect/io/internal/defaultServices/console"
import * as internalEffect from "@effect/io/internal/effect"
import * as executionStrategy from "@effect/io/internal/executionStrategy"
import * as internalFiber from "@effect/io/internal/fiber"
Expand Down Expand Up @@ -1331,22 +1332,29 @@ export const currentMinimumLogLevel: FiberRef.FiberRef<LogLevel.LogLevel> = core
LogLevel.fromLiteral("Info")
)

/** @internal */
export const getConsole = (refs: FiberRefs.FiberRefs) => {
const defaultServicesValue = FiberRefs.getOrDefault(refs, defaultServices.currentServices)
const cnsl = Context.get(defaultServicesValue, consoleTag)
return cnsl.unsafe
}

/** @internal */
export const defaultLogger: Logger<unknown, void> = internalLogger.makeLogger((options) => {
const formatted = internalLogger.stringLogger.log(options)
globalThis.console.log(formatted)
getConsole(options.context).log(formatted)
})

/** @internal */
export const filterMinimumLogLevel: Logger<unknown, void> = internalLogger.makeLogger((options) => {
const formatted = internalLogger.stringLogger.log(options)
globalThis.console.log(formatted)
getConsole(options.context).log(formatted)
})

/** @internal */
export const logFmtLogger: Logger<unknown, void> = internalLogger.makeLogger((options) => {
const formatted = internalLogger.logfmtLogger.log(options)
globalThis.console.log(formatted)
getConsole(options.context).log(formatted)
})

/** @internal */
Expand Down

0 comments on commit 5329ef0

Please sign in to comment.