-
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.
Merge pull request #898 from ploxiln/logging_refactor_2
*: internal logging package refactor
- Loading branch information
Showing
33 changed files
with
345 additions
and
470 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
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
This file was deleted.
Oops, something went wrong.
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
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,36 +1,35 @@ | ||
package http_api | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/nsqio/nsq/internal/app" | ||
"github.com/nsqio/nsq/internal/lg" | ||
) | ||
|
||
type logWriter struct { | ||
app.Logger | ||
logf lg.AppLogFunc | ||
} | ||
|
||
func (l logWriter) Write(p []byte) (int, error) { | ||
l.Logger.Output(2, string(p)) | ||
l.logf(lg.WARN, "%s", string(p)) | ||
return len(p), nil | ||
} | ||
|
||
func Serve(listener net.Listener, handler http.Handler, proto string, l app.Logger) { | ||
l.Output(2, fmt.Sprintf("%s: listening on %s", proto, listener.Addr())) | ||
func Serve(listener net.Listener, handler http.Handler, proto string, logf lg.AppLogFunc) { | ||
logf(lg.INFO, "%s: listening on %s", proto, listener.Addr()) | ||
|
||
server := &http.Server{ | ||
Handler: handler, | ||
ErrorLog: log.New(logWriter{l}, "", 0), | ||
ErrorLog: log.New(logWriter{logf}, "", 0), | ||
} | ||
err := server.Serve(listener) | ||
// theres no direct way to detect this error because it is not exposed | ||
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") { | ||
l.Output(2, fmt.Sprintf("ERROR: http.Serve() - %s", err)) | ||
logf(lg.ERROR, "http.Serve() - %s", err) | ||
} | ||
|
||
l.Output(2, fmt.Sprintf("%s: closing %s", proto, listener.Addr())) | ||
logf(lg.INFO, "%s: closing %s", proto, listener.Addr()) | ||
} |
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,75 @@ | ||
// 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 AppLogFunc func(lvl LogLevel, f string, args ...interface{}) | ||
|
||
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") | ||
} |
Oops, something went wrong.