Skip to content

Commit

Permalink
Merge pull request #358 from rusq/logger
Browse files Browse the repository at this point in the history
dlog->slog
  • Loading branch information
rusq authored Nov 19, 2024
2 parents 28d8393 + 27bcf00 commit 77d4295
Show file tree
Hide file tree
Showing 82 changed files with 431 additions and 523 deletions.
47 changes: 12 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ There are four modes of operation (more on this in [User Guide][ug]):
1. Creating a Slack Export in Mattermost or Standard modes.
1. Emoji download mode.

Slackdump accepts two types of input (see `Dumping Conversations`_ section):
Slackdump accepts two types of input (see [Dumping
Conversations][usage-channels] section):

1. the URL/link of the channel or thread, OR
1. the ID of the channel.

[ug]: doc/README.rst
[usage-channels]: doc/usage-channels.rst

Quick Start
===========
Expand Down Expand Up @@ -161,51 +163,26 @@ func main() {
See [Package Documentation][godoc].

### Using Custom Logger
Slackdump uses a simple [rusq/dlog][dlog] as a default logger (it is a wrapper around
the standard logger that adds `Debug*` functions).
Slackdump uses a "log/slog" package, it defaults to "slog.Default()". Set the
default slog logger to the one you want to use.

If you want to use the same default logger that Slackdump uses in your
application, it is available as `logger.Default`.
If you were using `logger.Silent` before, you would need to
[implement][slog-handler-guide] a discarding [Handler][godoc-slog-handler] for slog.

No doubts that everyone has their own favourite logger that is better than other
miserable loggers. Please read below for instructions on plugging your
favourite logger.

[dlog]: https://github.com/rusq/dlog

#### Logrus
Good news is [logrus] can be plugged in straight away, as it implements the
`logger.Interface` out of the box.
[slog-handler-guide]: https://github.com/golang/example/blob/master/slog-handler-guide/README.md
[godoc-slog-handler]: https://pkg.go.dev/log/slog#Handler

```go
lg := logrus.New()
sd, err := slackdump.New(context.Background(), provider, WithLogger(lg))
if err != nil {
log.Print(err)
return
}
```

[logrus]: https://github.com/sirupsen/logrus


#### Glog and others
If you need to use some other logger, such as [glog], it is a matter of wrapping
the calls to satisfy the `logger.Interface` (defined in the [logger]
package), and then setting the `Logger` variable in `slackdump.Options` (see
[options.go]), or using `WithLogger` option.

[glog]:https://github.com/golang/glog
[logger]: logger/logger.go
[options.go]: options.go

## FAQ

#### Do I need to create a Slack application?

No, you don't. Just run the application and EZ-Login 3000 will take
care of the authentication or, alternatively, grab that token and
cookie from the browser Slack session. See `User Guide`_.
cookie from the browser Slack session. See [User's Guide][ug].



#### I'm getting "invalid_auth" error
Expand Down
27 changes: 12 additions & 15 deletions auth/browser/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"runtime"
Expand All @@ -13,7 +14,6 @@ import (

"github.com/playwright-community/playwright-go"
"github.com/rusq/slackdump/v3/auth/browser/pwcompat"
"github.com/rusq/slackdump/v3/logger"
)

const (
Expand All @@ -30,7 +30,7 @@ type Client struct {
verbose bool
}

var Logger logger.Interface = logger.Default
var Logger = slog.Default()

var (
installFn = playwright.Install
Expand All @@ -51,7 +51,7 @@ func New(workspace string, opts ...Option) (*Client, error) {
for _, opt := range opts {
opt(cl)
}
l().Debugf("browser=%s, timeout=%f", cl.br, cl.loginTimeout)
slog.Debug("New", "workspace", cl.workspace, "browser", cl.br, "timeout", cl.loginTimeout)
runopts := &playwright.RunOptions{
Browsers: []string{cl.br.String()},
Verbose: cl.verbose,
Expand All @@ -77,13 +77,14 @@ func (cl *Client) Authenticate(ctx context.Context) (string, []*http.Cookie, err
_b = playwright.Bool
)

lg := slog.Default()
pw, err := playwright.Run()
if err != nil {
return "", nil, err
}
defer func() {
if err := pw.Stop(); err != nil {
l().Printf("failed to stop playwright: %v", err)
lg.ErrorContext(ctx, "failed to stop playwright", "error", err)
}
}()

Expand Down Expand Up @@ -124,7 +125,7 @@ func (cl *Client) Authenticate(ctx context.Context) (string, []*http.Cookie, err
page.On("close", func() { trace.Log(ctx, "user", "page closed"); close(cl.pageClosed) })

uri := fmt.Sprintf("https://%s"+slackDomain, cl.workspace)
l().Debugf("opening browser URL=%s", uri)
lg.Debug("opening browser", "URL", uri)

if _, err := page.Goto(uri); err != nil {
return "", nil, err
Expand Down Expand Up @@ -219,13 +220,6 @@ func float2time(v float64) time.Time {
return time.Unix(int64(v), 0)
}

func l() logger.Interface {
if Logger == nil {
return logger.Default
}
return Logger
}

// pwRepair attempts to repair the playwright installation.
func pwRepair(runopts *playwright.RunOptions) error {
ad, err := pwcompat.NewAdapter(runopts)
Expand All @@ -249,18 +243,21 @@ func Reinstall(browser Browser, verbose bool) error {
}

func reinstall(runopts *playwright.RunOptions) error {
l().Debugf("reinstalling browser: %s", runopts.Browsers[0])
lg := slog.With("browser", runopts.Browsers[0])

lg.Debug("reinstalling browser")
ad, err := pwcompat.NewAdapter(runopts)
if err != nil {
return err
}
l().Debugf("removing %s", ad.DriverDirectory)
lg = lg.With("driver_directory", ad.DriverDirectory)
lg.Debug("removing driver directory")
if err := os.RemoveAll(ad.DriverDirectory); err != nil {
return err
}

// attempt to reinstall
l().Debugf("reinstalling %s", ad.DriverDirectory)
lg.Debug("reinstalling")
if err := installFn(runopts); err != nil {
// we did everything we could, but it still failed.
return err
Expand Down
8 changes: 4 additions & 4 deletions auth/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"os"
"time"

Expand All @@ -12,7 +13,6 @@ import (

"github.com/rusq/slackdump/v3/auth/auth_ui"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/logger"
)

// RODHeadlessTimeout is the default timeout for the headless login flow.
Expand Down Expand Up @@ -116,12 +116,12 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
}
defer cl.Close()

lg := logger.FromContext(ctx)
lg := slog.Default()
t := time.Now()
var sp simpleProvider
switch resp.Type {
case auth_ui.LInteractive, auth_ui.LUserBrowser:
lg.Printf("ℹ️ Initialising browser, once the browser appears, login as usual")
lg.InfoContext(ctx, "ℹ️ Initialising browser, once the browser appears, login as usual")
var err error
sp.Token, sp.Cookie, err = cl.Manual(ctx)
if err != nil {
Expand All @@ -135,7 +135,7 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
case auth_ui.LCancel:
return r, ErrCancelled
}
lg.Printf("✅ authenticated (time taken: %s)", time.Since(t))
lg.InfoContext(ctx, "✅ authenticated", "time_taken", time.Since(t).String())

return RodAuth{
simpleProvider: sp,
Expand Down
9 changes: 4 additions & 5 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ func (s *Session) getChannels(ctx context.Context, chanTypes []string, cb func(t
}
total += len(chans)

s.log.Printf("channels request #%5d, fetched: %4d, total: %8d (speed: %6.2f/sec, avg: %6.2f/sec)\n",
i, len(chans), total,
float64(len(chans))/float64(time.Since(reqStart).Seconds()),
float64(total)/float64(time.Since(fetchStart).Seconds()),
s.log.InfoContext(ctx, "channels", "request", i, "fetched", len(chans), "total", total,
"speed", float64(len(chans))/time.Since(reqStart).Seconds(),
"avg", float64(total)/time.Since(fetchStart).Seconds(),
)

if nextcur == "" {
s.log.Printf("channels fetch complete, total: %d channels", total)
s.log.InfoContext(ctx, "channels fetch complete", "total", total)
break
}

Expand Down
4 changes: 2 additions & 2 deletions channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slackdump
import (
"context"
"errors"
"log/slog"
"reflect"
"testing"

Expand All @@ -11,7 +12,6 @@ import (
"go.uber.org/mock/gomock"

"github.com/rusq/slackdump/v3/internal/network"
"github.com/rusq/slackdump/v3/logger"
"github.com/rusq/slackdump/v3/types"
)

Expand Down Expand Up @@ -80,7 +80,7 @@ func TestSession_getChannels(t *testing.T) {
sd := &Session{
client: mc,
cfg: tt.fields.config,
log: logger.Silent,
log: slog.Default(),
}

if tt.expectFn != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/slackdump/internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
_ "embed"
"errors"
"log/slog"
"time"

"github.com/rusq/fsadapter"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/rusq/slackdump/v3/internal/chunk/control"
"github.com/rusq/slackdump/v3/internal/chunk/transform/fileproc"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/logger"
"github.com/rusq/slackdump/v3/stream"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func RunArchive(ctx context.Context, cmd *base.Command, args []string) error {
base.SetExitStatus(base.SInitializationError)
return err
}
lg := logger.FromContext(ctx)
lg := cfg.Log
stream := sess.Stream(
stream.OptLatest(time.Time(cfg.Latest)),
stream.OptOldest(time.Time(cfg.Oldest)),
Expand All @@ -84,14 +84,14 @@ func RunArchive(ctx context.Context, cmd *base.Command, args []string) error {
base.SetExitStatus(base.SApplicationError)
return err
}
lg.Printf("Recorded workspace data to %s", cd.Name())
lg.Info("Recorded workspace data", "filename", cd.Name())

return nil
}

func resultLogger(lg logger.Interface) func(sr stream.Result) error {
func resultLogger(lg *slog.Logger) func(sr stream.Result) error {
return func(sr stream.Result) error {
lg.Printf("%s", sr)
lg.Info("stream", "result", sr.String())
return nil
}
}
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/archive/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package archive
import (
"context"
"errors"
"log/slog"
"strings"

"github.com/rusq/fsadapter"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/rusq/slackdump/v3/internal/chunk"
"github.com/rusq/slackdump/v3/internal/chunk/control"
"github.com/rusq/slackdump/v3/internal/chunk/transform/fileproc"
"github.com/rusq/slackdump/v3/logger"
"github.com/rusq/slackdump/v3/stream"
)

Expand Down Expand Up @@ -134,7 +134,7 @@ func initController(ctx context.Context, args []string) (*control.Controller, fu
}
defer cd.Close()

lg := logger.FromContext(ctx)
lg := slog.Default()
dl, stop := fileproc.NewDownloader(
ctx,
cfg.DownloadFiles,
Expand Down
5 changes: 2 additions & 3 deletions cmd/slackdump/internal/cfg/cache.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cfg

import (
"log/slog"
"os"
"path/filepath"

"github.com/rusq/dlog"
)

const (
Expand All @@ -15,7 +14,7 @@ const (
func ucd(ucdFn func() (string, error)) string {
ucd, err := ucdFn()
if err != nil {
dlog.Debug(err)
slog.Debug("ucd", "error", err)
return "."
}
return filepath.Join(ucd, cacheDirName)
Expand Down
7 changes: 4 additions & 3 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cfg
import (
"flag"
"fmt"
"log/slog"
"os"
"time"

Expand All @@ -12,7 +13,6 @@ import (
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/auth/browser"
"github.com/rusq/slackdump/v3/internal/network"
"github.com/rusq/slackdump/v3/logger"
)

const (
Expand All @@ -22,6 +22,7 @@ const (
var (
TraceFile string
LogFile string
JsonHandler bool
Verbose bool
AccessibleMode = (os.Getenv("ACCESSIBLE") != "" && os.Getenv("ACCESSIBLE") != "0")

Expand Down Expand Up @@ -55,8 +56,7 @@ var (
UserCacheRetention time.Duration
NoUserCache bool

Log logger.Interface

Log *slog.Logger = slog.Default()
// LoadSecrets is a flag that indicates whether to load secrets from the
// environment variables.
LoadSecrets bool
Expand Down Expand Up @@ -104,6 +104,7 @@ const (
func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.StringVar(&TraceFile, "trace", os.Getenv("TRACE_FILE"), "trace `filename`")
fs.StringVar(&LogFile, "log", os.Getenv("LOG_FILE"), "log `file`, if not specified, messages are printed to STDERR")
fs.BoolVar(&JsonHandler, "log-json", osenv.Value("JSON_LOG", false), "log in JSON format")
fs.BoolVar(&Verbose, "v", osenv.Value("DEBUG", false), "verbose messages")

if mask&OmitAuthFlags == 0 {
Expand Down
Loading

0 comments on commit 77d4295

Please sign in to comment.