Skip to content

Commit

Permalink
Use development logging when running a local build (#889)
Browse files Browse the repository at this point in the history
* Use development logging when running a local build

The [zap development
logger](https://godoc.org/go.uber.org/zap#NewDevelopmentConfig) provides nicer
console output as well as a few other different defaults for development.

To decide whether to use production or development it's checking if `Version`
has been set at build time. If not it assumes it's development. It may be
better not to conflate version number with build type in which case we could
add a separate build type. But first I wanted to see if the general idea
desirable.

* add log profile flag

* add build variable
  • Loading branch information
jrcamp authored Apr 29, 2020
1 parent 66462ee commit 2cc9fd9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ LINT=golangci-lint
IMPI=impi
GOSEC=gosec
STATIC_CHECK=staticcheck
# BUILD_TYPE should be one of (dev, release).
BUILD_TYPE?=release

GIT_SHA=$(shell git rev-parse --short HEAD)
BUILD_INFO_IMPORT_PATH=github.com/open-telemetry/opentelemetry-collector/internal/version
BUILD_X1=-X $(BUILD_INFO_IMPORT_PATH).GitHash=$(GIT_SHA)
ifdef VERSION
BUILD_X2=-X $(BUILD_INFO_IMPORT_PATH).Version=$(VERSION)
endif
BUILD_INFO=-ldflags "${BUILD_X1} ${BUILD_X2}"
BUILD_X3=-X $(BUILD_INFO_IMPORT_PATH).BuildType=$(BUILD_TYPE)
BUILD_INFO=-ldflags "${BUILD_X1} ${BUILD_X2} ${BUILD_X3}"

all-srcs:
@echo $(ALL_SRC) | tr ' ' '\n' | sort
Expand Down
18 changes: 18 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,30 @@ import (
"runtime"
)

const (
buildDev = "dev"
buildRelease = "release"
)

// Version variable will be replaced at link time after `make` has been run.
var Version = "latest"

// GitHash variable will be replaced at link time after `make` has been run.
var GitHash = "<NOT PROPERLY GENERATED>"

// BuildType should be one of (dev, release).
var BuildType = buildDev

// IsDevBuild returns true if this is a development (local) build.
func IsDevBuild() bool {
return BuildType == buildDev
}

// IsReleaseBuild returns true if this is a release build.
func IsReleaseBuild() bool {
return BuildType == buildRelease
}

// Info returns a formatted string, with linebreaks, intended to be displayed
// on stdout.
func Info() string {
Expand Down
24 changes: 22 additions & 2 deletions service/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@ import (

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

"github.com/open-telemetry/opentelemetry-collector/internal/version"
)

const (
logLevelCfg = "log-level"
logLevelCfg = "log-level"
logProfileCfg = "log-profile"
)

var (
// Command line pointer to logger level flag configuration.
loggerLevelPtr *string
loggerLevelPtr *string
loggerProfilePtr *string
)

func loggerFlags(flags *flag.FlagSet) {
loggerLevelPtr = flags.String(logLevelCfg, "INFO", "Output level of logs (DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL)")
loggerProfilePtr = flags.String(logProfileCfg, "", "Logging profile to use (dev, prod)")
}

func newLogger() (*zap.Logger, error) {
Expand All @@ -40,7 +45,22 @@ func newLogger() (*zap.Logger, error) {
if err != nil {
return nil, err
}

conf := zap.NewProductionConfig()

// Use logger profile if set on command line before falling back
// to default based on build type.
switch *loggerProfilePtr {
case "dev":
conf = zap.NewDevelopmentConfig()
case "prod":
conf = zap.NewProductionConfig()
default:
if version.IsDevBuild() {
conf = zap.NewDevelopmentConfig()
}
}

conf.Level.SetLevel(level)
return conf.Build()
}

0 comments on commit 2cc9fd9

Please sign in to comment.