Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@ import (
"net/http"
"os"

"code.cloudfoundry.org/lager"
"github.com/pivotal-cf/brokerapi"
"go.uber.org/zap"

"github.com/cloudfoundry-incubator/blockhead/pkg/broker"
"github.com/cloudfoundry-incubator/blockhead/pkg/config"
"github.com/cloudfoundry-incubator/blockhead/pkg/util/logging"
)

func main() {
logger := lager.NewLogger("blockhead-broker")
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
zlogger, _ := zap.NewProduction()
defer zlogger.Sync() // flushes buffer on exit
zlogger.Info("starting blockhead broker")

lag := logging.NewLagerAdapter(zlogger)

if len(os.Args) < 2 {
logger.Fatal("main", errors.New("config file is missing"))
lag.Fatal("main", errors.New("config file is missing"))
}

configFilepath := os.Args[1]
cfg, err := config.NewConfig(configFilepath)
if err != nil {
logger.Fatal("main", err)
lag.Fatal("main", err)
}

broker := broker.NewBlockheadBroker(*cfg)
creds := brokerapi.BrokerCredentials{
Username: cfg.Username,
Password: cfg.Password,
}
brokerAPI := brokerapi.New(broker, logger, creds)
brokerAPI := brokerapi.New(broker, lag, creds)

http.Handle("/", brokerAPI)
logger.Fatal("http-listen", http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil))
lag.Fatal("http-listen", http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil))
}
83 changes: 83 additions & 0 deletions pkg/util/logging/lager_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package logging

import (
"fmt"

"code.cloudfoundry.org/lager"
"go.uber.org/zap"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import changed.

)

// LagerAdapter satisfies the lager.Logger interface with zap as the
// implementation.
type LagerAdapter struct {
originalLogger *zap.Logger
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all pointers now.

}

// NewLagerAdapter returns a new lager.Logger that uses zap underneath.
func NewLagerAdapter(zapLogger *zap.Logger) *LagerAdapter {
return &LagerAdapter{
originalLogger: zapLogger,
}
}

// RegisterSink is never used after initialization, so it does nothing.
func (l *LagerAdapter) RegisterSink(_ lager.Sink) {}

// Session returns a new logger with a nested session.
func (l *LagerAdapter) Session(task string, data ...lager.Data) lager.Logger {
tmpLogger := l.originalLogger.Named(task)

if data != nil {
tmpLogger = l.originalLogger.With(dataToFields(data)...)
}

return &LagerAdapter{
originalLogger: tmpLogger,
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is implemented correctly, and I mostly don't care.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Session is more useful in tracing and knowing where the logs are coming from in bigger projects where a lot of component logs come through or when you have many requests and you correlate all the logs for one request.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saying that, lager does a special thing with session so you can track it all the way down to where the original logger was created. With the adapter we won't have that so can't use Session in that way anymore.


// SessionName returns the name of the logger session
func (l *LagerAdapter) SessionName() string {
return "why would you want the name of the logger?"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't understand why this is a thing that exists.

}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything below is a direct copy.

// Debug logs a message at the debug log level.
func (l *LagerAdapter) Debug(action string, data ...lager.Data) {
l.originalLogger.Debug(action, dataToFields(data)...)
}

// Info logs a message at the info log level.
func (l *LagerAdapter) Info(action string, data ...lager.Data) {
l.originalLogger.Info(action, dataToFields(data)...)
}

// Error logs a message at the error log level.
func (l *LagerAdapter) Error(action string, err error, data ...lager.Data) {
l.originalLogger.Error(action, appendError(err, dataToFields(data))...)
}

// Fatal logs a message and exits with status 1.
func (l *LagerAdapter) Fatal(action string, err error, data ...lager.Data) {
l.originalLogger.Fatal(action, appendError(err, dataToFields(data))...)
}

// WithData returns a logger with newly added data.
func (l *LagerAdapter) WithData(data lager.Data) lager.Logger {
return &LagerAdapter{
originalLogger: l.originalLogger.With(dataToFields([]lager.Data{data})...),
}
}

func dataToFields(data []lager.Data) []zap.Field {
fields := []zap.Field{}
for _, datum := range data {
for key, value := range datum {
fields = append(fields, zap.String(key, fmt.Sprintf("%v", value)))
}
}
return fields
}

func appendError(err error, fields []zap.Field) []zap.Field {
return append(fields, zap.Error(err))
}
15 changes: 15 additions & 0 deletions vendor/go.uber.org/atomic/.codecov.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/go.uber.org/atomic/.github/PULL_REQUEST_TEMPLATE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/go.uber.org/atomic/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/go.uber.org/atomic/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/go.uber.org/atomic/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions vendor/go.uber.org/atomic/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions vendor/go.uber.org/atomic/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading