Skip to content

Instantly share code, notes, and snippets.

@xavierskip
Created April 9, 2026 07:30
Show Gist options
  • Select an option

  • Save xavierskip/07469dee0aaf44da90128b730f9bf765 to your computer and use it in GitHub Desktop.

Select an option

Save xavierskip/07469dee0aaf44da90128b730f9bf765 to your computer and use it in GitHub Desktop.
Simplify Mihomo's log output in the terminal.
# 强制设定控制台和 PowerShell 管道的编码为 UTF-8,拯救 Emoji
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
function Start-Mihomo {
param(
[string]$Path = ".\mihomo.exe",
[string]$Dir = ".",
[string]$Config = "config.yml"
)
& $Path -d $Dir -f $Config 2>&1 | ForEach-Object {
if ($_ -match 'time=".*?T(\d{2}:\d{2}:\d{2}).*?"\s+level=([a-z]+)\s+msg="(.*)"') {
$time = $Matches[1]
$level = $Matches[2].ToUpper()
$msg = $Matches[3]
# 【新增逻辑】简化 msg 内容
# 匹配从开头到 "--> " 的所有字符并将其替换为空(即删除)
# 如果不包含 "--> ",则 -replace 不起作用,保留原样
$match = $msg -replace '^.*?-->\s*', ''
# 根据日志级别分配终端颜色代码
$color = switch ($level) {
'INFO' { "`e[36m" } # 青色 (Cyan)
'WARNING' { "`e[33m" } # 黄色 (Yellow)
'ERROR' { "`e[31m" } # 红色 (Red)
'DEBUG' { "`e[90m" } # 灰色 (Gray)
default { "`e[37m" } # 白色 (White)
}
$reset = "`e[0m"
# 重新拼接字符串并输出
"${color}[$time]${reset} $match"
} else {
$_ # 输出无法匹配的原始行(比如启动时的 banner)
}
}
}
Start-Mihomo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment