From e1e7c745daea1e36a5e20211f38c8e91a787f83b Mon Sep 17 00:00:00 2001 From: Mitsunee <19474909+Mitsunee@users.noreply.github.com> Date: Wed, 27 Nov 2024 20:52:29 +0100 Subject: [PATCH] fix: force-disable color in util.inspect if unsupported --- src/index.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 46bc9d8..f169e1d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import { inspect } from "util"; -import { resolveLoggerOpts } from "./resolveLoggerOpts"; import type { LogFn, LoggerOpts, ResolvedLevelOpts } from "./types"; -import { formatName } from "./formatName"; +import { formatName, colorSupport } from "./formatName"; import { formatTime } from "./formatTime"; +import { resolveLoggerOpts } from "./resolveLoggerOpts"; export function createLogger(opts: LoggerOpts) { const loggerOpts = resolveLoggerOpts(opts); @@ -35,8 +35,14 @@ export function createLogger(opts: LoggerOpts) { // quit early if level is too low if (levelIdx < currentLevelIdx) return; - // resolve arg + // merge inspectOpts with logger defaults const inspectOpts = Object.assign({}, loggerOpts.inspectOpts, opts); + if (!colorSupport[levelOpts.type == "log" ? "stdout" : "stderr"]) { + // if output stream does not support color, force disable color opt + inspectOpts.colors = false; + } + + // resolve arg const argStr = typeof arg == "string" ? arg : inspect(arg, inspectOpts); // resolve prefix