Skip to content

Commit 388daee

Browse files
committed
Update class diagram to include new wrpappers: struct, http.Request, http.Response
1 parent beabf07 commit 388daee

File tree

4 files changed

+2423
-1750
lines changed

4 files changed

+2423
-1750
lines changed

README.md

+103-15
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,29 @@ structuredlogger.Configure(logger.NewConfiguration(logger.WithFormat("key-value"
8888

8989
All options available for the configuration are:
9090

91-
- For Standard Logger
92-
93-
| Method | Default | Description |
94-
|----------------|:-----------------------------:|------------------------------------------------------------------------------------|
95-
| WithErrorLevel | level.Error | Set logging level used to log raised or captured error. |
96-
| WithPanicLevel | level.Critical | Set logging level used to log panic. |
97-
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
98-
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
99-
| WithTemplate | "%(level):%(name):%(message)" | Set template for logging message. |
100-
| WithFile | "" | Set file where to log messages, if not set, then logging to file will be disabled. |
101-
| WithName | "root" | Set logger name. |
102-
| WithTimeFormat | time.RFC3339 | Set time format for logging message. |
103-
104-
- For Structured Logger
91+
For Standard Logger
92+
93+
| Method | Default | Description |
94+
|----------------------|:-----------------------------------:|------------------------------------------------------------------------------------|
95+
| WithErrorLevel | level.Error | Set logging level used to log raised or captured error. |
96+
| WithPanicLevel | level.Critical | Set logging level used to log panic. |
97+
| WithRequestTemplate | "Request: [{Method}] {URL}" | Set template for the http.Request wrapper. |
98+
| WithResponseTemplate | "Response: [{StatusCode}] {Status}" | Set template for the http.Response wrapper. |
99+
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
100+
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
101+
| WithTemplate | "%(level):%(name):%(message)" | Set template for logging message. |
102+
| WithFile | "" | Set file where to log messages, if not set, then logging to file will be disabled. |
103+
| WithName | "root" | Set logger name. |
104+
| WithTimeFormat | time.RFC3339 | Set time format for logging message. |
105+
106+
For Structured Logger
105107

106108
| Method | Default | Description |
107109
|-----------------------|:-------------------------------------------------------------------------------------------------------------------:|---------------------------------------------------------------------------------------------------------------------------------------|
108110
| WithErrorLevel | level.Error | Set logging level used to log raised or captured error. |
109111
| WithPanicLevel | level.Critical | Set logging level used to log panic. |
112+
| WithRequestMapping | map[string]string {<br/>"url": "URL",<br/>"method": "Method",<br/>} | Set mapping for the http.Request wrapper. |
113+
| WithResponseMapping | map[string]string {<br/>"status": "Status",<br/>"status-code": "StatusCode",<br/>} | Set mapping for the http.Response wrapper. |
110114
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
111115
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
112116
| WithTemplate | map[string]string {<br/>"timestamp": "%(timestamp)",<br/>"level": "%(level)",<br/>"name": "%(name)",<br/>} | Set template for logging structure. |
@@ -324,7 +328,9 @@ if err := applicationLogger.Open(100); err != nil {
324328
will add messages to the queue until it is not full, then it will wait (blocking the process) until the message from the
325329
queue will be processed and free up the space in the message queue.*
326330

327-
### Error / Panic Wrapper
331+
### Wrappers
332+
333+
#### Error / Panic
328334

329335
You could wrap error or raise a new error and log error message using the logger. By default, it will log error message
330336
using the `level.Error` level. However, it could be changed by setting the error level in the logger configuration.
@@ -382,6 +388,88 @@ Similarly, you could panic and log panic message using the logger. By default, i
382388
applicationLogger.Panic("exit code: 1", "hostname", "localhost")
383389
```
384390

391+
#### Struct
392+
393+
You could wrap a struct and log its public fields using the logger. To do this, you need to provide template (standard
394+
logger), mapping (structured logger) of the struct fields to the logger fields. Optionally, for structured logger you
395+
could also provide additional fields that will be logged with the struct fields.
396+
397+
- Standard logger
398+
399+
```go
400+
type MyStruct struct {
401+
String string
402+
Int int
403+
}
404+
405+
myStruct := MyStruct{
406+
String: "example",
407+
Int: 10,
408+
}
409+
410+
applicationLogger.WrapStruct(level.Info, "{String}: {Int}", myStruct)
411+
```
412+
413+
- Structured logger
414+
415+
```go
416+
type MyStruct struct {
417+
String string
418+
Int int
419+
}
420+
421+
myStruct := MyStruct{
422+
String: "example",
423+
Int: 10,
424+
}
425+
426+
applicationLogger.WrapStruct(level.Info, map[string]string{
427+
"log-string": "String",
428+
"log-int": "Int",
429+
}, myStruct, "hostname", "localhost")
430+
```
431+
432+
#### http.Request and http.Response
433+
434+
You could wrap http.Request and http.Response and log their fields using the logger. By default, logger has predefined
435+
template (standard logger), mapping (structured logger) for http.Request and http.Response fields. However, you could
436+
change it by providing your own template / mapping. Optionally, for structured logger you could also provide additional
437+
fields that will be logged with the http.Request and http.Response fields.
438+
439+
- Standard logger
440+
441+
```go
442+
request, _ := http.NewRequest("GET", "http://example.com", nil)
443+
response, _ := http.Get("http://example.com")
444+
445+
// Set custom template for http.Request and http.Response
446+
applicationLogger.SetRequestTemplate("[{Method}] {URL}")
447+
applicationLogger.SetResponseTemplate("[{StatusCode}] {Status}")
448+
449+
applicationLogger.WrapRequest(level.Info, request)
450+
applicationLogger.WrapResponse(level.Info, response)
451+
```
452+
453+
- Structured logger
454+
455+
```go
456+
request, _ := http.NewRequest("GET", "http://example.com", nil)
457+
response, _ := http.Get("http://example.com")
458+
459+
// Set custom mapping for http.Request and http.Response
460+
applicationLogger.SetRequestMapping(map[string]string{
461+
"Method": "Method",
462+
"Url": "URL",
463+
})
464+
applicationLogger.SetResponseMapping(map[string]string{
465+
"StatusCode": "StatusCode",
466+
"Status": "Status",
467+
})
468+
469+
applicationLogger.WrapRequest(level.Info, request, "hostname", "localhost")
470+
applicationLogger.WrapResponse(level.Info, response, "hostname", "localhost")
471+
```
472+
385473
### Reading Configuration from File
386474

387475
You could also read configuration from a file. Configuration file should be in one of the following formats: `*.json`,

0 commit comments

Comments
 (0)