Skip to content

Commit

Permalink
fix: force-disable color in util.inspect if unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsunee committed Nov 27, 2024
1 parent 1d889c2 commit e1e7c74
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<Level extends string>(opts: LoggerOpts<Level>) {
const loggerOpts = resolveLoggerOpts(opts);
Expand Down Expand Up @@ -35,8 +35,14 @@ export function createLogger<Level extends string>(opts: LoggerOpts<Level>) {
// 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
Expand Down

0 comments on commit e1e7c74

Please sign in to comment.