mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 16:51:25 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -25,7 +25,9 @@ export type SubsystemLogger = {
|
||||
};
|
||||
|
||||
function shouldLogToConsole(level: LogLevel, settings: { level: LogLevel }): boolean {
|
||||
if (settings.level === "silent") return false;
|
||||
if (settings.level === "silent") {
|
||||
return false;
|
||||
}
|
||||
const current = levelToMinLevel(level);
|
||||
const min = levelToMinLevel(settings.level);
|
||||
return current <= min;
|
||||
@@ -35,7 +37,9 @@ type ChalkInstance = InstanceType<typeof Chalk>;
|
||||
|
||||
function isRichConsoleEnv(): boolean {
|
||||
const term = (process.env.TERM ?? "").toLowerCase();
|
||||
if (process.env.COLORTERM || process.env.TERM_PROGRAM) return true;
|
||||
if (process.env.COLORTERM || process.env.TERM_PROGRAM) {
|
||||
return true;
|
||||
}
|
||||
return term.length > 0 && term !== "dumb";
|
||||
}
|
||||
|
||||
@@ -44,7 +48,9 @@ function getColorForConsole(): ChalkInstance {
|
||||
typeof process.env.FORCE_COLOR === "string" &&
|
||||
process.env.FORCE_COLOR.trim().length > 0 &&
|
||||
process.env.FORCE_COLOR.trim() !== "0";
|
||||
if (process.env.NO_COLOR && !hasForceColor) return new Chalk({ level: 0 });
|
||||
if (process.env.NO_COLOR && !hasForceColor) {
|
||||
return new Chalk({ level: 0 });
|
||||
}
|
||||
const hasTty = Boolean(process.stdout.isTTY || process.stderr.isTTY);
|
||||
return hasTty || isRichConsoleEnv() ? new Chalk({ level: 1 }) : new Chalk({ level: 0 });
|
||||
}
|
||||
@@ -59,7 +65,9 @@ const CHANNEL_SUBSYSTEM_PREFIXES = new Set<string>(CHAT_CHANNEL_ORDER);
|
||||
|
||||
function pickSubsystemColor(color: ChalkInstance, subsystem: string): ChalkInstance {
|
||||
const override = SUBSYSTEM_COLOR_OVERRIDES[subsystem];
|
||||
if (override) return color[override];
|
||||
if (override) {
|
||||
return color[override];
|
||||
}
|
||||
let hash = 0;
|
||||
for (let i = 0; i < subsystem.length; i += 1) {
|
||||
hash = (hash * 31 + subsystem.charCodeAt(i)) | 0;
|
||||
@@ -78,7 +86,9 @@ function formatSubsystemForConsole(subsystem: string): string {
|
||||
) {
|
||||
parts.shift();
|
||||
}
|
||||
if (parts.length === 0) return original;
|
||||
if (parts.length === 0) {
|
||||
return original;
|
||||
}
|
||||
if (CHANNEL_SUBSYSTEM_PREFIXES.has(parts[0])) {
|
||||
return parts[0];
|
||||
}
|
||||
@@ -92,7 +102,9 @@ export function stripRedundantSubsystemPrefixForConsole(
|
||||
message: string,
|
||||
displaySubsystem: string,
|
||||
): string {
|
||||
if (!displaySubsystem) return message;
|
||||
if (!displaySubsystem) {
|
||||
return message;
|
||||
}
|
||||
|
||||
// Common duplication: "[discord] discord: ..." (when a message manually includes the subsystem tag).
|
||||
if (message.startsWith("[")) {
|
||||
@@ -101,22 +113,34 @@ export function stripRedundantSubsystemPrefixForConsole(
|
||||
const bracketTag = message.slice(1, closeIdx);
|
||||
if (bracketTag.toLowerCase() === displaySubsystem.toLowerCase()) {
|
||||
let i = closeIdx + 1;
|
||||
while (message[i] === " ") i += 1;
|
||||
while (message[i] === " ") {
|
||||
i += 1;
|
||||
}
|
||||
return message.slice(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const prefix = message.slice(0, displaySubsystem.length);
|
||||
if (prefix.toLowerCase() !== displaySubsystem.toLowerCase()) return message;
|
||||
if (prefix.toLowerCase() !== displaySubsystem.toLowerCase()) {
|
||||
return message;
|
||||
}
|
||||
|
||||
const next = message.slice(displaySubsystem.length, displaySubsystem.length + 1);
|
||||
if (next !== ":" && next !== " ") return message;
|
||||
if (next !== ":" && next !== " ") {
|
||||
return message;
|
||||
}
|
||||
|
||||
let i = displaySubsystem.length;
|
||||
while (message[i] === " ") i += 1;
|
||||
if (message[i] === ":") i += 1;
|
||||
while (message[i] === " ") i += 1;
|
||||
while (message[i] === " ") {
|
||||
i += 1;
|
||||
}
|
||||
if (message[i] === ":") {
|
||||
i += 1;
|
||||
}
|
||||
while (message[i] === " ") {
|
||||
i += 1;
|
||||
}
|
||||
return message.slice(i);
|
||||
}
|
||||
|
||||
@@ -186,12 +210,16 @@ function logToFile(
|
||||
message: string,
|
||||
meta?: Record<string, unknown>,
|
||||
) {
|
||||
if (level === "silent") return;
|
||||
if (level === "silent") {
|
||||
return;
|
||||
}
|
||||
const safeLevel = level;
|
||||
const method = (fileLogger as unknown as Record<string, unknown>)[safeLevel] as
|
||||
| ((...args: unknown[]) => void)
|
||||
| undefined;
|
||||
if (typeof method !== "function") return;
|
||||
if (typeof method !== "function") {
|
||||
return;
|
||||
}
|
||||
if (meta && Object.keys(meta).length > 0) {
|
||||
method.call(fileLogger, meta, message);
|
||||
} else {
|
||||
@@ -202,7 +230,9 @@ function logToFile(
|
||||
export function createSubsystemLogger(subsystem: string): SubsystemLogger {
|
||||
let fileLogger: TsLogger<LogObj> | null = null;
|
||||
const getFileLogger = () => {
|
||||
if (!fileLogger) fileLogger = getChildLogger({ subsystem });
|
||||
if (!fileLogger) {
|
||||
fileLogger = getChildLogger({ subsystem });
|
||||
}
|
||||
return fileLogger;
|
||||
};
|
||||
const emit = (level: LogLevel, message: string, meta?: Record<string, unknown>) => {
|
||||
@@ -219,8 +249,12 @@ export function createSubsystemLogger(subsystem: string): SubsystemLogger {
|
||||
fileMeta = Object.keys(rest).length > 0 ? rest : undefined;
|
||||
}
|
||||
logToFile(getFileLogger(), level, message, fileMeta);
|
||||
if (!shouldLogToConsole(level, { level: consoleSettings.level })) return;
|
||||
if (!shouldLogSubsystemToConsole(subsystem)) return;
|
||||
if (!shouldLogToConsole(level, { level: consoleSettings.level })) {
|
||||
return;
|
||||
}
|
||||
if (!shouldLogSubsystemToConsole(subsystem)) {
|
||||
return;
|
||||
}
|
||||
const consoleMessage = consoleMessageOverride ?? message;
|
||||
if (
|
||||
!isVerbose() &&
|
||||
|
||||
Reference in New Issue
Block a user