<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國際加速解決方案。 廣告
                [TOC] ## logrus-OverView ***** 一個用于記錄日志的庫,用于大型系統可以進行日志切割,過期清理,兼容golango原本對日志庫 ``` log "github.com/sirupsen/logrus" ``` ## logrus-Usage ***** ### 基本使用 ``` package main import ( log "github.com/sirupsen/logrus" ) func main() { log.WithFields(log.Fields{ "animal": "walrus", }).Info("A walrus appears") } ``` `logrus`與golang標準庫日志模塊完全兼容,可以使用`log“github.com/sirupsen/logrus”`替換所有日志導入.`logrus`可以通過簡單的配置,來定義輸出、格式或者日志級別等. ` ` ### 日志切割,過期清理 結合 `github.com/lestrrat-go/file-rotatelogs`包可以對日志進行 日志切割,過期清理: ``` package logger import ( rotations "github.com/lestrrat-go/file-rotatelogs" "github.com/rifflock/lfshook" log "github.com/sirupsen/logrus" "path" "time" ) var Logger = log.New() func init() { baseLogPath := path.Join("../../runtime", "test") /* 日志輪轉相關函數 `WithLinkName` 為最新的日志建立軟連接 `WithRotationTime` 設置日志分割的時間,隔多久分割一次 WithMaxAge 和 WithRotationCount二者只能設置一個 `WithMaxAge` 設置文件清理前的最長保存時間 `WithRotationCount` 設置文件清理前最多保存的個數 */ // 下面配置日志每隔 1 分鐘輪轉一個新文件,保留最近 3 分鐘的日志文件,多余的自動清理掉。 writer, _ := rotations.New( baseLogPath+".%Y%m%d%H%M", rotations.WithLinkName(baseLogPath), rotations.WithMaxAge(time.Duration(180)*time.Second), rotations.WithRotationTime(time.Duration(60)*time.Second), ) lfHook := lfshook.NewHook(lfshook.WriterMap{ log.DebugLevel: writer, // 為不同級別設置不同的輸出目的 log.InfoLevel: writer, log.WarnLevel: writer, log.ErrorLevel: writer, log.FatalLevel: writer, log.PanicLevel: writer, },&log.TextFormatter{DisableColors: true}) Logger.AddHook(lfHook) Logger.WithFields(log.Fields{ "animal": "walrus", "size": 10, }).Info("A group of walrus emerges from the ocean") } ``` ## Zap 參考鏈接:[https://www.liwenzhou.com/posts/Go/zap/](https://www.liwenzhou.com/posts/Go/zap/) 基本使用+日志切割 ``` package main import ( "net/http" "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var sugarLogger *zap.SugaredLogger func main() { InitLogger() defer sugarLogger.Sync() simpleHttpGet("www.sogo.com") simpleHttpGet("http://www.sogo.com") } func InitLogger() { writeSyncer := getLogWriter() encoder := getEncoder() core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel) logger := zap.New(core, zap.AddCaller()) sugarLogger = logger.Sugar() } func getEncoder() zapcore.Encoder { encoderConfig := zap.NewProductionEncoderConfig() encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder return zapcore.NewConsoleEncoder(encoderConfig) } func getLogWriter() zapcore.WriteSyncer { lumberJackLogger := &lumberjack.Logger{ Filename: "./test.log", MaxSize: 1, MaxBackups: 5, MaxAge: 30, Compress: false, } fileWriteSyncer := zapcore.AddSync(lumberJackLogger) return fileWriteSyncer //return zapcore.NewMultiWriteSyncer(fileWriteSyncer,zapcore.AddSync(os.Stdout)) //這一步是將日志輸出到控制臺 } func simpleHttpGet(url string) { sugarLogger.Debugf("Trying to hit GET request for %s", url) resp, err := http.Get(url) if err != nil { sugarLogger.Errorf("Error fetching URL %s : Error = %s", url, err) } else { sugarLogger.Infof("Success! statusCode = %s for URL %s", resp.Status, url) resp.Body.Close() } } ```
                  <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>

                              哎呀哎呀视频在线观看