Skip to content

Commit

Permalink
add: disable color middleware based on color support of stdout and st…
Browse files Browse the repository at this point in the history
…derr
  • Loading branch information
Mitsunee committed Nov 24, 2024
1 parent 71d78b6 commit ecdb72a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
15 changes: 11 additions & 4 deletions src/formatName.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { padStr } from "./padStr";
import type { ResolvedLevelOpts } from "./types";

const colorSupport = {
stdout: process.stdout?.hasColors() ?? false,
stderr: process.stderr?.hasColors() ?? false
};

function formatNameVar<Name extends string = string>(
str: string,
level: ResolvedLevelOpts<Name>
level: ResolvedLevelOpts<Name>,
supportsColor: boolean
): string {
let direction: null | "left" | "center" | "right" = null;
const strInner = str.replace(/%|#/g, "");
Expand All @@ -26,7 +32,7 @@ function formatNameVar<Name extends string = string>(
}

// use color middleware if set and mode is "name"
if (level.color && level.colorMode == "name") {
if (supportsColor && level.color && level.colorMode == "name") {
output = level.color(output);
}

Expand All @@ -44,12 +50,13 @@ function formatNameVar<Name extends string = string>(
export function formatName<Name extends string = string>(
level: ResolvedLevelOpts<Name>
) {
const supportsColor = colorSupport[level.type == "log" ? "stdout" : "stderr"];
let prefix = level.template.template.replace(/%#?name#?%/gi, str =>
formatNameVar(str, level)
formatNameVar(str, level, supportsColor)
);

// use color middleware if set and mode is "full"
if (level.color && level.colorMode == "full") {
if (supportsColor && level.color && level.colorMode == "full") {
prefix = level.color(prefix);
}

Expand Down
14 changes: 5 additions & 9 deletions the-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,16 @@ aka prevented pre-release feature creep :)

- Any objects passed should either be recreated or frozen to prevent changes
- opts for `node:util.inspect` should likely just be sealed/frozen?❓
- Should detect color support (see https://github.com/alexeyraspopov/picocolors/issues/85)
- `FORCE_COLOR` env cannot be 0
- `NO_COLOR` cannot be set
- unsure about `CI`

## Env Variables

Environment Variables can be used to override settings passed to `createLogger`:

| var | Description |
| :---------- | :------------------------------ |
| `LOG_LEVEL` | Overrides the default log level |

TODO: see misc notes about standardized color env vars
| var | Description |
| :------------ | :--------------------------------------------- |
| `LOG_LEVEL` | Overrides the default log level |
| `NO_COLOR` | Setting this disables color middleware |
| `FORCE_COLOR` | Settings this to `0` disables color middleware |

## API

Expand Down

0 comments on commit ecdb72a

Please sign in to comment.