WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 5a85cce

Browse files
authored
Merge pull request #17 from dl1998/structured-logger
Add structured logger
2 parents 6025b7c + 1ecd471 commit 5a85cce

File tree

29 files changed

+3616
-1457
lines changed

29 files changed

+3616
-1457
lines changed

README.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,100 @@ Logger supports 11 logging levels + 2 (when not set):
4141

4242
Default logger could be used like in the following example:
4343

44+
- Standard logger
45+
46+
```go
47+
logger.Warning("Message for logging: %s.", "my message")
48+
```
49+
50+
- Structured logger
51+
52+
```go
53+
structuredlogger.Warning("message", "My message.")
54+
```
55+
56+
or
57+
4458
```go
45-
logging.Warning("Message for logging: %s.", "my message")
59+
structuredlogger.Warning(map[string]string{
60+
"message": "My message.",
61+
})
4662
```
4763

4864
By default, root logger prints on console only, and starting from Warning level. It could be changed by setting logging
4965
level:
5066

67+
- Standard logger
68+
5169
```go
5270
logger.Configure(logger.NewConfiguration(logger.WithFromLevel(level.All)))
5371
```
5472

55-
After changing log level to "None" it will print messages for any level.
73+
- Structured logger
74+
75+
```go
76+
structuredlogger.Configure(logger.NewConfiguration(logger.WithFromLevel(level.All)))
77+
```
78+
79+
After changing log level to "All" it will print messages for any level.
80+
81+
You could also change the format of the default structured logger by setting the format (default: json).
82+
83+
```go
84+
structuredlogger.Configure(logger.NewConfiguration(logger.WithFormat("key-value")))
85+
```
5686

5787
### Custom Logger
5888

5989
Alternatively you could create application logger. To do this you would need to create a new logger.
6090

91+
- Standard logger
92+
6193
```go
6294
applicationLogger := logger.New("application-logger")
6395
```
6496

97+
- Structured logger
98+
99+
```go
100+
applicationStructuredLogger := structuredlogger.New("application-logger")
101+
```
102+
65103
After this you need to set up it, for this create a new formatter that says how to log the message by providing a
66104
template.
67105

68106
#### Formatter
69107

108+
- Standard logger
109+
70110
```go
71111
applicationFormatter := formatter.New("%(isotime) [%(level)] %(message)")
72112
```
73113

114+
- Structured logger
115+
- JSON format
116+
117+
```go
118+
applicationFormatter := formatter.NewJSON(map[string]string{
119+
"time": "%(timestamp)",
120+
"level": "%(level)",
121+
}, false)
122+
```
123+
124+
- Key-Value format
125+
126+
```go
127+
applicationFormatter := formatter.NewKeyValue(map[string]string{
128+
"time": "%(timestamp)",
129+
"level": "%(level)",
130+
}, "=", " ")
131+
```
132+
74133
After creation of the formatter, you need to create a new handler that tells where to write log messages.
75134

76135
#### Handler
77136

78-
There are three predefined types of handler:
137+
There are three predefined types of handler (for standard and structured logger each):
79138

80139
- Console Handler - it takes log level starting from which it would log messages, log level till which it would log
81140
messages, and formatter that tells how to log message. It logs messages to standard output.
@@ -118,10 +177,27 @@ applicationLogger.AddHandler(newFileHandler)
118177
Now it could be used to log the message, simply by calling respective level of logging and providing message with
119178
arguments.
120179

180+
- Standard logger
181+
121182
```go
122183
applicationLogger.Info("My message: %s.", "logged using application logger")
123184
```
124185

186+
- Structured logger
187+
- Varargs
188+
189+
```go
190+
applicationLogger.Info("message", "Logged using structured logger with varargs.")
191+
```
192+
193+
- Map
194+
195+
```go
196+
applicationLogger.Info(map[string]string{
197+
"message": "Logged using structured logger with map.",
198+
})
199+
```
200+
125201
## Class Diagram
126202

127203
![Class Diagram](./docs/architecture/diagrams/png/class_diagram.png)

0 commit comments

Comments
 (0)