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

Commit

Permalink
accept unknown as input for Effect.log
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jul 25, 2023
1 parent bdaad78 commit b330a1d
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-needles-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/io": minor
---

accept unknown as input for Effect.log\*
6 changes: 3 additions & 3 deletions examples/logMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ type LogMeta = Record<string, string>

const logMeta = FiberRef.unsafeMake<LogMeta>({})

const logInfoWithMeta = (message: string, data: LogMeta) => Effect.locally(logMeta, data)(Effect.logInfo(message))
const logInfoWithMeta = (message: unknown, data: LogMeta) => Effect.locally(logMeta, data)(Effect.logInfo(message))

const customLogger = Logger.make<string, void>((options) => {
const customLogger = Logger.make<unknown, void>((options) => {
const meta = FiberRefs.getOrDefault(options.context, logMeta)
const formatted = Logger.stringLogger.log(options)
console.log(formatted, { meta })
})

const program = Effect.gen(function*($) {
yield* $(logInfoWithMeta("message1", { foo: "bar" }))
yield* $(logInfoWithMeta({ msg: "message1" }, { foo: "bar" }))
})

const main = pipe(
Expand Down
28 changes: 14 additions & 14 deletions src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4138,9 +4138,9 @@ export const matchEffect: {
* @since 1.0.0
* @category logging
*/
export const log: <A extends string | Cause.Cause<unknown>>(
export const log: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.log

/**
Expand All @@ -4149,9 +4149,9 @@ export const log: <A extends string | Cause.Cause<unknown>>(
* @since 1.0.0
* @category logging
*/
export const logTrace: <A extends string | Cause.Cause<unknown>>(
export const logTrace: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.logTrace

/**
Expand All @@ -4160,9 +4160,9 @@ export const logTrace: <A extends string | Cause.Cause<unknown>>(
* @since 1.0.0
* @category logging
*/
export const logDebug: <A extends string | Cause.Cause<unknown>>(
export const logDebug: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.logDebug

/**
Expand All @@ -4171,9 +4171,9 @@ export const logDebug: <A extends string | Cause.Cause<unknown>>(
* @since 1.0.0
* @category logging
*/
export const logInfo: <A extends string | Cause.Cause<unknown>>(
export const logInfo: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.logInfo

/**
Expand All @@ -4182,9 +4182,9 @@ export const logInfo: <A extends string | Cause.Cause<unknown>>(
* @since 1.0.0
* @category logging
*/
export const logWarning: <A extends string | Cause.Cause<unknown>>(
export const logWarning: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.logWarning

/**
Expand All @@ -4193,9 +4193,9 @@ export const logWarning: <A extends string | Cause.Cause<unknown>>(
* @since 1.0.0
* @category logging
*/
export const logError: <A extends string | Cause.Cause<unknown>>(
export const logError: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.logError

/**
Expand All @@ -4204,9 +4204,9 @@ export const logError: <A extends string | Cause.Cause<unknown>>(
* @since 1.0.0
* @category logging
*/
export const logFatal: <A extends string | Cause.Cause<unknown>>(
export const logFatal: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect<never, never, void> = effect.logFatal

/**
Expand Down
2 changes: 1 addition & 1 deletion src/FiberRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export const currentLogAnnotations: FiberRef<HashMap.HashMap<string, string>> =
* @since 1.0.0
* @category fiberRefs
*/
export const currentLoggers: FiberRef<HashSet.HashSet<Logger.Logger<string, any>>> = fiberRuntime.currentLoggers
export const currentLoggers: FiberRef<HashSet.HashSet<Logger.Logger<unknown, any>>> = fiberRuntime.currentLoggers

/**
* @since 1.0.0
Expand Down
32 changes: 16 additions & 16 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ export const make: <Message, Output>(
* @since 1.0.0
* @category context
*/
export const add: <B>(logger: Logger<string, B>) => Layer.Layer<never, never, never> = circular.addLogger
export const add: <B>(logger: Logger<unknown, B>) => Layer.Layer<never, never, never> = circular.addLogger

/**
* @since 1.0.0
* @category context
*/
export const addEffect: <R, E, A>(effect: Effect<R, E, Logger<string, A>>) => Layer.Layer<R, E, never> =
export const addEffect: <R, E, A>(effect: Effect<R, E, Logger<unknown, A>>) => Layer.Layer<R, E, never> =
circular.addLoggerEffect

/**
* @since 1.0.0
* @category context
*/
export const addScoped: <R, E, A>(
effect: Effect<R | Scope, E, Logger<string, A>>
effect: Effect<R | Scope, E, Logger<unknown, A>>
) => Layer.Layer<Exclude<R, Scope>, E, never> = circular.addLoggerScoped

/**
Expand Down Expand Up @@ -163,24 +163,24 @@ export const none: Logger<unknown, void> = internal.none
* @since 1.0.0
* @category context
*/
export const remove: <A>(logger: Logger<string, A>) => Layer.Layer<never, never, never> = circular.removeLogger
export const remove: <A>(logger: Logger<unknown, A>) => Layer.Layer<never, never, never> = circular.removeLogger

/**
* @since 1.0.0
* @category context
*/
export const replace: {
<B>(that: Logger<string, B>): <A>(self: Logger<string, A>) => Layer.Layer<never, never, never>
<A, B>(self: Logger<string, A>, that: Logger<string, B>): Layer.Layer<never, never, never>
<B>(that: Logger<unknown, B>): <A>(self: Logger<unknown, A>) => Layer.Layer<never, never, never>
<A, B>(self: Logger<unknown, A>, that: Logger<unknown, B>): Layer.Layer<never, never, never>
} = circular.replaceLogger

/**
* @since 1.0.0
* @category context
*/
export const replaceEffect: {
<R, E, B>(that: Effect<R, E, Logger<string, B>>): <A>(self: Logger<string, A>) => Layer.Layer<R, E, never>
<A, R, E, B>(self: Logger<string, A>, that: Effect<R, E, Logger<string, B>>): Layer.Layer<R, E, never>
<R, E, B>(that: Effect<R, E, Logger<unknown, B>>): <A>(self: Logger<unknown, A>) => Layer.Layer<R, E, never>
<A, R, E, B>(self: Logger<unknown, A>, that: Effect<R, E, Logger<unknown, B>>): Layer.Layer<R, E, never>
} = circular.replaceLoggerEffect

/**
Expand All @@ -189,11 +189,11 @@ export const replaceEffect: {
*/
export const replaceScoped: {
<R, E, B>(
that: Effect<Scope | R, E, Logger<string, B>>
): <A>(self: Logger<string, A>) => Layer.Layer<Exclude<R, Scope>, E, never>
that: Effect<Scope | R, E, Logger<unknown, B>>
): <A>(self: Logger<unknown, A>) => Layer.Layer<Exclude<R, Scope>, E, never>
<A, R, E, B>(
self: Logger<string, A>,
that: Effect<Scope | R, E, Logger<string, B>>
self: Logger<unknown, A>,
that: Effect<Scope | R, E, Logger<unknown, B>>
): Layer.Layer<Exclude<R, Scope>, E, never>
} = circular.replaceLoggerScoped

Expand Down Expand Up @@ -282,25 +282,25 @@ export const zipRight: {
* @since 1.0.0
* @category constructors
*/
export const defaultLogger: Logger<string, void> = fiberRuntime.defaultLogger
export const defaultLogger: Logger<unknown, void> = fiberRuntime.defaultLogger

/**
* @since 1.0.0
* @category constructors
*/
export const logfmtLogger: Logger<string, string> = internal.logfmtLogger
export const logfmtLogger: Logger<unknown, string> = internal.logfmtLogger

/**
* @since 1.0.0
* @category constructors
*/
export const stringLogger: Logger<string, string> = internal.stringLogger
export const stringLogger: Logger<unknown, string> = internal.stringLogger

/**
* @since 1.0.0
* @category constructors
*/
export const tracerLogger: Logger<string, void> = fiberRuntime.tracerLogger
export const tracerLogger: Logger<unknown, void> = fiberRuntime.tracerLogger

/**
* @since 1.0.0
Expand Down
42 changes: 21 additions & 21 deletions src/internal/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,19 +862,19 @@ export const iterate = <Z, R, E>(
})

const logWithLevel = (level?: LogLevel.LogLevel) =>
<A extends string | Cause.Cause<unknown>>(
<A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
): Effect.Effect<never, never, void> => {
const levelOption = Option.fromNullable(level)
let message: string
let message: unknown
let cause: Cause.Cause<unknown>
if (typeof messageOrCause === "string") {
if (internalCause.isCause(messageOrCause)) {
cause = messageOrCause
message = (supplementry as unknown) ?? ""
} else {
message = messageOrCause
cause = (supplementry as Cause.Cause<unknown>) ?? internalCause.empty
} else {
cause = messageOrCause
message = (supplementry as string) ?? ""
}
return core.withFiberRuntime<never, never, void>((fiberState) => {
fiberState.log(message, cause, levelOption)
Expand All @@ -883,45 +883,45 @@ const logWithLevel = (level?: LogLevel.LogLevel) =>
}

/** @internal */
export const log: <A extends string | Cause.Cause<unknown>>(
export const log: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel()

/** @internal */
export const logTrace: <A extends string | Cause.Cause<unknown>>(
export const logTrace: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel(LogLevel.Trace)

/** @internal */
export const logDebug: <A extends string | Cause.Cause<unknown>>(
export const logDebug: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel(LogLevel.Debug)

/** @internal */
export const logInfo: <A extends string | Cause.Cause<unknown>>(
export const logInfo: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel(LogLevel.Info)

/** @internal */
export const logWarning: <A extends string | Cause.Cause<unknown>>(
export const logWarning: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel(LogLevel.Warning)

/** @internal */
export const logError: <A extends string | Cause.Cause<unknown>>(
export const logError: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel(LogLevel.Error)

/** @internal */
export const logFatal: <A extends string | Cause.Cause<unknown>>(
export const logFatal: <A>(
messageOrCause: A,
supplementry?: A extends string ? Cause.Cause<unknown> : string
supplementry?: A extends Cause.Cause<any> ? unknown : Cause.Cause<unknown>
) => Effect.Effect<never, never, void> = logWithLevel(LogLevel.Fatal)

/* @internal */
Expand Down
14 changes: 7 additions & 7 deletions src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ export class FiberRuntime<E, A> implements Fiber.RuntimeFiber<E, A> {
}

log(
message: string,
message: unknown,
cause: Cause.Cause<any>,
overrideLogLevel: Option.Option<LogLevel.LogLevel>
): void {
Expand Down Expand Up @@ -1316,25 +1316,25 @@ export const currentMinimumLogLevel: FiberRef.FiberRef<LogLevel.LogLevel> = core
)

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

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

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

/** @internal */
export const tracerLogger = internalLogger.makeLogger<string, void>(({
export const tracerLogger = internalLogger.makeLogger<unknown, void>(({
annotations,
cause,
context,
Expand All @@ -1360,15 +1360,15 @@ export const tracerLogger = internalLogger.makeLogger<string, void>(({
}

span.value.event(
message,
String(message),
clockService.value.unsafeCurrentTimeNanos(),
attributes
)
})

/** @internal */
export const currentLoggers: FiberRef.FiberRef<
HashSet.HashSet<Logger<string, any>>
HashSet.HashSet<Logger<unknown, any>>
> = core.fiberRefUnsafeMakeHashSet(HashSet.make(defaultLogger, tracerLogger))

// circular with Effect
Expand Down
Loading

0 comments on commit b330a1d

Please sign in to comment.