Skip to content

Commit

Permalink
Add support to configure all levels at once with the 'all' scope (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
nacx authored Jan 28, 2021
1 parent eb04d1e commit 0fba4e2
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 83 deletions.
16 changes: 0 additions & 16 deletions .circleci/config.yml

This file was deleted.

32 changes: 32 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021 Tetrate Labs
#
# 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.

name: build

on: [push, pull_request]

env:
GOPROXY: https://proxy.golang.org

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.15
- run: make build
- run: make test
- run: make lint
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor
.idea
.vscode

bin/
19 changes: 7 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
# Packages to build
PKGS := $(shell go list -f '{{if .GoFiles}}{{.ImportPath}}{{end}}' ./...)
# Tests to run
TESTS := $(shell go list -f '{{if .TestGoFiles}}{{.ImportPath}}{{end}}' ./...)

# Enable go modules when building, even if the repo is copied into a GOPATH.
export GO111MODULE=on
PKGS := ./...
TEST_OPTS ?=

build:
@echo "--- build ---"
@go build -v $(PKGS)
@go vet $(PKGS)
go build -v $(PKGS)

test:
@echo "--- test ---"
@go test $(TESTS)
go test $(TEST_OPTS) $(PKGS)

LINTER := bin/golangci-lint
$(LINTER):
@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.11.2
wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b bin v1.23.6

lint: $(LINTER) ./bin/.golangci.yml
lint: $(LINTER) golangci.yml
@echo "--- lint ---"
@$(LINTER) run --config ./bin/.golangci.yml
$(LINTER) run --config golangci.yml

.PHONY: build test lint
1 change: 0 additions & 1 deletion bin/.gitignore

This file was deleted.

29 changes: 0 additions & 29 deletions bin/.golangci.yml

This file was deleted.

61 changes: 38 additions & 23 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,44 +205,59 @@ func updateScopes(options *Options, core zapcore.Core, errSink zapcore.WriteSync
allScopes := Scopes()

// update the output levels of all scopes
levels := strings.Split(options.outputLevels, ",")
for _, sl := range levels {
s, l, err := convertScopedLevel(sl)
if err != nil {
return err
if err := processLevels(allScopes, options.outputLevels, func(s *Scope, l Level) { s.SetOutputLevel(l) }); err != nil {
return err
}

// update the stack tracing levels of all scopes
if err := processLevels(allScopes, options.stackTraceLevels, func(s *Scope, l Level) { s.SetStackTraceLevel(l) }); err != nil {
return err
}

// update the caller location setting of all scopes
sc := strings.Split(options.logCallers, ",")
for _, s := range sc {
if s == "" {
continue
}

if s == OverrideScopeName {
// ignore everything else and just apply the override value
for _, scope := range allScopes {
scope.SetLogCallers(true)
}

return nil
}

if scope, ok := allScopes[s]; ok {
scope.SetOutputLevel(l)
scope.SetLogCallers(true)
} else {
return fmt.Errorf("unknown scope '%s' specified", s)
}
}

// update the stack tracing levels of all scopes
levels = strings.Split(options.stackTraceLevels, ",")
return nil
}

// processLevels breaks down an argument string into a set of scope & levels and then
// tries to apply the result to the scopes. It supports the use of a global override.
func processLevels(allScopes map[string]*Scope, arg string, setter func(*Scope, Level)) error {
levels := strings.Split(arg, ",")
for _, sl := range levels {
s, l, err := convertScopedLevel(sl)
if err != nil {
return err
}

if scope, ok := allScopes[s]; ok {
scope.SetStackTraceLevel(l)
} else {
return fmt.Errorf("unknown scope '%s' specified", s)
}
}

// update the caller location setting of all scopes
sc := strings.Split(options.logCallers, ",")
for _, s := range sc {
if s == "" {
continue
}

if scope, ok := allScopes[s]; ok {
scope.SetLogCallers(true)
setter(scope, l)
} else if s == OverrideScopeName {
// override replaces everything
for _, scope := range allScopes {
setter(scope, l)
}
return nil
} else {
return fmt.Errorf("unknown scope '%s' specified", s)
}
Expand Down
48 changes: 48 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func TestTimestampProperMicros(t *testing.T) {
}

func TestOddballs(t *testing.T) {
resetGlobals()

o := DefaultOptions()
_ = Configure(o)

Expand Down Expand Up @@ -315,6 +317,47 @@ func TestCapture(t *testing.T) {
t.Error("Could not find stack trace info in output")
}

func TestOverrides(t *testing.T) {
resetGlobals()
s := RegisterScope("TestOverrides", "For testing", 0)

o := DefaultOptions()
o.outputLevels = "default:debug,all:info"
if err := Configure(o); err != nil {
t.Errorf("Expecting success, got %v", err)
}
if s.GetOutputLevel() != InfoLevel {
t.Errorf("Expecting InfoLevel, got %v", s.GetOutputLevel())
}
if defaultScope.GetOutputLevel() != InfoLevel {
t.Errorf("Expecting InfoLevel, got %v", defaultScope.GetOutputLevel())
}

o = DefaultOptions()
o.stackTraceLevels = "default:debug,all:info"
if err := Configure(o); err != nil {
t.Errorf("Expecting success, got %v", err)
}
if s.GetStackTraceLevel() != InfoLevel {
t.Errorf("Expecting InfoLevel, got %v", s.GetStackTraceLevel())
}
if defaultScope.GetStackTraceLevel() != InfoLevel {
t.Errorf("Expecting InfoLevel, got %v", defaultScope.GetStackTraceLevel())
}

o = DefaultOptions()
o.logCallers = "all"
if err := Configure(o); err != nil {
t.Errorf("Expecting success, got %v", err)
}
if !s.GetLogCallers() {
t.Error("Expecting true, got false")
}
if !defaultScope.GetLogCallers() {
t.Error("Expecting true, got false")
}
}

// Runs the given function while capturing everything sent to stdout
func captureStdout(f func()) ([]string, error) {
tf, err := ioutil.TempFile("", "log_test")
Expand All @@ -341,3 +384,8 @@ func captureStdout(f func()) ([]string, error) {

return strings.Split(string(content), "\n"), nil
}

func resetGlobals() {
scopes = make(map[string]*Scope, 1)
defaultScope = registerDefaultScope()
}
6 changes: 5 additions & 1 deletion default.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import (
"go.uber.org/zap/zapcore"
)

var defaultScope = RegisterScope(DefaultScopeName, "Unscoped logging messages.", 0)
func registerDefaultScope() *Scope {
return RegisterScope(DefaultScopeName, "Unscoped logging messages.", 1)
}

var defaultScope = registerDefaultScope()

// Error outputs a message at error level.
func Error(msg string, fields ...zapcore.Field) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/tetratelabs/log

go 1.15

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
39 changes: 39 additions & 0 deletions golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
linters:
enable:
- deadcode
- errcheck
- goconst
- golint
- ineffassign
- lll
- maligned
- misspell
- structcheck
- unconvert
- varcheck
- govet
- goimports
- prealloc
- unused
- staticcheck
- gosimple
- megacheck
disable:
- interfacer
linters-settings:
lll:
line-length: 170
goconst:
min-occurrences: 4
govet:
check-shadowing: true
run:
deadline: 10m
skip-dirs:
- k8s/istioapis/generated
- k8s/tsbapis/generated
issues:
exclude:
# staticcheck
- 'SA1019: Package github.com/golang/protobuf/proto is deprecated: Use the "google.golang.org/protobuf/proto" package instead.'
- 'SA1019: Package github.com/golang/protobuf/jsonpb is deprecated: Use the "google.golang.org/protobuf/encoding/protojson" package instead.'
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
const (
// DefaultScopeName defines the name of the default scope.
DefaultScopeName = "default"
OverrideScopeName = "all"
defaultOutputLevel = InfoLevel
defaultStackTraceLevel = NoneLevel
defaultOutputPath = "stdout"
Expand Down Expand Up @@ -342,6 +343,7 @@ func (o *Options) AttachToFlagSet(fs *pflag.FlagSet) *pflag.FlagSet {
for name := range allScopes {
keys = append(keys, name)
}
keys = append(keys, OverrideScopeName)
sort.Strings(keys)
s := strings.Join(keys, ", ")

Expand Down
6 changes: 6 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
)

func TestOpts(t *testing.T) {
resetGlobals()

cases := []struct {
cmdLine string
result Options
Expand Down Expand Up @@ -299,6 +301,7 @@ func TestOpts(t *testing.T) {
}

func TestSetLevel(t *testing.T) {
resetGlobals()
_ = RegisterScope("TestSetLevel", "", 0)

cases := []struct {
Expand Down Expand Up @@ -357,6 +360,8 @@ func TestSetLevel(t *testing.T) {
}

func TestGetLevel(t *testing.T) {
resetGlobals()

cases := []struct {
levels string
scope string
Expand Down Expand Up @@ -421,6 +426,7 @@ func TestGetLevel(t *testing.T) {
}

func TestLogCallers(t *testing.T) {
resetGlobals()
o := DefaultOptions()

o.SetLogCallers("s1", true)
Expand Down

0 comments on commit 0fba4e2

Please sign in to comment.