github.com/gogf/gf/v2@v2.7.4/os/glog/glog_logger_handler_json.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package glog 8 9 import ( 10 "context" 11 12 "github.com/gogf/gf/v2/internal/json" 13 ) 14 15 // HandlerOutputJson is the structure outputting logging content as single json. 16 type HandlerOutputJson struct { 17 Time string `json:""` // Formatted time string, like "2016-01-09 12:00:00". 18 TraceId string `json:",omitempty"` // Trace id, only available if tracing is enabled. 19 CtxStr string `json:",omitempty"` // The retrieved context value string from context, only available if Config.CtxKeys configured. 20 Level string `json:""` // Formatted level string, like "DEBU", "ERRO", etc. Eg: ERRO 21 CallerPath string `json:",omitempty"` // The source file path and its line number that calls logging, only available if F_FILE_SHORT or F_FILE_LONG set. 22 CallerFunc string `json:",omitempty"` // The source function name that calls logging, only available if F_CALLER_FN set. 23 Prefix string `json:",omitempty"` // Custom prefix string for logging content. 24 Content string `json:""` // Content is the main logging content, containing error stack string produced by logger. 25 Stack string `json:",omitempty"` // Stack string produced by logger, only available if Config.StStatus configured. 26 } 27 28 // HandlerJson is a handler for output logging content as a single json string. 29 func HandlerJson(ctx context.Context, in *HandlerInput) { 30 output := HandlerOutputJson{ 31 Time: in.TimeFormat, 32 TraceId: in.TraceId, 33 CtxStr: in.CtxStr, 34 Level: in.LevelFormat, 35 CallerFunc: in.CallerFunc, 36 CallerPath: in.CallerPath, 37 Prefix: in.Prefix, 38 Content: in.Content, 39 Stack: in.Stack, 40 } 41 if len(in.Values) > 0 { 42 if output.Content != "" { 43 output.Content += " " 44 } 45 output.Content += in.ValuesContent() 46 } 47 // Output json content. 48 jsonBytes, err := json.Marshal(output) 49 if err != nil { 50 panic(err) 51 } 52 in.Buffer.Write(jsonBytes) 53 in.Buffer.Write([]byte("\n")) 54 in.Next(ctx) 55 }