Skip to content

Commit

Permalink
feat: visualise empty string, null and undefined in PrettyLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej committed Nov 15, 2023
1 parent f644e7a commit 385ab30
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-mugs-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-log": patch
---

Visualise empty string, null and undefined messages in PrettyLogger.
11 changes: 10 additions & 1 deletion examples/example-logging-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export const exampleEffect = pipe(
Effect.tap(() => Effect.logDebug("Sooo sad, not annotations for me")),
Effect.tap(() => Effect.logTrace("Never Gonna Give You Up")),
Effect.provide(Logger.minimumLogLevel(LogLevel.All)),
Effect.flatMap(() => Effect.dieMessage("Aaaand it's gone")),
Effect.tapErrorCause(Effect.logError),
Effect.flatMap(() =>
pipe(Effect.log(""), Effect.annotateLogs("likes", "fp-ts")),
),
Effect.flatMap(() =>
pipe(Effect.log(undefined), Effect.annotateLogs("likes", "fp-ts")),
),
Effect.flatMap(() =>
pipe(Effect.log(null), Effect.annotateLogs("likes", "fp-ts")),
),
Effect.flatMap(() => Effect.dieMessage("Aaaand it's gone")),
);
13 changes: 12 additions & 1 deletion src/PrettyLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ const createLogLevelString = (logLevel: LogLevel.LogLevel) => {
return `${logLevelColor}${logLevelText}${RESET}`;
};

const messageText = (message: unknown) => {
if (message === undefined) {
return `${DIM}undefined${RESET}`;
} else if (message === null) {
return `${DIM}null${RESET}`;
} else if (message === "") {
return `${DIM}<empty message>${RESET}`;
}
return serializeUnknown(message);
};

const createText = (message: unknown, cause: Cause.Cause<unknown>) =>
pipe(
[createCauseMessage(cause), serializeUnknown(message)],
[createCauseMessage(cause), messageText(message)],
ReadonlyArray.filter((i) => i !== ""),
ReadonlyArray.join(" "),
);
Expand Down

0 comments on commit 385ab30

Please sign in to comment.