<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 使用context包進行日志記錄 本節將展示如何在各種函數間傳遞log操作。使用Go標準庫中的context包是在函數之間傳遞和取消變量的絕佳方式。本節將探討如何使用該方案在函數之間分配變量以進行日志記錄。 本節使用到了上節中提到的apex庫。 ### 實踐 1. 獲取第三方庫: ``` go get github.com/apex/log ``` 2. 建立log.go: ``` package context import ( "context" "github.com/apex/log" ) type key int const logFields key = 0 func getFields(ctx context.Context) *log.Fields { fields, ok := ctx.Value(logFields).(*log.Fields) if !ok { f := make(log.Fields) fields = &f } return fields } // FromContext 接收一個log.Interface和context // 然后返回由context對象填充的log.Entry指針 func FromContext(ctx context.Context, l log.Interface) (context.Context, *log.Entry) { fields := getFields(ctx) e := l.WithFields(fields) ctx = context.WithValue(ctx, logFields, fields) return ctx, e } // WithField 將log.Fielder添加到context func WithField(ctx context.Context, key string, value interface{}) context.Context { return WithFields(ctx, log.Fields{key: value}) } // WithFields 將log.Fielder添加到context func WithFields(ctx context.Context, fields log.Fielder) context.Context { f := getFields(ctx) for key, val := range fields.Fields() { (*f)[key] = val } ctx = context.WithValue(ctx, logFields, f) return ctx } ``` 3. 建立collect.go: ``` package context import ( "context" "os" "github.com/apex/log" "github.com/apex/log/handlers/text" ) // Initialize調用3個函數來設置,然后在操作完成之前記錄日志 func Initialize() { log.SetHandler(text.New(os.Stdout)) //初始化context ctx := context.Background() // 將context與log.Log建立聯系 ctx, e := FromContext(ctx, log.Log) ctx = WithField(ctx, "id", "123") e.Info("starting") gatherName(ctx) e.Info("after gatherName") gatherLocation(ctx) e.Info("after gatherLocation") } func gatherName(ctx context.Context) { ctx = WithField(ctx, "name", "Go Cookbook") } func gatherLocation(ctx context.Context) { ctx = WithFields(ctx, log.Fields{"city": "Seattle", "state": "WA"}) } ``` 4. 建立main.go: ``` package main import "github.com/agtorre/go-cookbook/chapter4/context" func main() { context.Initialize() } ``` 5. 這會輸出: ``` INFO[0000] starting id=123 INFO[0000] after gatherName id=123 name=Go Cookbook INFO[0000] after gatherLocation city=Seattle id=123 name=Go Cookbook state=WA ``` ### 說明 context包的使用在databases包和HTTP包中都可以看到。本節將log附加到context并將其用于記錄日志以達到并發安全的目的。我們通過在方法中傳遞context來同時傳遞附加于其上的屬性,以達到在最終調用位置記錄日志的目的。 這些修改了存儲在上下文中的單個值,并提供了使用context的另一個好處:可以執行取消和超時操作,并且線程安全。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看