Skip to content

Commit

Permalink
improve NSQ logs, switch to zerolog lib (close #79)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Feb 9, 2022
1 parent 6eb9b04 commit 01a5c92
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 61 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v0.7.1] - 2022-02-08 Tue

- Add [#79]: better NSQ logs, switch to zerolog library.

## [v0.7.0]

- Add [#77]: optional log aggregation by an NSQ-messaging service.
Expand Down Expand Up @@ -127,6 +131,8 @@

This document follows [changelog guidelines]

[v0.7.1]: https://github.com/gnames/gnverifier/compare/v0.7.0...v1.7.1
[v0.7.0]: https://github.com/gnames/gnverifier/compare/v0.6.6...v1.7.0
[v0.6.6]: https://github.com/gnames/gnverifier/compare/v0.6.5...v1.6.6
[v0.6.5]: https://github.com/gnames/gnverifier/compare/v0.6.4...v1.6.5
[v0.6.4]: https://github.com/gnames/gnverifier/compare/v0.6.3...v1.6.4
Expand All @@ -151,6 +157,16 @@ This document follows [changelog guidelines]
[v0.2.0]: https://github.com/gnames/gnverifier/compare/v0.1.0...v0.2.0
[v0.1.0]: https://github.com/gnames/gnverifier/tree/v0.1.0

[#90]: https://github.com/gnames/gnverifier/issues/90
[#89]: https://github.com/gnames/gnverifier/issues/89
[#88]: https://github.com/gnames/gnverifier/issues/88
[#87]: https://github.com/gnames/gnverifier/issues/87
[#86]: https://github.com/gnames/gnverifier/issues/86
[#85]: https://github.com/gnames/gnverifier/issues/85
[#84]: https://github.com/gnames/gnverifier/issues/84
[#83]: https://github.com/gnames/gnverifier/issues/83
[#82]: https://github.com/gnames/gnverifier/issues/82
[#81]: https://github.com/gnames/gnverifier/issues/81
[#80]: https://github.com/gnames/gnverifier/issues/80
[#79]: https://github.com/gnames/gnverifier/issues/79
[#78]: https://github.com/gnames/gnverifier/issues/78
Expand Down
74 changes: 37 additions & 37 deletions gnverifier/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/gnames/gnverifier/ent/output"
"github.com/gnames/gnverifier/io/verifrest"
"github.com/gnames/gnverifier/io/web"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -33,9 +34,7 @@ import (
var configText string

var (
opts []config.Option
quiet bool
msgs []string
opts []config.Option
)

// cfgData purpose is to achieve automatic import of data from the
Expand Down Expand Up @@ -74,11 +73,9 @@ https://github.com/gnames/gnverifier
os.Exit(0)
}

quiet, _ = cmd.Flags().GetBool("quiet")
if !quiet {
for i := range msgs {
log.Print(msgs[i])
}
quiet, _ := cmd.Flags().GetBool("quiet")
if quiet {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

caps, _ := cmd.Flags().GetBool("capitalize")
Expand All @@ -93,8 +90,12 @@ https://github.com/gnames/gnverifier

formatString, _ := cmd.Flags().GetString("format")
frmt, _ := gnfmt.NewFormat(formatString)
if frmt == gnfmt.FormatNone && !quiet {
log.Warnf("Cannot set format from '%s', setting format to csv", formatString)
if frmt == gnfmt.FormatNone {
log.Warn().
Msgf(
"Cannot set format from '%s', setting format to csv",
formatString,
)
frmt = gnfmt.CSV
}
opts = append(opts, config.OptFormat(frmt))
Expand Down Expand Up @@ -132,7 +133,8 @@ https://github.com/gnames/gnverifier
webOpts = append(webOpts, config.OptWebLogsNsqdTCP(nsqAddr))
}

log.SetFormatter(&log.JSONFormatter{})
log.Logger = zerolog.New(os.Stderr).With().
Str("gnApp", "gnverifier").Logger()
cnf := config.New(webOpts...)
vfr := verifrest.New(cnf.VerifierURL)
gnv := gnverifier.New(cnf, vfr)
Expand Down Expand Up @@ -217,7 +219,7 @@ func initConfig() {
// Find config directory.
configDir, err = os.UserConfigDir()
if err != nil {
log.Fatalf("Cannot find config directory: %s.", err)
log.Fatal().Err(err).Msg("Cannot find config directory")
}

// Search config in home directory with name ".gnmatcher" (without extension).
Expand All @@ -243,8 +245,7 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
msg := fmt.Sprintf("Using config file: %s.", viper.ConfigFileUsed())
msgs = append(msgs, msg)
log.Info().Msgf("Using config file: %s.", viper.ConfigFileUsed())
}
getOpts()
}
Expand All @@ -255,7 +256,7 @@ func getOpts() {
cfg := &cfgData{}
err := viper.Unmarshal(cfg)
if err != nil {
log.Fatalf("Cannot deserialize config data: %s.", err)
log.Fatal().Err(err).Msg("Cannot deserialize config data")
}

if cfg.Format != "" {
Expand Down Expand Up @@ -295,7 +296,7 @@ func getOpts() {
func showVersionFlag(cmd *cobra.Command) bool {
hasVersionFlag, err := cmd.Flags().GetBool("version")
if err != nil {
log.Fatalf("Cannot get version flag: %s.", err)
log.Fatal().Err(err).Msg("Cannot get version flag")
}

if hasVersionFlag {
Expand All @@ -313,12 +314,12 @@ func parseDataSources(s string) []int {
for _, v := range dss {
v = strings.Trim(v, " ")
ds, err := strconv.Atoi(v)
if err != nil && !quiet {
log.Warnf("Cannot convert data-source '%s' to list, skipping", v)
if err != nil {
log.Warn().Msgf("Cannot convert data-source '%s' to list, skipping", v)
return nil
}
if ds < 0 && !quiet {
log.Warnf("Data source ID %d is less than zero, skipping", ds)
if ds < 0 {
log.Warn().Msgf("Data source ID %d is less than zero, skipping", ds)
} else {
res = append(res, int(ds))
}
Expand Down Expand Up @@ -346,7 +347,7 @@ func checkStdin() bool {
stdInFile := os.Stdin
stat, err := stdInFile.Stat()
if err != nil {
log.Fatal(err)
log.Fatal().Err(err)
}
return (stat.Mode() & os.ModeCharDevice) == 0
}
Expand All @@ -368,7 +369,7 @@ func verify(gnv gnverifier.GNverifier, str string) {
if fileExists {
f, err := os.OpenFile(str, os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
log.Fatal().Err(err)
}
verifyFile(gnv, f)
f.Close()
Expand Down Expand Up @@ -417,13 +418,13 @@ func processResults(gnv gnverifier.GNverifier, out <-chan []vlib.Name,
timeSpent := float64(time.Now().UnixNano()-timeStart) / 1_000_000_000
speed := int64(float64(total) / timeSpent)

if !quiet {
log.Printf("Verified %s records, %s names/sec\n", humanize.Comma(total),
humanize.Comma(speed))
}
log.Info().
Str("names/sec", humanize.Comma(speed)).
Str("names", humanize.Comma(int64(total))).
Msg("Verified")
for _, r := range o {
if r.Error != "" && !quiet {
log.Println(r.Error)
if r.Error != "" {
log.Warn().Msg(r.Error)
}
fmt.Println(output.NameOutput(r, f, gnv.Config().PreferredOnly))
}
Expand All @@ -433,7 +434,7 @@ func processResults(gnv gnverifier.GNverifier, out <-chan []vlib.Name,
func verifyString(gnv gnverifier.GNverifier, name string) {
res, err := gnv.VerifyOne(name)
if err != nil {
log.Fatal(err)
log.Fatal().Err(err)
}

f := gnv.Config().Format
Expand All @@ -456,7 +457,7 @@ func searchQuery(gnv gnverifier.GNverifier, s string) {
}
res, err := gnv.Search(inp)
if err != nil {
log.Fatal(err)
log.Fatal().Err(err)
}

f := gnv.Config().Format
Expand All @@ -465,8 +466,8 @@ func searchQuery(gnv gnverifier.GNverifier, s string) {
}

for _, v := range res {
if v.Error != "" && !quiet {
log.Println(v.Error)
if v.Error != "" {
log.Warn().Msg(v.Error)
}
fmt.Println(output.NameOutput(v, f, gnv.Config().PreferredOnly))
}
Expand All @@ -478,20 +479,19 @@ func touchConfigFile(configPath string) {
if fileExists {
return
}
msg := fmt.Sprintf("Creating config file: %s.", configPath)
msgs = append(msgs, msg)
log.Info().Msgf("Creating config file '%s'", configPath)
createConfig(configPath)
}

// createConfig creates config file.
func createConfig(path string) {
err := gnsys.MakeDir(filepath.Dir(path))
if err != nil {
log.Fatalf("Cannot create dir %s: %s.", path, err)
log.Fatal().Err(err).Msgf("Cannot create dir %s", path)
}

err = os.WriteFile(path, []byte(configText), 0644)
if err != nil {
log.Fatalf("Cannot write to file %s: %s", path, err)
log.Fatal().Err(err).Msgf("Cannot write to file %s", path)
}
}
14 changes: 13 additions & 1 deletion gnverifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ THE SOFTWARE.
*/
package main

import "github.com/gnames/gnverifier/gnverifier/cmd"
import (
"os"

"github.com/gnames/gnverifier/gnverifier/cmd"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func main() {
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: "15:04:05",
},
)
cmd.Execute()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/labstack/echo/v4 v4.6.3
github.com/labstack/gommon v0.3.1
github.com/maxbrunsfeld/counterfeiter/v6 v6.4.1
github.com/rs/zerolog v1.26.1
github.com/sfgrp/lognsq v0.1.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx
github.com/rendon/testcli v1.0.0/go.mod h1:z5nHelI3O4dlSj2vIeFKvwn2z2Tm3hwV2M8J7SQ7XOg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand All @@ -426,8 +429,6 @@ github.com/sfgrp/lognsq v0.1.1/go.mod h1:aNbHWh6z8OIKU/3jwQIYnd3hDh0f4Nmdj0kyAiH
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down Expand Up @@ -504,6 +505,7 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down
Loading

0 comments on commit 01a5c92

Please sign in to comment.