github.com/minio/madmin-go@v1.7.5/api-log.go (about)

     1  //
     2  // MinIO Object Storage (c) 2021 MinIO, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package madmin
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/url"
    24  	"strconv"
    25  )
    26  
    27  // LogMask is a bit mask for log types.
    28  type LogMask uint64
    29  
    30  const (
    31  	LogMaskMinIO LogMask = 1 << iota
    32  	LogMaskApplication
    33  
    34  	// LogMaskAll must be the last.
    35  	LogMaskAll LogMask = (1 << iota) - 1
    36  )
    37  
    38  // Mask returns the LogMask as uint64
    39  func (m LogMask) Mask() uint64 {
    40  	return uint64(m)
    41  }
    42  
    43  // Contains returns whether all flags in other is present in t.
    44  func (m LogMask) Contains(other LogMask) bool {
    45  	return m&other == other
    46  }
    47  
    48  // LogKind specifies the kind of error log
    49  type LogKind string
    50  
    51  const (
    52  	// LogKindMinio Minio errors
    53  	LogKindMinio LogKind = "MINIO"
    54  	// LogKindApplication Application errors
    55  	LogKindApplication LogKind = "APPLICATION"
    56  	// LogKindAll All errors
    57  	LogKindAll LogKind = "ALL"
    58  )
    59  
    60  // LogMask returns the mask based on the kind.
    61  func (l LogKind) LogMask() LogMask {
    62  	switch l {
    63  	case LogKindMinio:
    64  		return LogMaskMinIO
    65  	case LogKindApplication:
    66  		return LogMaskApplication
    67  	case LogKindAll:
    68  		return LogMaskAll
    69  	}
    70  	return 0
    71  }
    72  
    73  func (l LogKind) String() string {
    74  	return string(l)
    75  }
    76  
    77  // LogInfo holds console log messages
    78  type LogInfo struct {
    79  	logEntry
    80  	ConsoleMsg string
    81  	NodeName   string `json:"node"`
    82  	Err        error  `json:"-"`
    83  }
    84  
    85  // GetLogs - listen on console log messages.
    86  func (adm AdminClient) GetLogs(ctx context.Context, node string, lineCnt int, logKind string) <-chan LogInfo {
    87  	logCh := make(chan LogInfo, 1)
    88  
    89  	// Only success, start a routine to start reading line by line.
    90  	go func(logCh chan<- LogInfo) {
    91  		defer close(logCh)
    92  		urlValues := make(url.Values)
    93  		urlValues.Set("node", node)
    94  		urlValues.Set("limit", strconv.Itoa(lineCnt))
    95  		urlValues.Set("logType", logKind)
    96  		for {
    97  			reqData := requestData{
    98  				relPath:     adminAPIPrefix + "/log",
    99  				queryValues: urlValues,
   100  			}
   101  			// Execute GET to call log handler
   102  			resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
   103  			if err != nil {
   104  				closeResponse(resp)
   105  				return
   106  			}
   107  
   108  			if resp.StatusCode != http.StatusOK {
   109  				logCh <- LogInfo{Err: httpRespToErrorResponse(resp)}
   110  				return
   111  			}
   112  			dec := json.NewDecoder(resp.Body)
   113  			for {
   114  				var info LogInfo
   115  				if err = dec.Decode(&info); err != nil {
   116  					break
   117  				}
   118  				select {
   119  				case <-ctx.Done():
   120  					return
   121  				case logCh <- info:
   122  				}
   123  			}
   124  
   125  		}
   126  	}(logCh)
   127  
   128  	// Returns the log info channel, for caller to start reading from.
   129  	return logCh
   130  }
   131  
   132  // Mask returns the mask based on the error level.
   133  func (l LogInfo) Mask() uint64 {
   134  	return l.LogKind.LogMask().Mask()
   135  }