Files
app-cli/dev/logs.ps1
toom1996 b993b4d61e update
2026-10-22 19:58:53 +08:00

82 lines
2.6 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#
.SYNOPSIS
查看本地服务的运行日志(dev.ps1 用后台进程启动,输出不落在终端)。
.DESCRIPTION
dev.ps1 把后端/前端的 stdout/stderr 重定向到 .logs\ 下的文件,
所以「服务在跑但终端没有任何输出」是正常的。本脚本就是看那些文件的入口。
每个服务有两个文件:
.logs\<name>.log 标准输出(业务日志、gin 路由、astro 启动信息)
.logs\<name>.err.log 标准错误(编译报错、panic、连接失败)
.EXAMPLE
.\dev\logs.ps1
看后端最近 50 行(默认)
.EXAMPLE
.\dev\logs.ps1 frontend -Lines 100
看前端最近 100 行
.EXAMPLE
.\dev\logs.ps1 backend -Follow
持续跟随后端日志(Ctrl+C 退出),等价于 Get-Content -Wait
.EXAMPLE
.\dev\logs.ps1 all
三个服务的日志都看一遍(只看不跟随)
#>
param(
[Parameter(Position = 0)]
[ValidateSet('backend', 'frontend', 'vision', 'all')]
[string]$Name = 'backend',
[int]$Lines = 50,
[switch]$Follow
)
$ErrorActionPreference = 'Stop'
$CliRoot = Split-Path -Parent $PSScriptRoot
$LogDir = Join-Path $CliRoot '.logs'
if (-not (Test-Path $LogDir)) {
Write-Host " 还没有日志:$LogDir" -ForegroundColor Yellow
Write-Host " 先跑 .\dev\dev.ps1 启动服务" -ForegroundColor Gray
exit 1
}
$targets = if ($Name -eq 'all') { @('backend', 'frontend', 'vision') } else { @($Name) }
if ($Follow -and $targets.Count -gt 1) {
Write-Host " -Follow 一次只能跟一个服务,改成:.\dev\logs.ps1 backend -Follow" -ForegroundColor Yellow
exit 1
}
foreach ($t in $targets) {
foreach ($suffix in @('.log', '.err.log')) {
$f = Join-Path $LogDir ($t + $suffix)
if (-not (Test-Path $f)) { continue }
$size = (Get-Item $f).Length
Write-Host ''
Write-Host (" --- {0}{1} ({2} 字节) ---" -f $t, $suffix, $size) -ForegroundColor DarkGray
if ($size -eq 0) {
Write-Host ' (空文件:进程可能刚启动,或输出还在缓冲区)' -ForegroundColor DarkGray
continue
}
if ($Follow) {
# -Wait 会阻塞直到 Ctrl+C;先打最近 N 行再跟随新增内容
Write-Host " 跟随中,Ctrl+C 退出" -ForegroundColor DarkGray
Get-Content -Path $f -Tail $Lines -Wait
} else {
Get-Content -Path $f -Tail $Lines | ForEach-Object { Write-Host " $_" }
}
}
}
Write-Host ''
Write-Host ' PID 登记:.run\<name>.pid 状态:.\dev\status.ps1' -ForegroundColor DarkGray