<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國際加速解決方案。 廣告
                golang自帶日志框架功能比較簡單,僅提供print、panic、fatal三個方法,對于常規的日志切割等功能并未提供支持。 這里推薦使用sirupsen/logrus日志庫。 logrus feature 結構化、可插拔的日志模塊 完全兼容官方log庫接口 Field機制 可擴展的HOOK機制 TEXT與JSON兩種可選格式 簡單使用示例 std Logger 與官方log類似,logrus也提供了一個名為std的標準logger,對外導出的各類方法直接使用std記錄日志,一般可按如下方式使用 ~~~ package main import log "github.com/sirupsen/logrus" func main() { log.Info("hello, world.") } ~~~ 自建Logger實例 ~~~ package main import ( "os" "github.com/sirupsen/logrus" ) // Create a new instance of the logger. You can have any number of instances. var log = logrus.New() func main() { // The API for setting attributes is a little different than the package level // exported logger. See Godoc. log.Out = os.Stdout // You could set this to any `io.Writer` such as a file // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) // if err == nil { // log.Out = file // } else { // log.Info("Failed to log to file, using default stderr") // } log.WithFields(logrus.Fields{ "animal": "walrus", "size": 10, }).Info("A group of walrus emerges from the ocean") } ~~~ 輸出到本地文件系統 下面示例代碼,通過一個hook將日志輸出到本地文件系統中,并提供日志切割功能 ~~~ import ( "github.com/lestrrat/go-file-rotatelogs" "github.com/rifflock/lfshook" log "github.com/sirupsen/logrus" "time" "os" "github.com/pkg/errors" "path" ) // config logrus log to local filesystem, with file rotation func ConfigLocalFilesystemLogger(logPath string, logFileName string, maxAge time.Duration, rotationTime time.Duration) { baseLogPaht := path.Join(logPath, logFileName) writer, err := rotatelogs.New( baseLogPaht+".%Y%m%d%H%M", rotatelogs.WithLinkName(baseLogPaht), // 生成軟鏈,指向最新日志文件 rotatelogs.WithMaxAge(maxAge), // 文件最大保存時間 rotatelogs.WithRotationTime(rotationTime), // 日志切割時間間隔 ) if err != nil { log.Errorf("config local file system logger error. %+v", errors.WithStack(err)) } lfHook := lfshook.NewHook(lfshook.WriterMap{ log.DebugLevel: writer, // 為不同級別設置不同的輸出目的 log.InfoLevel: writer, log.WarnLevel: writer, log.ErrorLevel: writer, log.FatalLevel: writer, log.PanicLevel: writer, }) log.AddHook(lfHook) } ~~~ 輸出到MQ或ES 如下示例代碼通過hook將日志輸出到amqp消息隊列,或者es中。 ~~~ import ( "github.com/vladoatanasov/logrus_amqp" "gopkg.in/olivere/elastic.v5" "gopkg.in/sohlich/elogrus.v2" log "github.com/sirupsen/logrus" "github.com/pkg/errors" ) // config logrus log to amqp func ConfigAmqpLogger(server, username, password, exchange, exchangeType, virtualHost, routingKey string) { hook := logrus_amqp.NewAMQPHookWithType(server, username, password, exchange, exchangeType, virtualHost, routingKey) log.AddHook(hook) } // config logrus log to es func ConfigESLogger(esUrl string, esHOst string, index string) { client, err := elastic.NewClient(elastic.SetURL(esUrl)) if err != nil { log.Errorf("config es logger error. %+v", errors.WithStack(err)) } esHook, err := elogrus.NewElasticHook(client, esHOst, log.DebugLevel, index) if err != nil { log.Errorf("config es logger error. %+v", errors.WithStack(err)) } log.AddHook(esHook) } ~~~
                  <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>

                              哎呀哎呀视频在线观看