Allow logging targets to be configured to receive minio (#8347)

specific errors, `application` errors or `all` by default.

console logging on server by default lists all logs -
enhance admin console API to accept `type` as query parameter to
subscribe to application/minio logs.
This commit is contained in:
poornas
2019-10-11 18:50:54 -07:00
committed by Harshavardhana
parent 8964ef821f
commit d7060c4c32
28 changed files with 116 additions and 72 deletions

View File

@@ -155,6 +155,6 @@ func AuditLog(w http.ResponseWriter, r *http.Request, api string, reqClaims map[
entry.API.StatusCode = statusCode
entry.API.TimeToFirstByte = timeToFirstByte.String()
entry.API.TimeToResponse = timeToResponse.String()
_ = t.Send(entry)
_ = t.Send(entry, string(All))
}
}

View File

@@ -273,36 +273,53 @@ func hashString(input string) string {
return hex.EncodeToString(checksum)
}
// Kind specifies the kind of error log
type Kind string
const (
// Minio errors
Minio Kind = "MINIO"
// Application errors
Application Kind = "APPLICATION"
// All errors
All Kind = "ALL"
)
// LogAlwaysIf prints a detailed error message during
// the execution of the server.
func LogAlwaysIf(ctx context.Context, err error) {
func LogAlwaysIf(ctx context.Context, err error, errKind ...interface{}) {
if err == nil {
return
}
logIf(ctx, err)
logIf(ctx, err, errKind...)
}
// LogIf prints a detailed error message during
// the execution of the server, if it is not an
// ignored error.
func LogIf(ctx context.Context, err error) {
func LogIf(ctx context.Context, err error, errKind ...interface{}) {
if err == nil {
return
}
if err.Error() != diskNotFoundError {
logIf(ctx, err)
logIf(ctx, err, errKind...)
}
}
// logIf prints a detailed error message during
// the execution of the server.
func logIf(ctx context.Context, err error) {
func logIf(ctx context.Context, err error, errKind ...interface{}) {
if Disable {
return
}
logKind := string(Minio)
if len(errKind) > 0 {
if ek, ok := errKind[0].(Kind); ok {
logKind = string(ek)
}
}
req := GetReqInfo(ctx)
if req == nil {
@@ -330,6 +347,7 @@ func logIf(ctx context.Context, err error) {
entry := log.Entry{
DeploymentID: req.DeploymentID,
Level: ErrorLvl.String(),
LogKind: logKind,
RemoteHost: req.RemoteHost,
Host: req.Host,
RequestID: req.RequestID,
@@ -359,7 +377,7 @@ func logIf(ctx context.Context, err error) {
// Iterate over all logger targets to send the log entry
for _, t := range Targets {
t.Send(entry)
t.Send(entry, entry.LogKind)
}
}
@@ -368,9 +386,9 @@ var ErrCritical struct{}
// CriticalIf logs the provided error on the console. It fails the
// current go-routine by causing a `panic(ErrCritical)`.
func CriticalIf(ctx context.Context, err error) {
func CriticalIf(ctx context.Context, err error, errKind ...interface{}) {
if err != nil {
LogIf(ctx, err)
LogIf(ctx, err, errKind...)
panic(ErrCritical)
}
}

View File

@@ -30,7 +30,7 @@ type logOnceType struct {
}
// One log message per error.
func (l *logOnceType) logOnceIf(ctx context.Context, err error, id interface{}) {
func (l *logOnceType) logOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
if err == nil {
return
}
@@ -49,7 +49,7 @@ func (l *logOnceType) logOnceIf(ctx context.Context, err error, id interface{})
l.Unlock()
if shouldLog {
LogIf(ctx, err)
LogIf(ctx, err, errKind...)
}
}
@@ -76,6 +76,6 @@ var logOnce = newLogOnceType()
// LogOnceIf - Logs notification errors - once per error.
// id is a unique identifier for related log messages, refer to cmd/notification.go
// on how it is used.
func LogOnceIf(ctx context.Context, err error, id interface{}) {
logOnce.logOnceIf(ctx, err, id)
func LogOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
logOnce.logOnceIf(ctx, err, id, errKind...)
}

View File

@@ -40,6 +40,7 @@ type API struct {
type Entry struct {
DeploymentID string `json:"deploymentid,omitempty"`
Level string `json:"level"`
LogKind string `json:"errKind"`
Time string `json:"time"`
API *API `json:"api,omitempty"`
RemoteHost string `json:"remotehost,omitempty"`

View File

@@ -32,7 +32,7 @@ import (
type Target struct{}
// Send log message 'e' to console
func (c *Target) Send(e interface{}) error {
func (c *Target) Send(e interface{}, logKind string) error {
entry, ok := e.(log.Entry)
if !ok {
return fmt.Errorf("Uexpected log entry structure %#v", e)

View File

@@ -22,6 +22,7 @@ import (
"errors"
"net/http"
gohttp "net/http"
"strings"
xhttp "github.com/minio/minio/cmd/http"
)
@@ -39,6 +40,7 @@ type Target struct {
endpoint string
// User-Agent to be set on each log request sent to the `endpoint`
userAgent string
logKind string
client gohttp.Client
}
@@ -75,10 +77,11 @@ func (h *Target) startHTTPLogger() {
// New initializes a new logger target which
// sends log over http to the specified endpoint
func New(endpoint, userAgent string, transport *gohttp.Transport) *Target {
func New(endpoint, userAgent, logKind string, transport *gohttp.Transport) *Target {
h := Target{
endpoint: endpoint,
userAgent: userAgent,
logKind: strings.ToUpper(logKind),
client: gohttp.Client{
Transport: transport,
},
@@ -90,7 +93,10 @@ func New(endpoint, userAgent string, transport *gohttp.Transport) *Target {
}
// Send log message 'e' to http target.
func (h *Target) Send(entry interface{}) error {
func (h *Target) Send(entry interface{}, errKind string) error {
if h.logKind != errKind && h.logKind != "ALL" {
return nil
}
select {
case h.logCh <- entry:
default:

View File

@@ -20,7 +20,7 @@ package logger
// a single log entry and Send it to the log target
// e.g. Send the log to a http server
type Target interface {
Send(entry interface{}) error
Send(entry interface{}, errKind string) error
}
// Targets is the set of enabled loggers