1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

Replace pkg/log with zerolog for normal logging #90

This commit is contained in:
James Hillyerd
2018-03-25 21:57:23 -07:00
parent 64ecd810b4
commit e2ba10c8ca
13 changed files with 177 additions and 153 deletions

View File

@@ -7,7 +7,7 @@ import (
"path/filepath"
"time"
"github.com/jhillyerd/inbucket/pkg/log"
"github.com/rs/zerolog/log"
)
// Message implements Message and contains a little bit of data about a
@@ -35,9 +35,12 @@ func (mb *mbox) newMessage() (*Message, error) {
// Delete old messages over messageCap
if mb.store.messageCap > 0 {
for len(mb.messages) >= mb.store.messageCap {
log.Infof("Mailbox %q over configured message cap", mb.name)
if err := mb.removeMessage(mb.messages[0].ID()); err != nil {
log.Errorf("Error deleting message: %s", err)
log.Info().Str("module", "storage").Str("mailbox", mb.name).
Msg("Mailbox over message cap")
id := mb.messages[0].ID()
if err := mb.removeMessage(id); err != nil {
log.Error().Str("module", "storage").Str("mailbox", mb.name).Str("id", id).
Err(err).Msg("Unable to delete message")
}
}
}

View File

@@ -10,10 +10,10 @@ import (
"time"
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/log"
"github.com/jhillyerd/inbucket/pkg/policy"
"github.com/jhillyerd/inbucket/pkg/storage"
"github.com/jhillyerd/inbucket/pkg/stringutil"
"github.com/rs/zerolog/log"
)
// Name of index file in each mailbox
@@ -57,7 +57,8 @@ func New(cfg config.Storage) (storage.Store, error) {
if _, err := os.Stat(mailPath); err != nil {
// Mail datastore does not yet exist
if err = os.MkdirAll(mailPath, 0770); err != nil {
log.Errorf("Error creating dir %q: %v", mailPath, err)
log.Error().Str("module", "storage").Str("path", mailPath).Err(err).
Msg("Error creating dir")
}
}
return &Store{path: path, mailPath: mailPath, messageCap: cfg.MailboxMsgCap}, nil

View File

@@ -9,8 +9,8 @@ import (
"path/filepath"
"sync"
"github.com/jhillyerd/inbucket/pkg/log"
"github.com/jhillyerd/inbucket/pkg/storage"
"github.com/rs/zerolog/log"
)
// mbox manages the mail for a specific user and correlates to a particular directory on disk.
@@ -87,7 +87,7 @@ func (mb *mbox) removeMessage(id string) error {
return nil
}
// There are still messages in the index
log.Tracef("Deleting %v", msg.rawPath())
log.Debug().Str("module", "storage").Str("path", msg.rawPath()).Msg("Deleting file")
return os.Remove(msg.rawPath())
}
@@ -104,7 +104,8 @@ func (mb *mbox) readIndex() error {
// Check if index exists
if _, err := os.Stat(mb.indexPath); err != nil {
// Does not exist, but that's not an error in our world
log.Tracef("Index %v does not exist (yet)", mb.indexPath)
log.Debug().Str("module", "storage").Str("path", mb.indexPath).
Msg("Index does not yet exist")
mb.indexLoaded = true
return nil
}
@@ -114,7 +115,8 @@ func (mb *mbox) readIndex() error {
}
defer func() {
if err := file.Close(); err != nil {
log.Errorf("Failed to close %q: %v", mb.indexPath, err)
log.Error().Str("module", "storage").Str("path", mb.indexPath).Err(err).
Msg("Failed to close")
}
}()
// Decode gob data
@@ -171,12 +173,13 @@ func (mb *mbox) writeIndex() error {
return err
}
if err := file.Close(); err != nil {
log.Errorf("Failed to close %q: %v", mb.indexPath, err)
log.Error().Str("module", "storage").Str("path", mb.indexPath).Err(err).
Msg("Failed to close")
return err
}
} else {
// No messages, delete index+maildir
log.Tracef("Removing mailbox %v", mb.path)
log.Debug().Str("module", "storage").Str("path", mb.path).Msg("Removing mailbox")
return mb.removeDir()
}
return nil
@@ -186,7 +189,8 @@ func (mb *mbox) writeIndex() error {
func (mb *mbox) createDir() error {
if _, err := os.Stat(mb.path); err != nil {
if err := os.MkdirAll(mb.path, 0770); err != nil {
log.Errorf("Failed to create directory %v, %v", mb.path, err)
log.Error().Str("module", "storage").Str("path", mb.path).Err(err).
Msg("Failed to create directory")
return err
}
}
@@ -223,10 +227,10 @@ func removeDirIfEmpty(path string) (removed bool) {
// Dir not empty
return false
}
log.Tracef("Removing dir %v", path)
log.Debug().Str("module", "storage").Str("path", path).Msg("Removing dir")
err = os.Remove(path)
if err != nil {
log.Errorf("Failed to remove %q: %v", path, err)
log.Error().Str("module", "storage").Str("path", path).Err(err).Msg("Failed to remove")
return false
}
return true

View File

@@ -7,7 +7,7 @@ import (
"time"
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/log"
"github.com/rs/zerolog/log"
)
var (
@@ -42,11 +42,12 @@ func init() {
rm.Set("RetainedSize", expRetainedSize)
rm.Set("SizeHist", expSizeHist)
log.AddTickerFunc(func() {
expRetentionDeletesHist.Set(log.PushMetric(retentionDeletesHist, expRetentionDeletesTotal))
expRetainedHist.Set(log.PushMetric(retainedHist, expRetainedCurrent))
expSizeHist.Set(log.PushMetric(sizeHist, expRetainedSize))
})
// TODO #90 move
// log.AddTickerFunc(func() {
// expRetentionDeletesHist.Set(log.PushMetric(retentionDeletesHist, expRetentionDeletesTotal))
// expRetainedHist.Set(log.PushMetric(retainedHist, expRetainedCurrent))
// expSizeHist.Set(log.PushMetric(sizeHist, expRetainedSize))
// })
}
// RetentionScanner looks for messages older than the configured retention period and deletes them.
@@ -79,16 +80,18 @@ func NewRetentionScanner(
// Start up the retention scanner if retention period > 0
func (rs *RetentionScanner) Start() {
if rs.retentionPeriod <= 0 {
log.Infof("Retention scanner disabled")
log.Info().Str("phase", "startup").Str("module", "storage").Msg("Retention scanner disabled")
close(rs.retentionShutdown)
return
}
log.Infof("Retention configured for %v", rs.retentionPeriod)
log.Info().Str("phase", "startup").Str("module", "storage").
Msgf("Retention configured for %v", rs.retentionPeriod)
go rs.run()
}
// run loops to kick off the scanner on the correct schedule
func (rs *RetentionScanner) run() {
slog := log.With().Str("module", "storage").Logger()
start := time.Now()
retentionLoop:
for {
@@ -96,7 +99,7 @@ retentionLoop:
since := time.Since(start)
if since < time.Minute {
dur := time.Minute - since
log.Tracef("Retention scanner sleeping for %v", dur)
slog.Debug().Msgf("Retention scanner sleeping for %v", dur)
select {
case <-rs.globalShutdown:
break retentionLoop
@@ -106,7 +109,7 @@ retentionLoop:
// Kickoff scan
start = time.Now()
if err := rs.DoScan(); err != nil {
log.Errorf("Error during retention scan: %v", err)
slog.Error().Err(err).Msg("Error during retention scan")
}
// Check for global shutdown
select {
@@ -115,13 +118,14 @@ retentionLoop:
default:
}
}
log.Tracef("Retention scanner shut down")
slog.Debug().Str("phase", "shutdown").Msg("Retention scanner shut down")
close(rs.retentionShutdown)
}
// DoScan does a single pass of all mailboxes looking for messages that can be purged.
func (rs *RetentionScanner) DoScan() error {
log.Tracef("Starting retention scan")
slog := log.With().Str("module", "storage").Logger()
slog.Debug().Msg("Starting retention scan")
cutoff := time.Now().Add(-1 * rs.retentionPeriod)
retained := 0
storeSize := int64(0)
@@ -129,9 +133,11 @@ func (rs *RetentionScanner) DoScan() error {
err := rs.ds.VisitMailboxes(func(messages []Message) bool {
for _, msg := range messages {
if msg.Date().Before(cutoff) {
log.Tracef("Purging expired message %v/%v", msg.Mailbox(), msg.ID())
slog.Debug().Str("mailbox", msg.Mailbox()).
Msgf("Purging expired message %v", msg.ID())
if err := rs.ds.RemoveMessage(msg.Mailbox(), msg.ID()); err != nil {
log.Errorf("Failed to purge message %v: %v", msg.ID(), err)
slog.Error().Str("mailbox", msg.Mailbox()).Err(err).
Msgf("Failed to purge message %v", msg.ID())
} else {
expRetentionDeletesTotal.Add(1)
}
@@ -142,7 +148,7 @@ func (rs *RetentionScanner) DoScan() error {
}
select {
case <-rs.globalShutdown:
log.Tracef("Retention scan aborted due to shutdown")
slog.Debug().Str("phase", "shutdown").Msg("Retention scan aborted due to shutdown")
return false
case <-time.After(rs.retentionSleep):
// Reduce disk thrashing