diff --git a/src/formatName.ts b/src/formatName.ts index 25a5143..b9b8081 100644 --- a/src/formatName.ts +++ b/src/formatName.ts @@ -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( str: string, - level: ResolvedLevelOpts + level: ResolvedLevelOpts, + supportsColor: boolean ): string { let direction: null | "left" | "center" | "right" = null; const strInner = str.replace(/%|#/g, ""); @@ -26,7 +32,7 @@ function formatNameVar( } // 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); } @@ -44,12 +50,13 @@ function formatNameVar( export function formatName( level: ResolvedLevelOpts ) { + 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); } diff --git a/the-plan.md b/the-plan.md index c4d5107..a13ffba 100644 --- a/the-plan.md +++ b/the-plan.md @@ -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