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:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user