github.com/minio/console@v1.4.1/api/admin_console.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/minio/madmin-go/v3"
    26  	"github.com/minio/websocket"
    27  )
    28  
    29  const logTimeFormat string = "15:04:05 MST 01/02/2006"
    30  
    31  // startConsoleLog starts log of the servers
    32  func startConsoleLog(ctx context.Context, conn WSConn, client MinioAdmin, logRequest LogRequest) error {
    33  	var node string
    34  	// name of node, default = "" (all)
    35  	if logRequest.node == "all" {
    36  		node = ""
    37  	} else {
    38  		node = logRequest.node
    39  	}
    40  
    41  	trimNode := strings.Split(node, ":")
    42  	// number of log lines
    43  	lineCount := 100
    44  	// type of logs "minio"|"application"|"all" default = "all"
    45  	var logKind string
    46  	if logRequest.logType == "minio" || logRequest.logType == "application" || logRequest.logType == "all" {
    47  		logKind = logRequest.logType
    48  	} else {
    49  		logKind = "all"
    50  	}
    51  
    52  	// Start listening on all Console Log activity.
    53  	logCh := client.getLogs(ctx, trimNode[0], lineCount, logKind)
    54  
    55  	for {
    56  		select {
    57  		case <-ctx.Done():
    58  			return nil
    59  		case logInfo, ok := <-logCh:
    60  
    61  			// zero value returned because the channel is closed and empty
    62  			if !ok {
    63  				return nil
    64  			}
    65  			if logInfo.Err != nil {
    66  				LogError("error on console logs: %v", logInfo.Err)
    67  				return logInfo.Err
    68  			}
    69  
    70  			// Serialize message to be sent
    71  			bytes, err := json.Marshal(serializeConsoleLogInfo(&logInfo))
    72  			if err != nil {
    73  				LogError("error on json.Marshal: %v", err)
    74  				return err
    75  			}
    76  
    77  			// Send Message through websocket connection
    78  			err = conn.writeMessage(websocket.TextMessage, bytes)
    79  			if err != nil {
    80  				LogError("error writeMessage: %v", err)
    81  				return err
    82  			}
    83  		}
    84  	}
    85  }
    86  
    87  func serializeConsoleLogInfo(l *madmin.LogInfo) (logInfo madmin.LogInfo) {
    88  	logInfo = *l
    89  	if logInfo.ConsoleMsg != "" {
    90  		logInfo.ConsoleMsg = strings.TrimPrefix(logInfo.ConsoleMsg, "\n")
    91  	}
    92  	if logInfo.Time != "" {
    93  		logInfo.Time = getLogTime(logInfo.Time)
    94  	}
    95  	return logInfo
    96  }
    97  
    98  func getLogTime(lt string) string {
    99  	tm, err := time.Parse(time.RFC3339Nano, lt)
   100  	if err != nil {
   101  		return lt
   102  	}
   103  	return tm.Format(logTimeFormat)
   104  }