summaryrefslogtreecommitdiff
path: root/internal/io/dlog/loggers
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-10-09 21:10:29 +0300
committerPaul Buetow <paul@buetow.org>2021-10-10 13:36:41 +0300
commit97747ea0f3178f7f5890512d483fdccaa82846b0 (patch)
tree9ff1335ca26afc90e55fd6de416457e252d75a35 /internal/io/dlog/loggers
parent7a7169791a64190e1002e38bc9c04ad0d5c1ce1f (diff)
vetting and linting and some code restyling
Diffstat (limited to 'internal/io/dlog/loggers')
-rw-r--r--internal/io/dlog/loggers/factory.go6
-rw-r--r--internal/io/dlog/loggers/file.go17
-rw-r--r--internal/io/dlog/loggers/logger.go1
-rw-r--r--internal/io/dlog/loggers/strategy.go11
4 files changed, 21 insertions, 14 deletions
diff --git a/internal/io/dlog/loggers/factory.go b/internal/io/dlog/loggers/factory.go
index dda3ee6..415d7fb 100644
--- a/internal/io/dlog/loggers/factory.go
+++ b/internal/io/dlog/loggers/factory.go
@@ -9,11 +9,13 @@ import (
var factoryMap map[string]Logger
var factoryMutex sync.Mutex
+// Factory is there to retrieve a logger based on various settings.
func Factory(sourceName, loggerName string, rotationStrategy Strategy) Logger {
factoryMutex.Lock()
defer factoryMutex.Unlock()
- id := fmt.Sprintf("sourceName:%s,fileBase:%s,loggerName:%s", sourceName, rotationStrategy.FileBase, loggerName)
+ id := fmt.Sprintf("sourceName:%s,fileBase:%s,loggerName:%s", sourceName,
+ rotationStrategy.FileBase, loggerName)
if factoryMap == nil {
factoryMap = make(map[string]Logger)
}
@@ -36,10 +38,10 @@ func Factory(sourceName, loggerName string, rotationStrategy Strategy) Logger {
panic(fmt.Sprintf("Unsupported logger type '%s'", loggerName))
}
}
-
return singleton
}
+// FactoryRotate invokes a log rotation of all loggers.
func FactoryRotate() {
factoryMutex.Lock()
defer factoryMutex.Unlock()
diff --git a/internal/io/dlog/loggers/file.go b/internal/io/dlog/loggers/file.go
index 87280fd..94824fe 100644
--- a/internal/io/dlog/loggers/file.go
+++ b/internal/io/dlog/loggers/file.go
@@ -12,8 +12,7 @@ import (
"github.com/mimecast/dtail/internal/config"
)
-type fileWriter struct {
-}
+type fileWriter struct{}
type fileMessageBuf struct {
now time.Time
@@ -35,7 +34,7 @@ type file struct {
}
func newFile(strategy Strategy) *file {
- f := file{
+ return &file{
bufferCh: make(chan *fileMessageBuf, runtime.NumCPU()*100),
pauseCh: make(chan struct{}),
resumeCh: make(chan struct{}),
@@ -43,16 +42,17 @@ func newFile(strategy Strategy) *file {
flushCh: make(chan struct{}),
strategy: strategy,
}
-
- return &f
}
func (f *file) Start(ctx context.Context, wg *sync.WaitGroup) {
f.mutex.Lock()
- defer f.mutex.Unlock()
+ defer func() {
+ f.started = true
+ f.mutex.Unlock()
+ }()
- // Logger already started from another Goroutine.
if f.started {
+ // Logger already started from another Goroutine.
wg.Done()
return
}
@@ -68,7 +68,6 @@ func (f *file) Start(ctx context.Context, wg *sync.WaitGroup) {
go func() {
defer wg.Done()
-
for {
select {
case m := <-f.bufferCh:
@@ -84,8 +83,6 @@ func (f *file) Start(ctx context.Context, wg *sync.WaitGroup) {
}
}
}()
-
- f.started = true
}
func (f *file) Log(now time.Time, message string) {
diff --git a/internal/io/dlog/loggers/logger.go b/internal/io/dlog/loggers/logger.go
index c88900d..d4e85de 100644
--- a/internal/io/dlog/loggers/logger.go
+++ b/internal/io/dlog/loggers/logger.go
@@ -6,6 +6,7 @@ import (
"time"
)
+// Logger is there to plug in your own log implementation.
type Logger interface {
Log(now time.Time, message string)
LogWithColors(now time.Time, message, messageWithColors string)
diff --git a/internal/io/dlog/loggers/strategy.go b/internal/io/dlog/loggers/strategy.go
index a1b9355..25d10f0 100644
--- a/internal/io/dlog/loggers/strategy.go
+++ b/internal/io/dlog/loggers/strategy.go
@@ -5,19 +5,26 @@ import (
"path/filepath"
)
+// Rotation is the actual strategy used for log rotation..
type Rotation int
const (
- DailyRotation Rotation = iota
+ // DailyRotation tells DTail to rotate its logs on a daily basis or on SIGHUP.
+ DailyRotation Rotation = iota
+ // SignalRotation tells DTail to rotate its logs only on SIGHUP.
SignalRotation Rotation = iota
)
+// Strategy is a pair of the rotation and the file base.
type Strategy struct {
+ // Rotation is the actual rotation strategy used.
Rotation Rotation
+ // FileBase can be a name (e.g. "dserver", "dmap") when signal rotation is used.
FileBase string
}
-func GetStrategy(name string) Strategy {
+// NewStrategy returns the stratey based on its name.
+func NewStrategy(name string) Strategy {
switch name {
case "daily":
return Strategy{DailyRotation, ""}