Go의 로그인 라이브러리 zap 정보

6171 단어 Gozaptech

이른바


거의 제목과 마찬가지로 고의 제3자가 만든 로그 라이브러리다.
https://github.com/uber-go/zap
zap에 관하여 특징과 기본적인 사용 방법을 총결하였다.

컨디션

  • macOS Big Sur 11.3.1
  • go 1.16.5 darwin/amd64
  • zap v1.15.0
  • 특징.

  • 반사를 사용하지 않습니다.
  • 인터페이스가 없습니다.

  • sync.Pool에서 이 위치를 피했다.
  • 또한여기.에 따라zap은 다른 Logging 라이브러리나 표준 포장보다 빠르다.

    Logger와 Sugared Logger의 차이점

  • Logger
  • 고속
  • 형 엄격
  • 구조화된 Logging
  • 만 가능
  • SugaredLogger
  • 약간 저속
  • 단맛(슈가만)
  • 구조화된 Logging
  • printf 스타일의 Logging 가능
  • 샘플 코드

  • Logger
  • logger, _ := zap.NewProduction()
    defer logger.Sync()
    url := "https://example.com/"
    logger.Info("failed to fetch URL",
      // Structured context as strongly typed Field values.
      zap.String("url", url),
      zap.Int("attempt", 3),
      zap.Duration("backoff", time.Second),
    )
    
    결과
    {"level":"info","ts":1633150860.2312949,"caller":"zap-example/main.go:13","msg":"failed to fetch URL","url":"https://example.com/","attempt":3,"backoff":"1"}
    
  • SugaredLogger
  • logger, _ := zap.NewProduction()
    defer logger.Sync() // flushes buffer, if any
    sugar := logger.Sugar()
    url := "https://example.com/"
    sugar.Infow("failed to fetch URL",
      // Structured context as loosely typed key-value pairs.
      "url", url,
      "attempt", 3,
      "backoff", time.Second,
    )
    sugar.Infof("Failed to fetch URL: %s", url)
    
    결과
    {"level":"info","ts":1633151590.2312949,"caller":"zap-example/main.go:13","msg":"failed to fetch URL","url":"https://example.com/","attempt":3,"backoff":"1"}
    {"level":"info","ts":1633151590.232893,"caller":"zap-example/main.go:19","msg":"failed to fetch URL: https://example.com/"}
    

    총결산


    Go의 Logging 라이브러리 zap에 대해 설명합니다.
    아무래도 속도를 의식할 때 Logger를 사용하고, 그 외에 기본적으로 Sugared Logger를 사용하면 문제없다.
    또 다른 Config 놀이가 있다면 로그를 맞춤형으로 만들 수 있지만 기회가 된다면 글을 쓰고 싶습니다.

    참고 문헌

  • 구글 고속 구조화 로그 라이브러리'zap'사용 방법
  • 좋은 웹페이지 즐겨찾기