-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new internal/lg/ logging package for nsqd nsqlookupd nsqadmin
introduce LogLevel type NilLogger for disabling logging consolidate LogLevel tests into internal/lg/ consolidate TestNoLogger
- Loading branch information
Showing
16 changed files
with
246 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// short for "log" | ||
package lg | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type LogLevel int | ||
|
||
const ( | ||
DEBUG = LogLevel(1) | ||
INFO = LogLevel(2) | ||
WARN = LogLevel(3) | ||
ERROR = LogLevel(4) | ||
FATAL = LogLevel(5) | ||
) | ||
|
||
type Logger interface { | ||
Output(maxdepth int, s string) error | ||
} | ||
|
||
type NilLogger struct{} | ||
|
||
func (l NilLogger) Output(maxdepth int, s string) error { | ||
return nil | ||
} | ||
|
||
func (l LogLevel) String() string { | ||
switch l { | ||
case 1: | ||
return "DEBUG" | ||
case 2: | ||
return "INFO" | ||
case 3: | ||
return "WARNING" | ||
case 4: | ||
return "ERROR" | ||
case 5: | ||
return "FATAL" | ||
} | ||
panic("invalid LogLevel") | ||
} | ||
|
||
func ParseLogLevel(levelstr string, verbose bool) (LogLevel, error) { | ||
lvl := INFO | ||
|
||
switch strings.ToLower(levelstr) { | ||
case "debug": | ||
lvl = DEBUG | ||
case "info": | ||
lvl = INFO | ||
case "warn": | ||
lvl = WARN | ||
case "error": | ||
lvl = ERROR | ||
case "fatal": | ||
lvl = FATAL | ||
default: | ||
return lvl, fmt.Errorf("invalid log-level '%s'", levelstr) | ||
} | ||
if verbose { | ||
lvl = DEBUG | ||
} | ||
return lvl, nil | ||
} | ||
|
||
func Logf(logger Logger, cfgLevel LogLevel, msgLevel LogLevel, f string, args ...interface{}) { | ||
if cfgLevel > msgLevel { | ||
return | ||
} | ||
logger.Output(3, fmt.Sprintf(msgLevel.String()+": "+f, args...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package lg | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/nsqio/nsq/internal/test" | ||
) | ||
|
||
type options struct { | ||
LogLevel string `flag:"log-level"` | ||
Verbose bool `flag:"verbose"` // for backwards compatibility | ||
Logger Logger | ||
logLevel LogLevel // private, not really an option | ||
} | ||
|
||
func newOptions() *options { | ||
return &options{ | ||
LogLevel: "info", | ||
} | ||
} | ||
|
||
type app struct { | ||
opts *options | ||
} | ||
|
||
func (n *app) logf(level LogLevel, f string, args ...interface{}) { | ||
Logf(n.opts.Logger, n.opts.logLevel, level, f, args) | ||
} | ||
|
||
func newApp(opts *options) *app { | ||
if opts.Logger == nil { | ||
opts.Logger = log.New(os.Stderr, "[app] ", log.Ldate|log.Ltime|log.Lmicroseconds) | ||
} | ||
n := &app{ | ||
opts: opts, | ||
} | ||
|
||
var err error | ||
opts.logLevel, err = ParseLogLevel(opts.LogLevel, opts.Verbose) | ||
if err != nil { | ||
n.logf(FATAL, "%s", err) | ||
os.Exit(1) | ||
} | ||
|
||
n.logf(INFO, "app 0.1") | ||
return n | ||
} | ||
|
||
type mockLogger struct { | ||
Count int | ||
} | ||
|
||
func (l *mockLogger) Output(maxdepth int, s string) error { | ||
l.Count++ | ||
return nil | ||
} | ||
|
||
func TestLogging(t *testing.T) { | ||
logger := &mockLogger{} | ||
opts := newOptions() | ||
opts.Logger = logger | ||
|
||
// Test only fatal get through | ||
opts.LogLevel = "FaTaL" | ||
nsqd := newApp(opts) | ||
logger.Count = 0 | ||
for i := 1; i <= 5; i++ { | ||
nsqd.logf(LogLevel(i), "Test") | ||
} | ||
test.Equal(t, 1, logger.Count) | ||
|
||
// Test only warnings or higher get through | ||
opts.LogLevel = "WARN" | ||
nsqd = newApp(opts) | ||
logger.Count = 0 | ||
for i := 1; i <= 5; i++ { | ||
nsqd.logf(LogLevel(i), "Test") | ||
} | ||
test.Equal(t, 3, logger.Count) | ||
|
||
// Test everything gets through | ||
opts.LogLevel = "debuG" | ||
nsqd = newApp(opts) | ||
logger.Count = 0 | ||
for i := 1; i <= 5; i++ { | ||
nsqd.logf(LogLevel(i), "Test") | ||
} | ||
test.Equal(t, 5, logger.Count) | ||
|
||
// Test everything gets through with verbose = true | ||
opts.LogLevel = "fatal" | ||
opts.Verbose = true | ||
nsqd = newApp(opts) | ||
logger.Count = 0 | ||
for i := 1; i <= 5; i++ { | ||
nsqd.logf(LogLevel(i), "Test") | ||
} | ||
test.Equal(t, 5, logger.Count) | ||
} | ||
|
||
func TestNoLogger(t *testing.T) { | ||
opts := newOptions() | ||
opts.Logger = NilLogger{} | ||
app := newApp(opts) | ||
|
||
app.logf(ERROR, "should never be logged") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,20 @@ | ||
package nsqadmin | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"github.com/nsqio/nsq/internal/lg" | ||
) | ||
|
||
type Logger interface { | ||
Output(maxdepth int, s string) error | ||
} | ||
type Logger lg.Logger | ||
|
||
const ( | ||
LOG_DEBUG = 1 | ||
LOG_INFO = 2 | ||
LOG_WARN = 3 | ||
LOG_ERROR = 4 | ||
LOG_FATAL = 5 | ||
LOG_DEBUG = lg.DEBUG | ||
LOG_INFO = lg.INFO | ||
LOG_WARN = lg.WARN | ||
LOG_ERROR = lg.ERROR | ||
LOG_FATAL = lg.FATAL | ||
) | ||
|
||
func (n *NSQAdmin) logLevelFromString(level string) int { | ||
// check log-level is valid and translate to int | ||
switch strings.ToLower(level) { | ||
case "debug": | ||
return LOG_DEBUG | ||
case "info": | ||
return LOG_INFO | ||
case "warn": | ||
return LOG_WARN | ||
case "error": | ||
return LOG_ERROR | ||
case "fatal": | ||
return LOG_FATAL | ||
default: | ||
return -1 | ||
} | ||
} | ||
|
||
func (n *NSQAdmin) logf(level int, f string, args ...interface{}) { | ||
levelString := "INFO" | ||
switch level { | ||
case LOG_DEBUG: | ||
levelString = "DEBUG" | ||
case LOG_INFO: | ||
levelString = "INFO" | ||
case LOG_WARN: | ||
levelString = "WARNING" | ||
case LOG_ERROR: | ||
levelString = "ERROR" | ||
case LOG_FATAL: | ||
levelString = "FATAL" | ||
} | ||
|
||
func (n *NSQAdmin) logf(level lg.LogLevel, f string, args ...interface{}) { | ||
opts := n.getOpts() | ||
if level >= opts.logLevel || opts.Verbose { | ||
n.getOpts().Logger.Output(2, fmt.Sprintf(levelString+": "+f, args...)) | ||
} | ||
lg.Logf(opts.Logger, opts.logLevel, level, f, args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.