Skip to content

Commit

Permalink
define ares self logger interface (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxning committed Feb 1, 2019
1 parent a51b06d commit 22972f2
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 24 deletions.
11 changes: 5 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ import (
"github.com/spf13/cobra"
"github.com/uber/aresdb/clients"
"github.com/uber/aresdb/memutils"
"go.uber.org/zap"
)

// Options represents options for executing command
type Options struct {
DefaultCfg map[string]interface{}
ServerLogger *zap.SugaredLogger
QueryLogger *zap.SugaredLogger
ServerLogger common.Logger
QueryLogger common.Logger
Metrics common.Metrics
HttpWrappers []utils.HTTPHandlerWrapper
}
Expand All @@ -53,8 +52,8 @@ func Execute(setters ...Option) {

loggerFactory := common.NewLoggerFactory()
options := &Options{
ServerLogger: loggerFactory.GetDefaultLogger().Sugar(),
QueryLogger: loggerFactory.GetLogger("query").Sugar(),
ServerLogger: loggerFactory.GetDefaultLogger(),
QueryLogger: loggerFactory.GetLogger("query"),
Metrics: common.NewNoopMetrics(),
}

Expand Down Expand Up @@ -88,7 +87,7 @@ func Execute(setters ...Option) {
}

// start is the entry point of starting ares.
func start(cfg common.AresServerConfig, logger *zap.SugaredLogger, queryLogger *zap.SugaredLogger, metricsCfg common.Metrics, httpWrappers ...utils.HTTPHandlerWrapper) {
func start(cfg common.AresServerConfig, logger common.Logger, queryLogger common.Logger, metricsCfg common.Metrics, httpWrappers ...utils.HTTPHandlerWrapper) {
logger.With("config", cfg).Info("Bootstrapping service")

// Check whether we have a correct device running environment
Expand Down
127 changes: 121 additions & 6 deletions common/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,147 @@ import (
"go.uber.org/zap"
)

// Logger is a general logger interface
type Logger interface {
// Log at debug level
Debug(args ...interface{})

// Log at debug level with fmt.Printf-like formatting
Debugf(format string, args ...interface{})

// Log at info level
Info(args ...interface{})

// Log at info level with fmt.Printf-like formatting
Infof(format string, args ...interface{})

// Log at warning level
Warn(args ...interface{})

// Log at warning level with fmt.Printf-like formatting
Warnf(format string, args ...interface{})

// Log at error level
Error(args ...interface{})

// Log at error level with fmt.Printf-like formatting
Errorf(format string, args ...interface{})

// Log at fatal level, then terminate process (irrecoverable)
Fatal(args ...interface{})

// Log at fatal level with fmt.Printf-like formatting, then terminate process (irrecoverable)
Fatalf(format string, args ...interface{})

// Log at panic level, then panic (recoverable)
Panic(args ...interface{})

// Log at panic level with fmt.Printf-like formatting, then panic (recoverable)
Panicf(format string, args ...interface{})

// Return a logger with the specified key-value pair set, to be logged in a subsequent normal logging call
With(args ...interface{}) Logger
}

// LoggerFactory defines the log factory ares needs.
type LoggerFactory interface {
// GetDefaultLogger returns the default logger.
GetDefaultLogger() *zap.Logger
GetDefaultLogger() Logger
// GetLogger returns logger given the logger name.
GetLogger(name string) *zap.Logger
GetLogger(name string) Logger
}

// ZapLoggerFactory is the stdlog implementation of LoggerFactory
type ZapLoggerFactory struct {
logger *zap.Logger
logger *ZapLogger
}

// NewLoggerFactory creates a default zap LoggerFactory implementation.
func NewLoggerFactory() LoggerFactory {
return &ZapLoggerFactory{
logger: zap.NewExample(),
&ZapLogger{
zap.NewExample().Sugar(),
},
}
}

// GetDefaultLogger returns the default zap logger.
func (r *ZapLoggerFactory) GetDefaultLogger() *zap.Logger {
func (r *ZapLoggerFactory) GetDefaultLogger() Logger {
return r.logger
}

// GetLogger of ZapLoggerFactory ignores the given name and just return the default logger.
func (r *ZapLoggerFactory) GetLogger(name string) *zap.Logger {
func (r *ZapLoggerFactory) GetLogger(name string) Logger {
return r.logger
}

// ZapLogger is wrapper of zap
type ZapLogger struct {
sugaredLogger *zap.SugaredLogger
}

// Debug is log at debug level
func (z *ZapLogger) Debug(args ...interface{}) {
z.sugaredLogger.Debug(args)
}

// Debugf is log at debug level with fmt.Printf-like formatting
func (z *ZapLogger) Debugf(format string, args ...interface{}) {
z.sugaredLogger.Debugf(format, args)
}

// Info is log at info level
func (z *ZapLogger) Info(args ...interface{}) {
z.sugaredLogger.Info(args)
}

// Infof is log at info level with fmt.Printf-like formatting
func (z *ZapLogger) Infof(format string, args ...interface{}) {
z.sugaredLogger.Infof(format, args)
}

// Warn is log at warning level
func (z *ZapLogger) Warn(args ...interface{}) {
z.sugaredLogger.Warn(args)
}

// Warnf is log at warning level with fmt.Printf-like formatting
func (z *ZapLogger) Warnf(format string, args ...interface{}) {
z.sugaredLogger.Warnf(format, args)
}

// Error is log at error level
func (z *ZapLogger) Error(args ...interface{}) {
z.sugaredLogger.Error(args)
}

// Errorf is log at error level with fmt.Printf-like formatting
func (z *ZapLogger) Errorf(format string, args ...interface{}) {
z.sugaredLogger.Errorf(format, args)
}

// Fatal is log at fatal level, then terminate process (irrecoverable)
func (z *ZapLogger) Fatal(args ...interface{}) {
z.sugaredLogger.Fatal(args)
}

// Fatalf is log at fatal level with fmt.Printf-like formatting, then terminate process (irrecoverable)
func (z *ZapLogger) Fatalf(format string, args ...interface{}) {
z.sugaredLogger.Fatalf(format, args)
}

// Panic is log at panic level, then panic (recoverable)
func (z *ZapLogger) Panic(args ...interface{}) {
z.sugaredLogger.Panic(args)
}

// Panicf is log at panic level with fmt.Printf-like formatting, then panic (recoverable)
func (z *ZapLogger) Panicf(format string, args ...interface{}) {
z.sugaredLogger.Panicf(format, args)
}

// With returns a logger with the specified key-value pair set, to be logged in a subsequent normal logging call
func (z *ZapLogger) With(args ...interface{}) Logger {
z.sugaredLogger = z.sugaredLogger.With(args...)
return z
}
2 changes: 1 addition & 1 deletion query/aql_compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = ginkgo.Describe("AQL compiler", func() {
}
utils.Init(common.AresServerConfig{Query: common.QueryConfig{TimezoneTable: common.TimezoneConfig{
TableName: "api_cities",
}}}, common.NewLoggerFactory().GetDefaultLogger().Sugar(), common.NewLoggerFactory().GetDefaultLogger().Sugar(), tally.NewTestScope("test", nil))
}}}, common.NewLoggerFactory().GetDefaultLogger(), common.NewLoggerFactory().GetDefaultLogger(), tally.NewTestScope("test", nil))

qc.processTimezone()
Ω(qc.Error).Should(BeNil())
Expand Down
4 changes: 2 additions & 2 deletions query/aql_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ var _ = ginkgo.Describe("aql_processor", func() {

utils.Init(common.AresServerConfig{Query: common.QueryConfig{TimezoneTable: common.TimezoneConfig{
TableName: "tableName",
}}}, common.NewLoggerFactory().GetDefaultLogger().Sugar(), common.NewLoggerFactory().GetDefaultLogger().Sugar(), tally.NewTestScope("test", nil))
}}}, common.NewLoggerFactory().GetDefaultLogger(), common.NewLoggerFactory().GetDefaultLogger(), tally.NewTestScope("test", nil))
qc := &AQLQueryContext{
timezoneTable: timezoneTableContext{tableColumn: "timezone"},
}
Expand Down Expand Up @@ -1136,7 +1136,7 @@ var _ = ginkgo.Describe("aql_processor", func() {

utils.Init(common.AresServerConfig{Query: common.QueryConfig{TimezoneTable: common.TimezoneConfig{
TableName: timezoneTable,
}}}, common.NewLoggerFactory().GetDefaultLogger().Sugar(), common.NewLoggerFactory().GetDefaultLogger().Sugar(), tally.NewTestScope("test", nil))
}}}, common.NewLoggerFactory().GetDefaultLogger(), common.NewLoggerFactory().GetDefaultLogger(), tally.NewTestScope("test", nil))

qc := &AQLQueryContext{}
q := &AQLQuery{
Expand Down
2 changes: 1 addition & 1 deletion query/time_bucketizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var _ = ginkgo.Describe("Time Bucketizer", func() {

utils.Init(common.AresServerConfig{Query: common.QueryConfig{TimezoneTable: common.TimezoneConfig{
TableName: "",
}}}, common.NewLoggerFactory().GetDefaultLogger().Sugar(), common.NewLoggerFactory().GetDefaultLogger().Sugar(), tally.NewTestScope("test", nil))
}}}, common.NewLoggerFactory().GetDefaultLogger(), common.NewLoggerFactory().GetDefaultLogger(), tally.NewTestScope("test", nil))
qc.timezoneTable.tableColumn = "timezone"
qc.timezoneTable.tableAlias = defaultTimezoneTableAlias
qc.TableIDByAlias = map[string]int{defaultTimezoneTableAlias: 0}
Expand Down
15 changes: 7 additions & 8 deletions utils/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import (
"github.com/spf13/viper"
"github.com/uber-go/tally"
"github.com/uber/aresdb/common"
"go.uber.org/zap"
)

// stores all common components together to avoid scattered references.
var (
logger *zap.SugaredLogger
queryLogger *zap.SugaredLogger
logger common.Logger
queryLogger common.Logger
reporterFactory *ReporterFactory
config common.AresServerConfig
)
Expand All @@ -36,8 +35,8 @@ func init() {

// ResetDefaults reset default config, logger and metrics settings
func ResetDefaults() {
logger = common.NewLoggerFactory().GetDefaultLogger().Sugar()
queryLogger = common.NewLoggerFactory().GetDefaultLogger().Sugar()
logger = common.NewLoggerFactory().GetDefaultLogger()
queryLogger = common.NewLoggerFactory().GetDefaultLogger()
scope := tally.NewTestScope("test", nil)
reporterFactory = NewReporterFactory(scope)

Expand All @@ -49,20 +48,20 @@ func ResetDefaults() {
}

// Init loads application specific common components settings.
func Init(c common.AresServerConfig, l *zap.SugaredLogger, ql *zap.SugaredLogger, s tally.Scope) {
func Init(c common.AresServerConfig, l common.Logger, ql common.Logger, s tally.Scope) {
config = c
logger = l
queryLogger = ql
reporterFactory = NewReporterFactory(s)
}

// GetLogger returns the logger.
func GetLogger() *zap.SugaredLogger {
func GetLogger() common.Logger {
return logger
}

// GetQueryLogger returns the logger for query.
func GetQueryLogger() *zap.SugaredLogger {
func GetQueryLogger() common.Logger {
return queryLogger
}

Expand Down

0 comments on commit 22972f2

Please sign in to comment.