Skip to content

Commit

Permalink
log: expose output writer interface (thrasher-corp#845)
Browse files Browse the repository at this point in the history
This allows applications that import GCT to define their own
writer on their own terms.
  • Loading branch information
lrascao authored Nov 25, 2021
1 parent 00c1d73 commit 0e6b653
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 10 additions & 2 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ func NewSubLogger(name string) (*SubLogger, error) {
return nil, errEmptyLoggerName
}
name = strings.ToUpper(name)
if _, ok := subLoggers[name]; ok {
if _, ok := SubLoggers[name]; ok {
return nil, errSubLoggerAlreadyregistered
}
return registerNewSubLogger(name), nil
}

// SetOutput overrides the default output with a new writer
func (s *SubLogger) SetOutput(o io.Writer) {
RWM.Lock()
defer RWM.Unlock()

s.output = o
}

func newLogger(c *Config) *Logger {
return &Logger{
Timestamp: c.AdvancedSettings.TimeStampFormat,
Expand Down Expand Up @@ -75,7 +83,7 @@ func CloseLogger() error {
}

func validSubLogger(s string) (bool, *SubLogger) {
if v, found := subLoggers[s]; found {
if v, found := SubLoggers[s]; found {
return true, v
}
return false, nil
Expand Down
10 changes: 5 additions & 5 deletions log/logger_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func configureSubLogger(logger, levels string, output io.Writer) error {
logPtr.output = output

logPtr.Levels = splitLevel(levels)
subLoggers[logger] = logPtr
SubLoggers[logger] = logPtr

return nil
}
Expand All @@ -95,9 +95,9 @@ func SetupGlobalLogger() {
}
}

for x := range subLoggers {
subLoggers[x].Levels = splitLevel(GlobalLogConfig.Level)
subLoggers[x].output = getWriters(&GlobalLogConfig.SubLoggerConfig)
for x := range SubLoggers {
SubLoggers[x].Levels = splitLevel(GlobalLogConfig.Level)
SubLoggers[x].output = getWriters(&GlobalLogConfig.SubLoggerConfig)
}

logger = newLogger(GlobalLogConfig)
Expand Down Expand Up @@ -128,7 +128,7 @@ func registerNewSubLogger(logger string) *SubLogger {
}

temp.Levels = splitLevel("INFO|WARN|DEBUG|ERROR")
subLoggers[logger] = &temp
SubLoggers[logger] = &temp

return &temp
}
Expand Down
2 changes: 1 addition & 1 deletion log/sublogger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "io"

// Global vars related to the logger package
var (
subLoggers = map[string]*SubLogger{}
SubLoggers = map[string]*SubLogger{}

Global *SubLogger
BackTester *SubLogger
Expand Down

0 comments on commit 0e6b653

Please sign in to comment.