mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 13:44:37 +03:00
switch to slog.Logger for logging, for easier reuse of packages by external software
we don't want external software to include internal details like mlog. slog.Logger is/will be the standard. we still have mlog for its helper functions, and its handler that logs in concise logfmt used by mox. packages that are not meant for reuse still pass around mlog.Log for convenience. we use golang.org/x/exp/slog because we also support the previous Go toolchain version. with the next Go release, we'll switch to the builtin slog.
This commit is contained in:
29
import.go
29
import.go
@ -16,11 +16,11 @@ import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
"github.com/mjl-/mox/config"
|
||||
"github.com/mjl-/mox/message"
|
||||
"github.com/mjl-/mox/metrics"
|
||||
"github.com/mjl-/mox/mlog"
|
||||
"github.com/mjl-/mox/mox-"
|
||||
"github.com/mjl-/mox/store"
|
||||
)
|
||||
@ -119,10 +119,9 @@ func xcmdXImport(mbox bool, c *cmd) {
|
||||
}
|
||||
defer store.Switchboard()()
|
||||
|
||||
xlog := mlog.New("import")
|
||||
cconn, sconn := net.Pipe()
|
||||
clientctl := ctl{conn: cconn, r: bufio.NewReader(cconn), log: xlog}
|
||||
serverctl := ctl{conn: sconn, r: bufio.NewReader(sconn), log: xlog}
|
||||
clientctl := ctl{conn: cconn, r: bufio.NewReader(cconn), log: c.log}
|
||||
serverctl := ctl{conn: sconn, r: bufio.NewReader(sconn), log: c.log}
|
||||
go servectlcmd(context.Background(), &serverctl, func() {})
|
||||
|
||||
ctlcmdImport(&clientctl, mbox, account, args[1], args[2])
|
||||
@ -177,7 +176,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
if mbox {
|
||||
kind = "mbox"
|
||||
}
|
||||
ctl.log.Info("importing messages", mlog.Field("kind", kind), mlog.Field("account", account), mlog.Field("mailbox", mailbox), mlog.Field("source", src))
|
||||
ctl.log.Info("importing messages", slog.String("kind", kind), slog.String("account", account), slog.String("mailbox", mailbox), slog.String("source", src))
|
||||
|
||||
var err error
|
||||
var mboxf *os.File
|
||||
@ -186,7 +185,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
|
||||
// Open account, creating a database file if it doesn't exist yet. It must be known
|
||||
// in the configuration file.
|
||||
a, err := store.OpenAccount(account)
|
||||
a, err := store.OpenAccount(ctl.log, account)
|
||||
ctl.xcheck(err, "opening account")
|
||||
defer func() {
|
||||
if a != nil {
|
||||
@ -222,13 +221,13 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
if mbox {
|
||||
mboxf, err = os.Open(src)
|
||||
ctl.xcheck(err, "open mbox file")
|
||||
msgreader = store.NewMboxReader(store.CreateMessageTemp, src, mboxf, ctl.log)
|
||||
msgreader = store.NewMboxReader(ctl.log, store.CreateMessageTemp, src, mboxf)
|
||||
} else {
|
||||
mdnewf, err = os.Open(filepath.Join(src, "new"))
|
||||
ctl.xcheck(err, "open subdir new of maildir")
|
||||
mdcurf, err = os.Open(filepath.Join(src, "cur"))
|
||||
ctl.xcheck(err, "open subdir cur of maildir")
|
||||
msgreader = store.NewMaildirReader(store.CreateMessageTemp, mdnewf, mdcurf, ctl.log)
|
||||
msgreader = store.NewMaildirReader(ctl.log, store.CreateMessageTemp, mdnewf, mdcurf)
|
||||
}
|
||||
|
||||
tx, err := a.DB.Begin(ctx, true)
|
||||
@ -253,7 +252,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
}
|
||||
|
||||
if x != ctl.x {
|
||||
ctl.log.Error("import error", mlog.Field("panic", fmt.Errorf("%v", x)))
|
||||
ctl.log.Error("import error", slog.String("panic", fmt.Sprintf("%v", x)))
|
||||
debug.PrintStack()
|
||||
metrics.PanicInc(metrics.Import)
|
||||
} else {
|
||||
@ -263,7 +262,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
for _, id := range deliveredIDs {
|
||||
p := a.MessagePath(id)
|
||||
err := os.Remove(p)
|
||||
ctl.log.Check(err, "closing message file after import error", mlog.Field("path", p))
|
||||
ctl.log.Check(err, "closing message file after import error", slog.String("path", p))
|
||||
}
|
||||
|
||||
ctl.xerror(fmt.Sprintf("import error: %v", x))
|
||||
@ -282,7 +281,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
err := a.DeliverMessage(ctl.log, tx, m, mf, sync, notrain, nothreads)
|
||||
ctl.xcheck(err, "delivering message")
|
||||
deliveredIDs = append(deliveredIDs, m.ID)
|
||||
ctl.log.Debug("delivered message", mlog.Field("id", m.ID))
|
||||
ctl.log.Debug("delivered message", slog.Int64("id", m.ID))
|
||||
changes = append(changes, m.ChangeAddUID())
|
||||
}
|
||||
|
||||
@ -319,9 +318,9 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
mb.Add(m.MailboxCounts())
|
||||
|
||||
// Parse message and store parsed information for later fast retrieval.
|
||||
p, err := message.EnsurePart(ctl.log, false, msgf, m.Size)
|
||||
p, err := message.EnsurePart(ctl.log.Logger, false, msgf, m.Size)
|
||||
if err != nil {
|
||||
ctl.log.Infox("parsing message, continuing", err, mlog.Field("path", origPath))
|
||||
ctl.log.Infox("parsing message, continuing", err, slog.String("path", origPath))
|
||||
}
|
||||
m.ParsedBuf, err = json.Marshal(p)
|
||||
ctl.xcheck(err, "marshal parsed message structure")
|
||||
@ -345,7 +344,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
m.JunkFlagsForMailbox(mb, conf)
|
||||
if jf != nil && m.NeedsTraining() {
|
||||
if words, err := jf.ParseMessage(p); err != nil {
|
||||
ctl.log.Infox("parsing message for updating junk filter", err, mlog.Field("parse", ""), mlog.Field("path", origPath))
|
||||
ctl.log.Infox("parsing message for updating junk filter", err, slog.String("parse", ""), slog.String("path", origPath))
|
||||
} else {
|
||||
err = jf.Train(ctx, !m.Junk, words)
|
||||
ctl.xcheck(err, "training junk filter")
|
||||
@ -407,7 +406,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
err = tx.Commit()
|
||||
ctl.xcheck(err, "commit")
|
||||
tx = nil
|
||||
ctl.log.Info("delivered messages through import", mlog.Field("count", len(deliveredIDs)))
|
||||
ctl.log.Info("delivered messages through import", slog.Int("count", len(deliveredIDs)))
|
||||
deliveredIDs = nil
|
||||
|
||||
store.BroadcastChanges(a, changes)
|
||||
|
Reference in New Issue
Block a user