|
| 1 | +// Example that shows how to use struct, http.Request, http.Response wrappers |
| 2 | +// with the standard/structured logger. |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/dl1998/go-logging/pkg/common/level" |
| 7 | + standard "github.com/dl1998/go-logging/pkg/logger" |
| 8 | + standardFormatter "github.com/dl1998/go-logging/pkg/logger/formatter" |
| 9 | + standardHandler "github.com/dl1998/go-logging/pkg/logger/handler" |
| 10 | + structured "github.com/dl1998/go-logging/pkg/structuredlogger" |
| 11 | + structuredFormatter "github.com/dl1998/go-logging/pkg/structuredlogger/formatter" |
| 12 | + structuredHandler "github.com/dl1998/go-logging/pkg/structuredlogger/handler" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + exampleStructTemplate = "Name: {Name}, Age: {Age}" |
| 20 | + exampleStructMapping = map[string]string{ |
| 21 | + "name": "Name", |
| 22 | + "age": "Age", |
| 23 | + } |
| 24 | + |
| 25 | + exampleStruct = ExampleStruct{ |
| 26 | + Name: "John", |
| 27 | + Age: 25, |
| 28 | + } |
| 29 | + |
| 30 | + exampleRequest = &http.Request{ |
| 31 | + Method: http.MethodGet, |
| 32 | + URL: &url.URL{ |
| 33 | + Scheme: "http", |
| 34 | + Host: "localhost", |
| 35 | + Path: "/example", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + exampleResponse = &http.Response{ |
| 40 | + StatusCode: http.StatusOK, |
| 41 | + Status: "OK", |
| 42 | + Request: exampleRequest, |
| 43 | + } |
| 44 | +) |
| 45 | + |
| 46 | +type ExampleStruct struct { |
| 47 | + Name string |
| 48 | + Age int |
| 49 | +} |
| 50 | + |
| 51 | +func main() { |
| 52 | + exampleStandardLogger() |
| 53 | + exampleStructuredLogger() |
| 54 | +} |
| 55 | + |
| 56 | +// exampleStandardLogger is a sample function to show how to use struct, |
| 57 | +// http.Request, http.Response wrappers with the standard logger. |
| 58 | +func exampleStandardLogger() { |
| 59 | + applicationLogger := standard.New("main", time.DateTime) |
| 60 | + |
| 61 | + applicationFormatter := standardFormatter.New("%(datetime)\t[%(level)]\t%(message)") |
| 62 | + |
| 63 | + applicationHandler := standardHandler.NewConsoleErrorHandler(level.All, level.Null, applicationFormatter) |
| 64 | + |
| 65 | + applicationLogger.AddHandler(applicationHandler) |
| 66 | + |
| 67 | + applicationLogger.SetRequestTemplate("[{Method}] {URL}") |
| 68 | + applicationLogger.SetResponseTemplate("[{StatusCode}] {Status}") |
| 69 | + |
| 70 | + applicationLogger.WrapStruct(level.Info, exampleStructTemplate, exampleStruct) |
| 71 | + applicationLogger.WrapRequest(level.Info, exampleRequest) |
| 72 | + applicationLogger.WrapResponse(level.Info, exampleResponse) |
| 73 | +} |
| 74 | + |
| 75 | +// exampleStructuredLogger is a sample function to show how to use struct, |
| 76 | +// http.Request, http.Response wrappers with the structured logger. |
| 77 | +func exampleStructuredLogger() { |
| 78 | + applicationLogger := structured.New("main", time.DateTime) |
| 79 | + |
| 80 | + applicationFormatter := structuredFormatter.NewJSON( |
| 81 | + map[string]string{ |
| 82 | + "timestamp": "%(timestamp)", |
| 83 | + "level": "%(level)", |
| 84 | + "name": "%(name)", |
| 85 | + }, |
| 86 | + false, |
| 87 | + ) |
| 88 | + |
| 89 | + applicationHandler := structuredHandler.NewConsoleErrorHandler(level.All, level.Null, applicationFormatter) |
| 90 | + |
| 91 | + applicationLogger.AddHandler(applicationHandler) |
| 92 | + |
| 93 | + applicationLogger.SetRequestMapping(map[string]string{ |
| 94 | + "ExampleMethod": "Method", |
| 95 | + "ExampleURL": "URL", |
| 96 | + }) |
| 97 | + applicationLogger.SetResponseMapping(map[string]string{ |
| 98 | + "ExampleStatusCode": "StatusCode", |
| 99 | + "ExampleStatus": "Status", |
| 100 | + }) |
| 101 | + |
| 102 | + applicationLogger.WrapStruct(level.Info, exampleStructMapping, exampleStruct, "hostname", "localhost") |
| 103 | + applicationLogger.WrapRequest(level.Info, exampleRequest, "hostname", "localhost") |
| 104 | + applicationLogger.WrapResponse(level.Info, exampleResponse, "hostname", "localhost") |
| 105 | +} |
0 commit comments