github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/debug/log/log.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/debug/log/log.go 14 15 // Package log provides debug logging 16 package log 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "time" 22 ) 23 24 var ( 25 // Default buffer size if any 26 DefaultSize = 256 27 // Default formatter 28 DefaultFormat = TextFormat 29 ) 30 31 // Log is debug log interface for reading and writing logs 32 type Log interface { 33 // Read reads log entries from the logger 34 Read(...ReadOption) ([]Record, error) 35 // Write writes records to log 36 Write(Record) error 37 // Stream log records 38 Stream() (Stream, error) 39 } 40 41 // Record is log record entry 42 type Record struct { 43 // Timestamp of logged event 44 Timestamp time.Time `json:"timestamp"` 45 // Metadata to enrich log record 46 Metadata map[string]string `json:"metadata"` 47 // Value contains log entry 48 Message interface{} `json:"message"` 49 } 50 51 // Stream returns a log stream 52 type Stream interface { 53 Chan() <-chan Record 54 Stop() error 55 } 56 57 // Format is a function which formats the output 58 type FormatFunc func(Record) string 59 60 // TextFormat returns text format 61 func TextFormat(r Record) string { 62 t := r.Timestamp.Format("2006-01-02 15:04:05") 63 return fmt.Sprintf("%s %v", t, r.Message) 64 } 65 66 // JSONFormat is a json Format func 67 func JSONFormat(r Record) string { 68 b, _ := json.Marshal(r) 69 return string(b) 70 }