storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/api-log.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 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  // LogInfo holds console log messages
    28  type LogInfo struct {
    29  	logEntry
    30  	ConsoleMsg string
    31  	NodeName   string `json:"node"`
    32  	Err        error  `json:"-"`
    33  }
    34  
    35  // GetLogs - listen on console log messages.
    36  func (adm AdminClient) GetLogs(ctx context.Context, node string, lineCnt int, logKind string) <-chan LogInfo {
    37  	logCh := make(chan LogInfo, 1)
    38  
    39  	// Only success, start a routine to start reading line by line.
    40  	go func(logCh chan<- LogInfo) {
    41  		defer close(logCh)
    42  		urlValues := make(url.Values)
    43  		urlValues.Set("node", node)
    44  		urlValues.Set("limit", strconv.Itoa(lineCnt))
    45  		urlValues.Set("logType", logKind)
    46  		for {
    47  			reqData := requestData{
    48  				relPath:     adminAPIPrefix + "/log",
    49  				queryValues: urlValues,
    50  			}
    51  			// Execute GET to call log handler
    52  			resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
    53  			if err != nil {
    54  				closeResponse(resp)
    55  				return
    56  			}
    57  
    58  			if resp.StatusCode != http.StatusOK {
    59  				logCh <- LogInfo{Err: httpRespToErrorResponse(resp)}
    60  				return
    61  			}
    62  			dec := json.NewDecoder(resp.Body)
    63  			for {
    64  				var info LogInfo
    65  				if err = dec.Decode(&info); err != nil {
    66  					break
    67  				}
    68  				select {
    69  				case <-ctx.Done():
    70  					return
    71  				case logCh <- info:
    72  				}
    73  			}
    74  
    75  		}
    76  	}(logCh)
    77  
    78  	// Returns the log info channel, for caller to start reading from.
    79  	return logCh
    80  }