Skip to content

Commit

Permalink
move to slog from zerlog (close #269)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Nov 6, 2024
1 parent feb8b4a commit f12d6e8
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 233 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix [#271]: distinguish between `ex` and `in`.
- Add [#269]: switch to slog from zerologs.
- Fix [#270]: missing verbatim authorship for names that look similar to
combination uninomial in ICZN.
- Fix [#268]: if botanical author looks like a combination uninomial,
Expand Down
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,6 @@ performance.
`--port -p`
: set a port to run web-interface and [RESTful API][OpenAPI].

` --nsqd-tcp`
: requires `--port`. Allows to redirect web-service log output to [NSQ]
messaging server's TCP-based endpoint. It is handy for aggregations of logs
from GNparser web-services running inside of Docker containers or
in Kubernetes pods.
`--species-group-cut`
: Changes stemmed canonical for autonym or species group names (e.g. `Aus bus bus`). It cuts infraspecific epithet, leaving only genus and specific
epithet. All other data stays the same. This feature might be useful to
Expand Down Expand Up @@ -582,23 +576,6 @@ The following enables web-access logs to be printed to STDERR
gnparser -p 80 --web-logs
```
This next settings allows to send logs to a [NSQ] messaging service.
This option allows aggregation logs from several instances of GNparser together.
It is a great way for log aggregation and analysis if the instances run
inside Docker containers or as Kubernetes Pods.

```
gnparser -p 80 --nsqd-tcp=127.0.0.1:4150
```

An **important note**: the address must point to the TCP service of nsqd.

To enable logs to be sent to STDERR and [NSQ] run

```
gnparser -p 80 --web-logs --nsqd-tcp=127.0.0.1:4150
```

### Use as a Docker image
You need to have [docker runtime installed](https://docs.docker.com/install/)
Expand Down Expand Up @@ -706,7 +683,6 @@ Released under [MIT license]
[CONTRIBUTING]: https://github.com/gnames/gnparser/blob/master/CONTRIBUTING.md
[Dmitry Mozzherin]: https://github.com/dimus
[Geoff Ower]: https://github.com/gdower
[NSQ]: https://nsq.io/overview/quick_start.html
[Toby Marsden]: https://github.com/tobymarsden
[Hernan Lucas Pereira]: https://github.com/LocoDelAssembly
[Homebrew]: https://brew.sh/
Expand Down
23 changes: 3 additions & 20 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gnparser

import (
"log/slog"
"runtime"

"github.com/gnames/gnfmt"
"github.com/rs/zerolog/log"
)

// Config keeps settings that might affect how parsing is done,
Expand Down Expand Up @@ -39,16 +39,6 @@ type Config struct {
// Port to run wer-service.
Port int

// WebLogsNsqdTCP provides an address to the NSQ messenger TCP service. If
// this value is set and valid, the web logs will be published to the NSQ.
// The option is ignored if `Port` is not set.
//
// If WithWebLogs option is set to `false`, but `WebLogsNsqdTCP` is set to a
// valid URL, the logs will be sent to the NSQ messanging service, but they
// wil not appear as STRERR output.
// Example: `127.0.0.1:4150`
WebLogsNsqdTCP string

// WithCapitalization flag, when true, the first letter of a name-string
// is capitalized, if appropriate.
WithCapitalization bool
Expand Down Expand Up @@ -89,7 +79,7 @@ type Option func(*Config)
func OptBatchSize(i int) Option {
return func(cfg *Config) {
if i <= 0 {
log.Info().Msg("Batch size should be a positive number")
slog.Warn("Batch size should be a positive number")
return
}
cfg.BatchSize = i
Expand All @@ -112,7 +102,7 @@ func OptFormat(s string) Option {
f, err := gnfmt.NewFormat(s)
if err != nil {
f = gnfmt.CSV
log.Printf("Set default CSV format due to error: %s.", err)
slog.Warn("Set default CSV format due to error", "error", err)
}
cfg.Format = f
}
Expand Down Expand Up @@ -148,13 +138,6 @@ func OptPort(i int) Option {
}
}

// OptWebLogsNsqdTCP provides a URL to NSQ messanging service.
func OptWebLogsNsqdTCP(s string) Option {
return func(cfg *Config) {
cfg.WebLogsNsqdTCP = s
}
}

// OptWithCapitaliation sets the WithCapitalization field.
func OptWithCapitaliation(b bool) Option {
return func(cfg *Config) {
Expand Down
4 changes: 2 additions & 2 deletions ent/internal/preparser/preparser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package preparser

import "github.com/rs/zerolog/log"
import "log/slog"

func New() *PreParser {
res := &PreParser{}
Expand All @@ -22,7 +22,7 @@ func (ppr *PreParser) NewString(s string) {
func (ppr *PreParser) TailIndex(s string) int {
ppr.NewString(s)
if err := ppr.Parse(); err != nil {
log.Warn().Err(err).Msgf("Preparsing broke for string '%s'", s)
slog.Error("Preparsing failed", "error", err, "string", s)
return -1
}
ppr.Execute()
Expand Down
11 changes: 6 additions & 5 deletions gnparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ func (gnp gnparser) ParseName(s string) parsed.Parsed {
ver = "test_version"
}
sciNameNode := gnp.parser.PreprocessAndParse(
s, ver, gnp.cfg.IgnoreHTMLTags, gnp.cfg.WithCapitalization, gnp.cfg.WithCultivars, gnp.cfg.WithPreserveDiaereses,
s,
ver,
gnp.cfg.IgnoreHTMLTags,
gnp.cfg.WithCapitalization,
gnp.cfg.WithCultivars,
gnp.cfg.WithPreserveDiaereses,
)
res := sciNameNode.ToOutput(
gnp.cfg.WithDetails,
Expand Down Expand Up @@ -125,10 +130,6 @@ func (gnp gnparser) WebLogs() bool {
return gnp.cfg.WithWebLogs
}

func (gnp gnparser) WebLogsNsqdTCP() string {
return gnp.cfg.WebLogsNsqdTCP
}

// ChangeConfig allows change configuration of already created
// GNparser object.
func (gnp gnparser) ChangeConfig(opts ...Option) GNparser {
Expand Down
15 changes: 1 addition & 14 deletions gnparser/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/gnames/gnparser"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -66,10 +65,7 @@ func portFlag(cmd *cobra.Command) int {
}

func versionFlag(cmd *cobra.Command) bool {
version, err := cmd.Flags().GetBool("version")
if err != nil {
log.Fatal().Err(err)
}
version, _ := cmd.Flags().GetBool("version")
if version {
fmt.Printf("\nversion: %s\n\nbuild: %s\n\n",
gnparser.Version, gnparser.Build)
Expand All @@ -78,15 +74,6 @@ func versionFlag(cmd *cobra.Command) bool {
return false
}

func webLogsNsqdTCPFlag(cmd *cobra.Command) string {
u, err := cmd.Flags().GetString("nsqd-tcp")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return u
}

func withCapitalizeFlag(cmd *cobra.Command) {
b, err := cmd.Flags().GetBool("capitalize")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions gnparser/cmd/parse_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bufio"
"fmt"
"io"
"log/slog"
"sync"
"time"

"github.com/gnames/gnfmt"
"github.com/gnames/gnparser"
"github.com/gnames/gnparser/ent/parsed"
"github.com/rs/zerolog/log"
)

func parseBatch(
Expand Down Expand Up @@ -41,7 +41,7 @@ func parseBatch(
chOut <- gnp.ParseNames(batch[:count])
close(chOut)
if err := sc.Err(); err != nil {
log.Fatal().Err(err)
slog.Error("File reading failed", "error", err)
}
wg.Wait()
}
Expand Down
4 changes: 2 additions & 2 deletions gnparser/cmd/parse_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"context"
"fmt"
"io"
"log/slog"
"sync"
"time"

"github.com/gnames/gnparser"
"github.com/gnames/gnparser/ent/nameidx"
"github.com/gnames/gnparser/ent/parsed"
"github.com/rs/zerolog/log"
)

func getNames(
Expand All @@ -35,7 +35,7 @@ func getNames(
}
}()
if err := sc.Err(); err != nil {
log.Fatal().Err(err)
slog.Error("Cannot read data", "error", err)
}
return chIn
}
Expand Down
31 changes: 16 additions & 15 deletions gnparser/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd

import (
"fmt"
"log/slog"
"os"
"time"

Expand All @@ -11,8 +12,6 @@ import (
"github.com/gnames/gnparser/ent/parsed"
"github.com/gnames/gnparser/io/web"
"github.com/gnames/gnsys"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -86,12 +85,17 @@ gnparser -j 5 -p 8080
batchSize = cfg.BatchSize

if port != 0 {
log.Logger = zerolog.New(os.Stderr).With().
Str("gnApp", "gnparser").Logger()

// Create a JSON handler
handler := slog.NewJSONHandler(os.Stdout, nil)
logger := slog.New(handler).With(
slog.String("gnApp", "gnparser"),
)
slog.SetDefault(logger)

webopts := []gnparser.Option{
gnparser.OptFormat("compact"),
gnparser.OptWithWebLogs(withWebLogsFlag(cmd)),
gnparser.OptWebLogsNsqdTCP(webLogsNsqdTCPFlag(cmd)),
}
cfg = gnparser.NewConfig(webopts...)
gnp := gnparser.New(cfg)
Expand All @@ -102,7 +106,7 @@ gnparser -j 5 -p 8080

quiet, _ := cmd.Flags().GetBool("quiet")
if quiet {
zerolog.SetGlobalLevel(zerolog.Disabled)
slog.SetLogLoggerLevel(10)
}

if len(args) == 0 {
Expand Down Expand Up @@ -174,9 +178,6 @@ func init() {

rootCmd.Flags().
BoolP("species-group-cut", "", false, "cut autonym/species group names to species for stemmed version")

rootCmd.Flags().
StringP("nsqd-tcp", "", "", "an addresss pointing to NSQ TCP service for logs redirection (e.g. 127.0.0.1:4150)")
}

func processStdin(cmd *cobra.Command, cfg gnparser.Config) {
Expand All @@ -197,7 +198,7 @@ func checkStdin() bool {
stdInFile := os.Stdin
stat, err := stdInFile.Stat()
if err != nil {
log.Fatal().Err(err)
slog.Error("No stdin input", "error", err)
}
return (stat.Mode() & os.ModeCharDevice) == 0
}
Expand Down Expand Up @@ -234,7 +235,7 @@ func parse(
if exists {
f, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatal().Err(err)
slog.Error("Cannot open file", "error", err, "path", path)
}
if cfg.WithStream {
parseStream(gnp, f)
Expand Down Expand Up @@ -263,8 +264,8 @@ func progressLog(start time.Time, namesNum int) {
dur := float64(time.Since(start)) / float64(time.Second)
rate := float64(namesNum) / dur
rateStr := humanize.Comma(int64(rate))
log.Info().
Str("names/sec", rateStr).
Str("count", humanize.Comma(int64(namesNum))).
Msg("File parsing")
slog.Info("File parsing",
"names/sec", rateStr,
"count", humanize.Comma(int64(namesNum)),
)
}
14 changes: 5 additions & 9 deletions gnparser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@
package main

import (
"log/slog"
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/gnames/gnparser/gnparser/cmd"
"github.com/lmittmann/tint"
)

func main() {
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: "15:04:05",
},
)
handle := slog.New(tint.NewHandler(os.Stderr, nil))
slog.SetDefault(handle)

cmd.Execute()
}
6 changes: 3 additions & 3 deletions gnparser_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package gnparser

import (
"context"
"log/slog"
"sync"

"github.com/gnames/gnparser/ent/nameidx"
"github.com/gnames/gnparser/ent/parsed"
"github.com/gnames/gnparser/ent/parser"
"github.com/gnames/organizer"
"github.com/rs/zerolog/log"
)

// ParseNameStream takes an input channel of input.Name and
Expand Down Expand Up @@ -72,7 +72,7 @@ func sendOrdered(
var p parsed.Parsed
err := v.Unpack(&p)
if err != nil {
log.Fatal().Err(err)
slog.Error("Cannot reorganize data", "error", err)
}
select {
case <-ctx.Done():
Expand All @@ -94,7 +94,7 @@ func sendUnordered(
var p parsed.Parsed
err := v.Unpack(&p)
if err != nil {
log.Fatal().Err(err)
slog.Error("Cannot reorganize data", "error", err)
}
select {
case <-ctx.Done():
Expand Down
Loading

0 comments on commit f12d6e8

Please sign in to comment.