.log 檔案 (Log File) 是**軟體/系統執行過程記錄的文字/二進位日誌**,內容通常為時間戳、事件等級 (INFO/WARN/ERROR)、模組名、訊息、堆疊追蹤等。無統一強制格式,視開發者定義而異。
| 場景 | 推薦工具/方法 | 關鍵操作 |
|---|---|---|
| 小檔案 (<50MB)、純文字、編碼標準 | **記事本 / Notepad++ / VS Code / Sublime Text** | 雙點 / 右鍵開啟方式 / 拖入編輯器 |
| 大檔案 (50MB-2GB)、需搜尋/過濾 | **Notepad++ (64-bit) / Sublime Text / LTFViewr / UltraEdit** | 開啟 → Ctrl+F 搜尋 / 正則篩選 / 書籤標記 |
| 超大檔案 (>2GB) / 伺服器無 GUI | **PowerShell / CMD / Linux 指令 (less, tail, grep, awk)** | Get-Content file.log -Tail 100, Select-String "ERROR" file.log |
| 亂碼/編碼未知 | **Notepad++ / VS Code (手動切換編碼) / TrID 鑑別 / file -i (Linux)** | 選單 → 編碼 → 選 UTF-8/UTF-16 LE/GB18030/Big5 |
| Windows 事件日誌 (.evtx) | **事件檢視器 (eventvwr.msc) / EvtxECmd / EVTX Viewer** | 系統管理工具 / 命令列匯出 JSON/CSV |
| 二進位/專屬格式 (ETW .etl, DB WAL, pcap) | **專用分析器 (WPA, LogMiner, Wireshark, 010 Editor + 模板)** | 不可用文字編輯器強開 |
| 生產環境/集中化/長期分析 | **Loki+Grafana / ELK/OpenSearch / Graylog / Splunk / SaaS 觀測平台** | 部署 Agent/Collector → 統一收集 → 索引/視覺化/告警 |
2024-01-15 10:30:45.123 [INFO ] [main] Application started. Version=1.2.3
2024-01-15 10:30:46.456 [DEBUG] [db.pool] HikariCP pool initialized. maxSize=20
2024-01-15 10:31:12.789 [WARN ] [http-nio-8080-exec-3] Slow query detected: SELECT * FROM users WHERE last_login < '2023-01-01' (took 2.3s)
2024-01-15 10:32:01.001 [ERROR] [order.service] Order processing failed for orderId=ORD-20240115-001
java.lang.NullPointerException: Cannot invoke "Customer.getEmail()" because "customer" is null
at com.example.OrderService.process(OrderService.java:45)
at com.example.OrderController.create(OrderController.java:22)
...
logrotate (Linux) / logrotate-windows / 應用程式內建 RollingFileAppender (Log4j, Logback, Serilog, NLog),避免佔滿磁碟導致服務癱瘓。.log = **系統/軟體的黑盒子記錄器**。會看 Log、善用工具、懂結構、重隱私、會清理,是開發/運維/資安人員的核心基本功。