Skip to content

Commit

Permalink
Adding Uber zap leveled logging
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeMoosery committed Jan 9, 2021
1 parent b30332b commit fb9652c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 54 deletions.
25 changes: 21 additions & 4 deletions cmd/supertype/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package main

import (
"log"
"net/http"

"github.com/fatih/color"
"github.com/super-type/supertype/pkg/authenticating"
"github.com/super-type/supertype/pkg/consuming"
"github.com/super-type/supertype/pkg/dashboard"
"github.com/super-type/supertype/pkg/http/rest"
"github.com/super-type/supertype/pkg/producing"
"github.com/super-type/supertype/pkg/storage/dynamo"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Set up logging using Uber's Zap logger
func initLogger() *zap.Logger {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, _ := config.Build()
return logger
}

func main() {
// Initialize storage
persistentStorage := new(dynamo.Storage)

// Initialize logger
loggerManager := initLogger()
zap.ReplaceGlobals(loggerManager)
defer loggerManager.Sync() // flushes buffer, if any
logger := loggerManager.Sugar()

// Initialize services
authenticator := authenticating.NewService(persistentStorage)
dashboard := dashboard.NewService(persistentStorage)
Expand All @@ -25,6 +42,6 @@ func main() {

// Initialize routers and startup server
httpRouter := rest.Router(authenticator, producing, consuming, dashboard)
color.Cyan("Starting HTTP server on port 5000...")
log.Fatal(http.ListenAndServe(":5000", httpRouter))
logger.Info("Starting HTTP server on port 5000...")
logger.Fatal(http.ListenAndServe(":5000", httpRouter))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ require (
github.com/joho/godotenv v1.3.0
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,11 @@ go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opentelemetry.io/otel v0.11.0 h1:IN2tzQa9Gc4ZVKnTaMbPVcHjvzOdg5n9QfnmlqiET7E=
go.opentelemetry.io/otel v0.11.0/go.mod h1:G8UCk+KooF2HLkgo8RHX9epABH/aRGYET7gQOqBVdB0=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
10 changes: 5 additions & 5 deletions pkg/authenticating/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "errors"
// ErrVendorNotFound is used when a vendor is not found in the database
var ErrVendorNotFound = errors.New("Vendor not found")

// ErrVendorAlreadyExists is used when attempting to create an already-used username
var ErrVendorAlreadyExists = errors.New("Vendor already exists")
// ErrAlreadyExists is used when attempting to create an already-used username
var ErrAlreadyExists = errors.New("Vendor already exists")

// ErrUserNotFound is used when a user is not found in the database
var ErrUserNotFound = errors.New("User not found")
Expand All @@ -30,10 +30,10 @@ var ErrGeneratingCipherBlock = errors.New("Error generating cipher block")
var ErrGeneratingIV = errors.New("Error generating iv")

// ErrInvalidEmailLength is used when an invalid email address length is used to create an account
var ErrInvalidEmailLength = errors.New("Invalid email address length. Account creation failed.")
var ErrInvalidEmailLength = errors.New("Invalid email address length. Account creation failed")

// ErrInvalidEmailMatching is used when eamil regex matching fails
var ErrInvalidEmailMatching = errors.New("Invalid email - bad matching. Account creation failed.")
var ErrInvalidEmailMatching = errors.New("Invalid email - bad matching. Account creation failed")

// ErrInvalidEmail is used when an invalid email address is used to create an account
var ErrInvalidEmail = errors.New("Invalid email address used. Account creation failed.")
var ErrInvalidEmail = errors.New("Invalid email address used. Account creation failed")
Loading

0 comments on commit fb9652c

Please sign in to comment.