Skip to content

Commit

Permalink
Merge pull request #47 from billsegall/logging-migration
Browse files Browse the repository at this point in the history
Logging migration
  • Loading branch information
lirm authored Feb 18, 2022
2 parents 083126f + c5633e8 commit 28ab69e
Show file tree
Hide file tree
Showing 24 changed files with 326 additions and 27 deletions.
2 changes: 1 addition & 1 deletion aeron/aeron.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"github.com/lirm/aeron-go/aeron/broadcast"
"github.com/lirm/aeron-go/aeron/counters"
"github.com/lirm/aeron-go/aeron/driver"
"github.com/lirm/aeron-go/aeron/logging"
rb "github.com/lirm/aeron-go/aeron/ringbuffer"
"github.com/lirm/aeron-go/aeron/util/memmap"
logging "github.com/op/go-logging"
)

// NewPublicationHandler is the handler type for new publication notification from the media driver
Expand Down
2 changes: 1 addition & 1 deletion aeron/clientconductor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/lirm/aeron-go/aeron/driver"
"github.com/lirm/aeron-go/aeron/idlestrategy"
"github.com/lirm/aeron-go/aeron/logbuffer"
logging "github.com/op/go-logging"
"github.com/lirm/aeron-go/aeron/logging"
)

var RegistrationStatus = struct {
Expand Down
2 changes: 1 addition & 1 deletion aeron/counters/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"errors"
"fmt"

logging "github.com/op/go-logging"
"github.com/lirm/aeron-go/aeron/logging"

"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/flyweight"
Expand Down
2 changes: 1 addition & 1 deletion aeron/driver/listeneradapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package driver

import (
logging "github.com/op/go-logging"
"github.com/lirm/aeron-go/aeron/logging"

"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/broadcast"
Expand Down
2 changes: 1 addition & 1 deletion aeron/logbuffer/logbuffers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package logbuffer

import (
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/aeron/util/memmap"
"github.com/op/go-logging"
"unsafe"
)

Expand Down
2 changes: 1 addition & 1 deletion aeron/logbuffer/logbuffers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package logbuffer
import (
"fmt"
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/aeron/util"
"github.com/lirm/aeron-go/aeron/util/memmap"
"github.com/op/go-logging"
"testing"
"unsafe"
)
Expand Down
232 changes: 232 additions & 0 deletions aeron/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// Copyright (C) 2021 Talos, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Provides a transition layer from "github.com/op/go-logging" to
// "go.uber.org/zap" to simply resolve some reentrancy issues in go-logging.
//
// This provides a largely api compatible layer so we can quickly
// drop in a replacement.
package logging

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sync"
)

// Zaplogger is a container to wrap zap logging with the parts of the go-logging API we use
type ZapLogger struct {
name string
sugar *zap.SugaredLogger
logger *zap.Logger
config zap.Config
}

// Mapping of go-logging to zap log levels. It's imperfect but good enough
const (
DEBUG = zapcore.DebugLevel
INFO = zapcore.InfoLevel
WARNING = zapcore.WarnLevel
NOTICE = zapcore.InfoLevel // Map to info
ERROR = zapcore.ErrorLevel
CRITICAL = zapcore.DPanicLevel
)

// The go-logging package creates named loggers and allows them to be
// accessed by name This is used in aeron to allow parent code to set
// the logging level of library components so we need to provide a
// mechanism for that
var namedLoggers sync.Map // [string]*ZapLogger

// MustGetLogger returns a new logger or panic()s
func MustGetLogger(name string) *ZapLogger {
z := new(ZapLogger)
z.name = name

z.config = aeronLoggingConfig()
logger, err := z.config.Build()
if err != nil {
panic("Failed to make logger")
}
z.logger = logger.Named(name)
z.sugar = z.logger.Sugar()

// Keep a reference
namedLoggers.Store(name, z)

return z
}

// newAeronEncoderConfig returns a default opinionated EncoderConfig for aeron
func newAeronEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}

// Default config
func aeronLoggingConfig() zap.Config {
return zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "console",
EncoderConfig: newAeronEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
}

// SetConfigAndRebuild so you can replace the default config
func (z *ZapLogger) SetConfigAndRebuild(c zap.Config) error {
z.config = c
logger, err := z.config.Build()
if err != nil {
return err
}

z.logger = logger.Named(z.name)
z.sugar = z.logger.Sugar()
return nil
}

// SetLevel on a named logger
func SetLevel(l zapcore.Level, name string) {
z, ok := namedLoggers.Load(name)
if ok {
zlogger := z.(*ZapLogger)
zlogger.SetLevel(l)
}
}

// GetLevel on a named logger
func GetLevel(name string) zapcore.Level {
z, ok := namedLoggers.Load(name)
if ok {
zlogger := z.(*ZapLogger)
return zlogger.GetLevel()
}

// Bogus return but that's the API we are emulating and shouldn't happen
return zapcore.InfoLevel
}

// Sugar returns the internal Sugared logger
func (z *ZapLogger) Sugar() *zap.SugaredLogger {
return z.sugar
}

// SetSugar sets our internal Sugared logger
func (z *ZapLogger) SetSugar(s *zap.SugaredLogger) {
z.sugar = s
}

// Logger returns the internal zap logger
func (z *ZapLogger) Logger() *zap.Logger {
return z.logger
}

// SetLogger sets the internal zap logger
func (z *ZapLogger) SetLogger(l *zap.Logger) {
z.logger = l
}

// SetLevel sets the log level at which we will log
func (z *ZapLogger) SetLevel(l zapcore.Level) {
z.config.Level.SetLevel(l)
}

// GetLevel returns the log level at which we will log
func (z *ZapLogger) GetLevel() zapcore.Level {
return z.config.Level.Level()
}

// IsEnabledFor returns true if logging is enabled for the specified level
func (z *ZapLogger) IsEnabledFor(level zapcore.Level) bool {
return level <= z.GetLevel()
}

// Fatalf logs a formatted string at log level Fatal and will then always exit()
func (z *ZapLogger) Fatalf(template string, args ...interface{}) {
z.sugar.Fatalf(template, args)
}

// Errorf logs a formatted string at log level Error
func (z *ZapLogger) Errorf(template string, args ...interface{}) {
z.sugar.Errorf(template, args)
}

// Warningf logs a formatted string at log level Warning
func (z *ZapLogger) Warningf(template string, args ...interface{}) {
z.sugar.Warnf(template, args)
}

// Infof logs a formatted string at log level Info
func (z *ZapLogger) Infof(template string, args ...interface{}) {
z.sugar.Infof(template, args)
}

// Noticef logs a formatted string at log level *Info*
func (z *ZapLogger) Noticef(template string, args ...interface{}) {
z.sugar.Infof(template, args)
}

// Debugf logs a formatted string at log level Debug
func (z *ZapLogger) Debugf(template string, args ...interface{}) {
z.sugar.Debugf(template, args)
}

// Fatal logs it's arguments at log level Fatal
func (z *ZapLogger) Fatal(args ...interface{}) {
z.sugar.Fatal(args)
}

// Error logs it's arguments at log level Error
func (z *ZapLogger) Error(args ...interface{}) {
z.sugar.Error(args)
}

// Warning logs it's arguments at log level Warning
func (z *ZapLogger) Warning(args ...interface{}) {
z.sugar.Warn(args)
}

// Info logs it's arguments at log level Info
func (z *ZapLogger) Info(args ...interface{}) {
z.sugar.Info(args)
}

// Notice logs it's arguments at log level *Info*
func (z *ZapLogger) Notice(args ...interface{}) {
z.sugar.Info(args)
}

// Debug logs it's arguments at log level Debug
func (z *ZapLogger) Debug(args ...interface{}) {
z.sugar.Debug(args)
}
36 changes: 36 additions & 0 deletions aeron/logging/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package logging

import (
"github.com/stretchr/testify/assert"
"testing"
)

const (
logname = "logtest"
)

func TestLogLevels(t *testing.T) {
assert := assert.New(t)
logger := MustGetLogger(logname)

// We initialzie to INFO
assert.EqualValues(INFO, GetLevel("logtest"))
assert.EqualValues(INFO, logger.GetLevel())

// Set to Warning (via name)
SetLevel(WARNING, logname)
assert.EqualValues(WARNING, GetLevel("logtest"))
assert.EqualValues(WARNING, logger.GetLevel())

// Set to Info (via logger)
logger.SetLevel(INFO)
assert.EqualValues(INFO, GetLevel("logtest"))
assert.EqualValues(INFO, logger.GetLevel())

// logger.Fatal("Log at Fatal")
logger.Error("Log at Error")
logger.Warning("Log at Warning")
logger.Info("Log at Info")
logger.Debug("Log at Debug") // Silent

}
2 changes: 1 addition & 1 deletion aeron/publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/logbuffer"
"github.com/lirm/aeron-go/aeron/logbuffer/term"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/aeron/util"
logging "github.com/op/go-logging"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions aeron/publication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package aeron

import (
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/util"
"github.com/lirm/aeron-go/aeron/counters"
"github.com/lirm/aeron-go/aeron/driver"
"github.com/lirm/aeron-go/aeron/logbuffer"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/aeron/ringbuffer"
"github.com/lirm/aeron-go/aeron/util"
"github.com/lirm/aeron-go/aeron/util/memmap"
"github.com/op/go-logging"
"os"
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion aeron/util/memmap/memmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"unsafe"

mapper "github.com/edsrzf/mmap-go"
"github.com/op/go-logging"
"github.com/lirm/aeron-go/aeron/logging"
)

// File is the wrapper around memory mapped file
Expand Down
2 changes: 1 addition & 1 deletion archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/lirm/aeron-go/aeron"
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/logbuffer"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/archive/codecs"
logging "github.com/op/go-logging"
"sync"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"flag"
"github.com/lirm/aeron-go/aeron"
"github.com/lirm/aeron-go/aeron/idlestrategy"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/archive/codecs"
logging "github.com/op/go-logging"
"log"
"math/rand"
"os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"github.com/lirm/aeron-go/aeron"
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/idlestrategy"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/archive"
"github.com/lirm/aeron-go/archive/examples"
logging "github.com/op/go-logging"
"os"
"time"
)
Expand Down
Loading

0 comments on commit 28ab69e

Please sign in to comment.