Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
move logging related utils to a logging package
Browse files Browse the repository at this point in the history
All users of the previous utils based logging functionality have
been pointed at the new logging package.
The name "logging" was chosen to avoid directly conflicting with
the Go standard library package "log", which is used by the
existing logging related code in heketi.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
  • Loading branch information
phlogistonjohn authored and obnoxxx committed Oct 8, 2018
1 parent a9e9f18 commit 2600f7a
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 79 deletions.
16 changes: 8 additions & 8 deletions apps/glusterfs/app.go
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/heketi/heketi/executors/kubeexec"
"github.com/heketi/heketi/executors/mockexec"
"github.com/heketi/heketi/executors/sshexec"
"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/rest"
)

Expand All @@ -41,7 +41,7 @@ const (
)

var (
logger = utils.NewLogger("[heketi]", utils.LEVEL_INFO)
logger = logging.NewLogger("[heketi]", logging.LEVEL_INFO)
dbfilename = "heketi.db"
// global var to track active node health cache
// if multiple apps are started the content of this var is
Expand Down Expand Up @@ -231,17 +231,17 @@ func NewApp(conf *GlusterFSConfig) *App {
func SetLogLevel(level string) error {
switch level {
case "none":
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
case "critical":
logger.SetLevel(utils.LEVEL_CRITICAL)
logger.SetLevel(logging.LEVEL_CRITICAL)
case "error":
logger.SetLevel(utils.LEVEL_ERROR)
logger.SetLevel(logging.LEVEL_ERROR)
case "warning":
logger.SetLevel(utils.LEVEL_WARNING)
logger.SetLevel(logging.LEVEL_WARNING)
case "info":
logger.SetLevel(utils.LEVEL_INFO)
logger.SetLevel(logging.LEVEL_INFO)
case "debug":
logger.SetLevel(utils.LEVEL_DEBUG)
logger.SetLevel(logging.LEVEL_DEBUG)
case "":
// treat empty string as a no-op & don't complain
// about it
Expand Down
13 changes: 7 additions & 6 deletions apps/glusterfs/app_logging.go
Expand Up @@ -15,22 +15,23 @@ import (
"net/http"

"github.com/heketi/heketi/pkg/glusterfs/api"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/heketi/pkg/utils"
)

func (a *App) logLevelName() string {
switch logger.Level() {
case utils.LEVEL_NOLOG:
case logging.LEVEL_NOLOG:
return "none"
case utils.LEVEL_CRITICAL:
case logging.LEVEL_CRITICAL:
return "critical"
case utils.LEVEL_ERROR:
case logging.LEVEL_ERROR:
return "error"
case utils.LEVEL_WARNING:
case logging.LEVEL_WARNING:
return "warning"
case utils.LEVEL_INFO:
case logging.LEVEL_INFO:
return "info"
case utils.LEVEL_DEBUG:
case logging.LEVEL_DEBUG:
return "debug"
default:
return "(unknown)"
Expand Down
33 changes: 17 additions & 16 deletions apps/glusterfs/app_logging_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/gorilla/mux"

"github.com/heketi/heketi/pkg/glusterfs/api"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/tests"
)
Expand All @@ -32,7 +33,7 @@ func TestGetLogLevelNoLog(t *testing.T) {
orig := logger.Level()
defer logger.SetLevel(orig)
// our test starts with logging at NOLOG
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestGetLogLevelDebug(t *testing.T) {
orig := logger.Level()
defer logger.SetLevel(orig)
// our test starts with logging at NOLOG
logger.SetLevel(utils.LEVEL_DEBUG)
logger.SetLevel(logging.LEVEL_DEBUG)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestSetLogLevelDebug(t *testing.T) {
orig := logger.Level()
defer logger.SetLevel(orig)
// our test starts with logging at NOLOG
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
Expand All @@ -106,8 +107,8 @@ func TestSetLogLevelDebug(t *testing.T) {
defer ts.Close()

// having done nothing yet our log level should still be NOLOG
tests.Assert(t, logger.Level() == utils.LEVEL_NOLOG,
`expected logger.Level() == utils.LEVEL_NOLOG, got:`, logger.Level())
tests.Assert(t, logger.Level() == logging.LEVEL_NOLOG,
`expected logger.Level() == logging.LEVEL_NOLOG, got:`, logger.Level())

request := []byte(`{"loglevel":{"glusterfs": "debug"} }`)
r, err := http.Post(ts.URL+"/internal/logging",
Expand All @@ -124,8 +125,8 @@ func TestSetLogLevelDebug(t *testing.T) {
`expected data.LogLevel == "debug", got:`, data.LogLevel)

// check the actual log level now
tests.Assert(t, logger.Level() == utils.LEVEL_DEBUG,
`expected logger.Level() == utils.LEVEL_DEBUG, got:`, logger.Level())
tests.Assert(t, logger.Level() == logging.LEVEL_DEBUG,
`expected logger.Level() == logging.LEVEL_DEBUG, got:`, logger.Level())
}

func TestSetLogLevelRoundtrips(t *testing.T) {
Expand All @@ -136,7 +137,7 @@ func TestSetLogLevelRoundtrips(t *testing.T) {
orig := logger.Level()
defer logger.SetLevel(orig)
// our test starts with logging at NOLOG
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
Expand All @@ -148,8 +149,8 @@ func TestSetLogLevelRoundtrips(t *testing.T) {
defer ts.Close()

// having done nothing yet our log level should still be NOLOG
tests.Assert(t, logger.Level() == utils.LEVEL_NOLOG,
`expected logger.Level() == utils.LEVEL_NOLOG, got:`, logger.Level())
tests.Assert(t, logger.Level() == logging.LEVEL_NOLOG,
`expected logger.Level() == logging.LEVEL_NOLOG, got:`, logger.Level())
data := api.LogLevelInfo{LogLevel: map[string]string{}}

names := []string{"none", "critical", "error", "warning", "info"}
Expand Down Expand Up @@ -179,7 +180,7 @@ func TestSetLogLevelBadJson(t *testing.T) {
orig := logger.Level()
defer logger.SetLevel(orig)
// our test starts with logging at NOLOG
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
Expand All @@ -191,8 +192,8 @@ func TestSetLogLevelBadJson(t *testing.T) {
defer ts.Close()

// having done nothing yet our log level should still be NOLOG
tests.Assert(t, logger.Level() == utils.LEVEL_NOLOG,
`expected logger.Level() == utils.LEVEL_NOLOG, got:`, logger.Level())
tests.Assert(t, logger.Level() == logging.LEVEL_NOLOG,
`expected logger.Level() == logging.LEVEL_NOLOG, got:`, logger.Level())

request := []byte(`{"loglevel": debug}`)
r, err := http.Post(ts.URL+"/internal/logging",
Expand All @@ -216,7 +217,7 @@ func TestSetLogLevelBadLogLevel(t *testing.T) {
orig := logger.Level()
defer logger.SetLevel(orig)
// our test starts with logging at NOLOG
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
Expand All @@ -228,8 +229,8 @@ func TestSetLogLevelBadLogLevel(t *testing.T) {
defer ts.Close()

// having done nothing yet our log level should still be NOLOG
tests.Assert(t, logger.Level() == utils.LEVEL_NOLOG,
`expected logger.Level() == utils.LEVEL_NOLOG, got:`, logger.Level())
tests.Assert(t, logger.Level() == logging.LEVEL_NOLOG,
`expected logger.Level() == logging.LEVEL_NOLOG, got:`, logger.Level())

request := []byte(`{"loglevel":{"glusterfs": "verdant"}}`)
r, err := http.Post(ts.URL+"/internal/logging",
Expand Down
20 changes: 10 additions & 10 deletions apps/glusterfs/app_test.go
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/boltdb/bolt"
"github.com/gorilla/mux"
client "github.com/heketi/heketi/client/api/go-client"
"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/tests"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ func TestAppLogLevel(t *testing.T) {
"debug",
}

logger.SetLevel(utils.LEVEL_DEBUG)
logger.SetLevel(logging.LEVEL_DEBUG)
for _, level := range levels {
conf := &GlusterFSConfig{
Executor: "mock",
Expand All @@ -82,23 +82,23 @@ func TestAppLogLevel(t *testing.T) {

switch level {
case "none":
tests.Assert(t, logger.Level() == utils.LEVEL_NOLOG)
tests.Assert(t, logger.Level() == logging.LEVEL_NOLOG)
case "critical":
tests.Assert(t, logger.Level() == utils.LEVEL_CRITICAL)
tests.Assert(t, logger.Level() == logging.LEVEL_CRITICAL)
case "error":
tests.Assert(t, logger.Level() == utils.LEVEL_ERROR)
tests.Assert(t, logger.Level() == logging.LEVEL_ERROR)
case "warning":
tests.Assert(t, logger.Level() == utils.LEVEL_WARNING)
tests.Assert(t, logger.Level() == logging.LEVEL_WARNING)
case "info":
tests.Assert(t, logger.Level() == utils.LEVEL_INFO)
tests.Assert(t, logger.Level() == logging.LEVEL_INFO)
case "debug":
tests.Assert(t, logger.Level() == utils.LEVEL_DEBUG)
tests.Assert(t, logger.Level() == logging.LEVEL_DEBUG)
}
app.Close()
}

// Test that an unknown value does not change the loglevel
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
conf := &GlusterFSConfig{
Executor: "mock",
Allocator: "simple",
Expand All @@ -109,7 +109,7 @@ func TestAppLogLevel(t *testing.T) {
app := NewApp(conf)
defer app.Close()
tests.Assert(t, app != nil)
tests.Assert(t, logger.Level() == utils.LEVEL_NOLOG)
tests.Assert(t, logger.Level() == logging.LEVEL_NOLOG)
}

func TestAppReadOnlyDb(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions executors/cmdexec/cmdexec.go
Expand Up @@ -12,11 +12,11 @@ package cmdexec
import (
"sync"

"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/heketi/pkg/logging"
)

var (
logger = utils.NewLogger("[cmdexec]", utils.LEVEL_DEBUG)
logger = logging.NewLogger("[cmdexec]", logging.LEVEL_DEBUG)
)

type RemoteCommandTransport interface {
Expand Down Expand Up @@ -61,20 +61,20 @@ func (s *CmdExecutor) FreeConnection(host string) {
func (s *CmdExecutor) SetLogLevel(level string) {
switch level {
case "none":
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
case "critical":
logger.SetLevel(utils.LEVEL_CRITICAL)
logger.SetLevel(logging.LEVEL_CRITICAL)
case "error":
logger.SetLevel(utils.LEVEL_ERROR)
logger.SetLevel(logging.LEVEL_ERROR)
case "warning":
logger.SetLevel(utils.LEVEL_WARNING)
logger.SetLevel(logging.LEVEL_WARNING)
case "info":
logger.SetLevel(utils.LEVEL_INFO)
logger.SetLevel(logging.LEVEL_INFO)
case "debug":
logger.SetLevel(utils.LEVEL_DEBUG)
logger.SetLevel(logging.LEVEL_DEBUG)
}
}

func (s *CmdExecutor) Logger() *utils.Logger {
func (s *CmdExecutor) Logger() *logging.Logger {
return logger
}
4 changes: 2 additions & 2 deletions executors/kubeexec/kubeexec.go
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/heketi/heketi/executors/cmdexec"
"github.com/heketi/heketi/pkg/kubernetes"
"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/heketi/pkg/logging"
"github.com/lpabon/godbc"
)

Expand All @@ -46,7 +46,7 @@ type KubeExecutor struct {
}

var (
logger = utils.NewLogger("[kubeexec]", utils.LEVEL_DEBUG)
logger = logging.NewLogger("[kubeexec]", logging.LEVEL_DEBUG)
inClusterConfig = func() (*restclient.Config, error) {
return restclient.InClusterConfig()
}
Expand Down
4 changes: 2 additions & 2 deletions executors/kubeexec/kubeexec_test.go
Expand Up @@ -16,15 +16,15 @@ import (
restclient "k8s.io/client-go/rest"

"github.com/heketi/heketi/executors/cmdexec"
"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/tests"
)

func init() {
inClusterConfig = func() (*restclient.Config, error) {
return &restclient.Config{}, nil
}
logger.SetLevel(utils.LEVEL_NOLOG)
logger.SetLevel(logging.LEVEL_NOLOG)
}

func TestNewKubeExecutor(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions executors/sshexec/sshexec.go
Expand Up @@ -16,8 +16,8 @@ import (
"strconv"

"github.com/heketi/heketi/executors/cmdexec"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/heketi/pkg/remoteexec/ssh"
"github.com/heketi/heketi/pkg/utils"
"github.com/lpabon/godbc"
)

Expand All @@ -38,7 +38,7 @@ type SshExecutor struct {

var (
ErrSshPrivateKey = errors.New("Unable to read private key file")
sshNew = func(logger *utils.Logger, user string, file string) (Ssher, error) {
sshNew = func(logger *logging.Logger, user string, file string) (Ssher, error) {
s := ssh.NewSshExecWithKeyFile(logger, user, file)
if s == nil {
return nil, ErrSshPrivateKey
Expand Down
10 changes: 5 additions & 5 deletions executors/sshexec/sshexec_test.go
Expand Up @@ -14,7 +14,7 @@ import (
"testing"

"github.com/heketi/heketi/executors/cmdexec"
"github.com/heketi/heketi/pkg/utils"
"github.com/heketi/heketi/pkg/logging"
"github.com/heketi/tests"
)

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestNewSshExec(t *testing.T) {

f := NewFakeSsh()
defer tests.Patch(&sshNew,
func(logger *utils.Logger, user string, file string) (Ssher, error) {
func(logger *logging.Logger, user string, file string) (Ssher, error) {
return f, nil
}).Restore()

Expand All @@ -78,7 +78,7 @@ func TestSshExecRebalanceOnExpansion(t *testing.T) {

f := NewFakeSsh()
defer tests.Patch(&sshNew,
func(logger *utils.Logger, user string, file string) (Ssher, error) {
func(logger *logging.Logger, user string, file string) (Ssher, error) {
return f, nil
}).Restore()

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestSshExecRebalanceOnExpansion(t *testing.T) {
func TestNewSshExecDefaults(t *testing.T) {
f := NewFakeSsh()
defer tests.Patch(&sshNew,
func(logger *utils.Logger, user string, file string) (Ssher, error) {
func(logger *logging.Logger, user string, file string) (Ssher, error) {
return f, nil
}).Restore()

Expand Down Expand Up @@ -157,7 +157,7 @@ func TestSshExecutorEnvVariables(t *testing.T) {

f := NewFakeSsh()
defer tests.Patch(&sshNew,
func(logger *utils.Logger, user string, file string) (Ssher, error) {
func(logger *logging.Logger, user string, file string) (Ssher, error) {
return f, nil
}).Restore()

Expand Down

0 comments on commit 2600f7a

Please sign in to comment.