summaryrefslogtreecommitdiff
path: root/internal/io/dlog
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
parent7a7169791a64190e1002e38bc9c04ad0d5c1ce1f (diff)
vetting and linting and some code restyling
Diffstat (limited to 'internal/io/dlog')
-rw-r--r--internal/io/dlog/dlog.go22
-rw-r--r--internal/io/dlog/level.go5
-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
6 files changed, 43 insertions, 19 deletions
diff --git a/internal/io/dlog/dlog.go b/internal/io/dlog/dlog.go
index 28e6882..da67585 100644
--- a/internal/io/dlog/dlog.go
+++ b/internal/io/dlog/dlog.go
@@ -82,7 +82,7 @@ func new(sourceProcess, sourcePackage source.Source) *DLog {
if err != nil {
panic(err)
}
- strategy := loggers.GetStrategy(config.Common.LogStrategy)
+ strategy := loggers.NewStrategy(config.Common.LogStrategy)
loggerName := config.Common.Logger
level := newLevel(config.Common.LogLevel)
@@ -153,6 +153,7 @@ func (d *DLog) writeArgStrings(sb *strings.Builder, args []interface{}) {
}
}
+// FatalPanic terminates the process with a fatal error.
func (d *DLog) FatalPanic(args ...interface{}) {
d.log(Fatal, args)
d.Flush()
@@ -162,42 +163,51 @@ func (d *DLog) FatalPanic(args ...interface{}) {
panic(sb.String())
}
+// Fatal logs a fatal error.
func (d *DLog) Fatal(args ...interface{}) string {
return d.log(Fatal, args)
}
+// Error logging.
func (d *DLog) Error(args ...interface{}) string {
return d.log(Error, args)
}
+// Warn logs a warning message.
func (d *DLog) Warn(args ...interface{}) string {
return d.log(Warn, args)
}
+// Info logging.
func (d *DLog) Info(args ...interface{}) string {
return d.log(Info, args)
}
+// Verbose logging.
func (d *DLog) Verbose(args ...interface{}) string {
return d.log(Verbose, args)
}
+// Debug logging.
func (d *DLog) Debug(args ...interface{}) string {
return d.log(Debug, args)
}
+// Trace logging.
func (d *DLog) Trace(args ...interface{}) string {
_, file, line, _ := runtime.Caller(1)
args = append(args, fmt.Sprintf("at %s:%d", file, line))
return d.log(Trace, args)
}
+// Devel used for development purpose only logging (e.g. "print" debugging).
func (d *DLog) Devel(args ...interface{}) string {
_, file, line, _ := runtime.Caller(1)
args = append(args, fmt.Sprintf("at %s:%d", file, line))
return d.log(Devel, args)
}
+// Raw message logging.
func (d *DLog) Raw(message string) string {
if !config.Client.TermColorsEnable || !d.logger.SupportsColors() {
d.logger.Log(time.Now(), message)
@@ -207,6 +217,7 @@ func (d *DLog) Raw(message string) string {
return message
}
+// Mapreduce logging.
func (d *DLog) Mapreduce(table string, data map[string]interface{}) string {
args := make([]interface{}, len(data)+1)
@@ -251,6 +262,11 @@ func (d *DLog) Mapreduce(table string, data map[string]interface{}) string {
return d.log(Info, args)
}
-func (d *DLog) Flush() { d.logger.Flush() }
-func (d *DLog) Pause() { d.logger.Pause() }
+// Flush the log buffers.
+func (d *DLog) Flush() { d.logger.Flush() }
+
+// Pause the logging.
+func (d *DLog) Pause() { d.logger.Pause() }
+
+// Resume the logging.
func (d *DLog) Resume() { d.logger.Resume() }
diff --git a/internal/io/dlog/level.go b/internal/io/dlog/level.go
index 248ad83..0971094 100644
--- a/internal/io/dlog/level.go
+++ b/internal/io/dlog/level.go
@@ -7,6 +7,7 @@ import (
type level int
+// Available log levels.
const (
Fatal level = iota
Error level = iota
@@ -20,7 +21,8 @@ const (
All level = iota
)
-var allLevels = []level{Fatal, Error, Warn, Info, Default, Verbose, Debug, Devel, Trace, All}
+var allLevels = []level{Fatal, Error, Warn, Info, Default, Verbose, Debug,
+ Devel, Trace, All}
func newLevel(l string) level {
switch strings.ToLower(l) {
@@ -73,6 +75,5 @@ func (l level) String() string {
case All:
return "ALL"
}
-
panic("Unknown log level " + fmt.Sprintf("%d", l))
}
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, ""}