storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/logger/target/console/console.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 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 console 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "strings" 23 "time" 24 25 "storj.io/minio/cmd/logger" 26 "storj.io/minio/cmd/logger/message/log" 27 "storj.io/minio/pkg/color" 28 "storj.io/minio/pkg/console" 29 ) 30 31 // Target implements loggerTarget to send log 32 // in plain or json format to the standard output. 33 type Target struct{} 34 35 // Validate - validate if the tty can be written to 36 func (c *Target) Validate() error { 37 return nil 38 } 39 40 // Endpoint returns the backend endpoint 41 func (c *Target) Endpoint() string { 42 return "" 43 } 44 45 func (c *Target) String() string { 46 return "console" 47 } 48 49 // Send log message 'e' to console 50 func (c *Target) Send(e interface{}, logKind string) error { 51 entry, ok := e.(log.Entry) 52 if !ok { 53 return fmt.Errorf("Uexpected log entry structure %#v", e) 54 } 55 if logger.IsJSON() { 56 logJSON, err := json.Marshal(&entry) 57 if err != nil { 58 return err 59 } 60 fmt.Println(string(logJSON)) 61 return nil 62 } 63 64 traceLength := len(entry.Trace.Source) 65 trace := make([]string, traceLength) 66 67 // Add a sequence number and formatting for each stack trace 68 // No formatting is required for the first entry 69 for i, element := range entry.Trace.Source { 70 trace[i] = fmt.Sprintf("%8v: %s", traceLength-i, element) 71 } 72 73 tagString := "" 74 for key, value := range entry.Trace.Variables { 75 if value != "" { 76 if tagString != "" { 77 tagString += ", " 78 } 79 tagString += fmt.Sprintf("%s=%v", key, value) 80 } 81 } 82 83 apiString := "API: " + entry.API.Name + "(" 84 if entry.API.Args != nil && entry.API.Args.Bucket != "" { 85 apiString = apiString + "bucket=" + entry.API.Args.Bucket 86 } 87 if entry.API.Args != nil && entry.API.Args.Object != "" { 88 apiString = apiString + ", object=" + entry.API.Args.Object 89 } 90 apiString += ")" 91 timeString := "Time: " + time.Now().Format(logger.TimeFormat) 92 93 var deploymentID string 94 if entry.DeploymentID != "" { 95 deploymentID = "\nDeploymentID: " + entry.DeploymentID 96 } 97 98 var requestID string 99 if entry.RequestID != "" { 100 requestID = "\nRequestID: " + entry.RequestID 101 } 102 103 var remoteHost string 104 if entry.RemoteHost != "" { 105 remoteHost = "\nRemoteHost: " + entry.RemoteHost 106 } 107 108 var host string 109 if entry.Host != "" { 110 host = "\nHost: " + entry.Host 111 } 112 113 var userAgent string 114 if entry.UserAgent != "" { 115 userAgent = "\nUserAgent: " + entry.UserAgent 116 } 117 118 if len(entry.Trace.Variables) > 0 { 119 tagString = "\n " + tagString 120 } 121 122 var msg = color.FgRed(color.Bold(entry.Trace.Message)) 123 var output = fmt.Sprintf("\n%s\n%s%s%s%s%s%s\nError: %s%s\n%s", 124 apiString, timeString, deploymentID, requestID, remoteHost, host, userAgent, 125 msg, tagString, strings.Join(trace, "\n")) 126 127 console.Println(output) 128 return nil 129 } 130 131 // New initializes a new logger target 132 // which prints log directly in the standard 133 // output. 134 func New() *Target { 135 return &Target{} 136 }