如何使用 Uber-Go/Zap 根據日誌等級記錄到 STDOUT 和 STDERR?
Zap,一個流行的 Go 日誌庫,可讓您根據日誌訊息的層級控制日誌訊息的寫入位置。以下是如何配置Zap 進行寫入:
- 信息日誌記錄到STDOUT
- 錯誤和致命日誌記錄到STDERR
將ZapCore.NewTee 與單獨的核心結合使用:
要實現這種分離,請建立一個結合兩個核心的Tee 核心:
- 一個專用核心到InfoLevel,寫入STDOUT。
- 另一個專用於 ErrorLevel 和 FatalLevel 的核心,寫入 STDERR。
這是示範此技術的範例程式:
<code class="go">package main import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" ) func main() { // Level enablers infoLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool { return level == zapcore.InfoLevel }) errorFatalLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool { return level == zapcore.ErrorLevel || level == zapcore.FatalLevel }) // Write syncers stdoutSyncer := zapcore.Lock(os.Stdout) stderrSyncer := zapcore.Lock(os.Stderr) // Tee core for InfoLevel and ErrorLevel/FatalLevel separation core := zapcore.NewTee( zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), stdoutSyncer, infoLevel, ), zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), stderrSyncer, errorFatalLevel, ), ) // Create logger with tee core logger := zap.New(core) logger.Info("Info log") logger.Error("Error log") }</code>
在此範例中,程式將訊息訊息記錄到 STDOUT,將錯誤訊息記錄到 STDERR。您可以透過將對應的輸出重新導向到 /dev/null 來驗證這一點:
<code class="bash"># stdout only $ go build main.go && ./main 2>/dev/null { "level": "info", "ts": <timestamp>, "msg": "Info log" } # stderr only $ go build main.go && ./main 1>/dev/null { "level": "error", "ts": <timestamp>, "msg": "Error log" }</timestamp></timestamp></code>
以上是如何使用 Uber-Go/Zap 將訊息日誌分離到 STDOUT 並將錯誤/致命日誌分離到 STDERR?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

theEncoding/binarypackageingoisessential forhandlingbinarydata,offeringFunctionStoreadAndWritedAtainBig-Endianandlittle-endianFormats.1)IT'SidealFornetwork-work-workprotocels,enableSeringSeringSerializationalializationalialization andDeSerialization andDeSerializationOfStructuredDatalizedDataliakePackackEtheadErloth

theessentionfunctionsingo'sbytespackageThatyOuneedToknoware:1)字節.indexforsearchingwithinbyteslices,2)bytes.splitforparsing數據,3)字節。 joinforConcatenatingslices,4)bytes.containsforcheckingsubslicepresence和5)bytes.replaceallfordatatatatransformatio

Gooffersalternativestothestringspackageforstringmanipulation:1)Theregexppackageforcomplexpatternmatching,2)Thestrconvpackagefornumericconversions,and3)Externallibrarieslikestrutilforspecializedoperations.Theseoptionscatertodifferentneeds,enhancingyou

效率地使用/binarypackageforhandlingvariousdatatypes,lofterTheSesteps:1)指定bbyteorder(例如binary.littleendian)for -compatibility.2)usepututuint32/uint32222222222forintfloAtfloat32bits/floatt32bits/floatth32222222frolbomentss.3225frolbomtss.3)

掌握bytes包的原因是它能显著提高处理字节切片的效率和性能。1)bytes包提供了强大的工具,如bytes.Contains用于搜索字节序列,2)bytes.Buffer类型适用于增量构建字节切片,3)了解bytes包的使用陷阱和性能优化策略,如重用bytes.Buffer实例,可以避免常见错误并提升效率。

Go的"strings"包提供了豐富的功能,使字符串操作高效且簡單。 1)使用strings.Contains()檢查子串。 2)strings.Split()可用於解析數據,但需謹慎使用以避免性能問題。 3)strings.Join()適用於格式化字符串,但對小數據集,循環使用 =更有效。 4)對於大字符串,使用strings.Builder構建字符串更高效。

Go語言使用"strings"包進行字符串操作。 1)拼接字符串使用strings.Join函數。 2)查找子串使用strings.Contains函數。 3)替換字符串使用strings.Replace函數,這些函數高效且易用,適用於各種字符串處理任務。

資助bytespackageingoisesential foreffited byteSemanipulation,uperingFunctionsLikeContains,index,andReplaceForsearchingangingAndModifyingBinaryData.itenHancesperformanceNandCoderAceAnibility,MakeitiTavitalToolToolToolToolToolToolToolToolToolForhandLingBinaryData,networkProtocols,networkProtocoLss,networkProtocols,andetFilei


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境