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 ec12d33

Browse files
authored
Merge pull request #28 from dl1998/add-default-logger-configuration-descriptions
Add description for default logger configuration options
2 parents e09080e + 22b5719 commit ec12d33

File tree

5 files changed

+135
-37
lines changed

5 files changed

+135
-37
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ go get github.com/dl1998/go-logging
1313
or
1414

1515
```bash
16-
go install github.com/dl1998/go-logging@v1.0.0
16+
go install github.com/dl1998/go-logging@[version]
1717
```
1818

19+
***Note: replace `[version]` with the version you want to install.***
20+
1921
## Usage
2022

2123
Check examples provided in the [examples](./examples).
@@ -83,6 +85,34 @@ You could also change the format of the default structured logger by setting the
8385
structuredlogger.Configure(logger.NewConfiguration(logger.WithFormat("key-value")))
8486
```
8587

88+
All options available for the configuration are:
89+
90+
- For Standard Logger
91+
92+
| Method | Default | Description |
93+
|----------------|:-----------------------------:|------------------------------------------------------------------------------------|
94+
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
95+
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
96+
| WithTemplate | "%(level):%(name):%(message)" | Set template for logging message. |
97+
| WithFile | "" | Set file where to log messages, if not set, then logging to file will be disabled. |
98+
| WithName | "root" | Set logger name. |
99+
| WithTimeFormat | time.RFC3339 | Set time format for logging message. |
100+
101+
- For Structured Logger
102+
103+
| Method | Default | Description |
104+
|-----------------------|:-------------------------------------------------------------------------------------------------------------------:|---------------------------------------------------------------------------------------------------------------------------------------|
105+
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
106+
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
107+
| WithTemplate | map[string]string {<br/>"timestamp": "%(timestamp)",<br/>"level": "%(level)",<br/>"name": "%(name)",<br/>} | Set template for logging structure. |
108+
| WithFile | "" | Set file where to log messages, if not set, then logging to file will be disabled. |
109+
| WithFormat | "json" | Set format for structured logging.<br/><br/>Could be one of the following<br/><ul><li>json</li><li>key-value</li></ul> |
110+
| WithPretty | false | Set if json message should be pretty printed.<br/>*Option works only with "json" format.* |
111+
| WithKeyValueDelimiter | ":" | Set key-value delimiter (eg. "key=value", where '=' is the delimiter).<br/>*Option works only with "key-value" format.* |
112+
| WithPairSeparator | " " | Set key-value separator (eg. "key1=value1,key2=value2", where ',' is the separator).<br/>*Option works only with "key-value" format.* |
113+
| WithName | "root" | Set logger name. |
114+
| WithTimeFormat | time.RFC3339 | Set time format for logging message. |
115+
86116
### Custom Logger
87117

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

pkg/logger/logger.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/dl1998/go-logging/pkg/logger/formatter"
77
"github.com/dl1998/go-logging/pkg/logger/handler"
88
"github.com/dl1998/go-logging/pkg/logger/logrecord"
9+
"time"
910
)
1011

1112
var rootLogger *Logger
@@ -243,11 +244,12 @@ func WithTimeFormat(timeFormat string) Option {
243244
// NewConfiguration creates a new instance of the Configuration.
244245
func NewConfiguration(options ...Option) *Configuration {
245246
newConfiguration := &Configuration{
246-
fromLevel: level.Warning,
247-
toLevel: level.Null,
248-
template: "%(level):%(name):%(message)",
249-
file: "",
250-
name: "root",
247+
fromLevel: level.Warning,
248+
toLevel: level.Null,
249+
template: "%(level):%(name):%(message)",
250+
file: "",
251+
name: "root",
252+
timeFormat: time.RFC3339,
251253
}
252254

253255
for _, option := range options {

pkg/logger/logger_test.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -862,23 +862,52 @@ func BenchmarkWithName(b *testing.B) {
862862
}
863863
}
864864

865+
// TestWithTimeFormat tests that WithTimeFormat sets the time format in the
866+
// Configuration.
867+
func TestWithTimeFormat(t *testing.T) {
868+
configuration := NewConfiguration()
869+
870+
timeFormat := time.RFC3339
871+
872+
option := WithTimeFormat(timeFormat)
873+
874+
option(configuration)
875+
876+
testutils.AssertEquals(t, configuration.timeFormat, timeFormat)
877+
}
878+
879+
// BenchmarkWithTimeFormat perform benchmarking of the WithTimeFormat().
880+
func BenchmarkWithTimeFormat(b *testing.B) {
881+
configuration := NewConfiguration()
882+
883+
timeFormat := time.RFC3339
884+
885+
option := WithTimeFormat(timeFormat)
886+
887+
for index := 0; index < b.N; index++ {
888+
option(configuration)
889+
}
890+
}
891+
865892
// TestNewConfiguration tests that NewConfiguration creates a new Configuration.
866893
func TestNewConfiguration(t *testing.T) {
867894
tests := map[string]struct {
868-
options []Option
869-
expectedFromLevel level.Level
870-
expectedToLevel level.Level
871-
expectedTemplate string
872-
expectedFile string
873-
expectedName string
895+
options []Option
896+
expectedFromLevel level.Level
897+
expectedToLevel level.Level
898+
expectedTemplate string
899+
expectedFile string
900+
expectedName string
901+
expectedTimeFormat string
874902
}{
875903
"Empty": {
876-
options: []Option{},
877-
expectedFromLevel: level.Warning,
878-
expectedToLevel: level.Null,
879-
expectedTemplate: "%(level):%(name):%(message)",
880-
expectedFile: "",
881-
expectedName: "root",
904+
options: []Option{},
905+
expectedFromLevel: level.Warning,
906+
expectedToLevel: level.Null,
907+
expectedTemplate: "%(level):%(name):%(message)",
908+
expectedFile: "",
909+
expectedName: "root",
910+
expectedTimeFormat: time.RFC3339,
882911
},
883912
"Non Standard": {
884913
options: []Option{
@@ -887,12 +916,14 @@ func TestNewConfiguration(t *testing.T) {
887916
WithTemplate("%(message):%(name):%(level)"),
888917
WithFile("file.log"),
889918
WithName("test"),
919+
WithTimeFormat(time.DateTime),
890920
},
891-
expectedFromLevel: level.All,
892-
expectedToLevel: level.Emergency,
893-
expectedTemplate: "%(message):%(name):%(level)",
894-
expectedFile: "file.log",
895-
expectedName: "test",
921+
expectedFromLevel: level.All,
922+
expectedToLevel: level.Emergency,
923+
expectedTemplate: "%(message):%(name):%(level)",
924+
expectedFile: "file.log",
925+
expectedName: "test",
926+
expectedTimeFormat: time.DateTime,
896927
},
897928
}
898929
for name, configuration := range tests {
@@ -904,6 +935,7 @@ func TestNewConfiguration(t *testing.T) {
904935
testutils.AssertEquals(t, configuration.expectedTemplate, newConfiguration.template)
905936
testutils.AssertEquals(t, configuration.expectedFile, newConfiguration.file)
906937
testutils.AssertEquals(t, configuration.expectedName, newConfiguration.name)
938+
testutils.AssertEquals(t, configuration.expectedTimeFormat, newConfiguration.timeFormat)
907939
})
908940
}
909941
}

pkg/structuredlogger/structuredlogger.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/dl1998/go-logging/pkg/structuredlogger/formatter"
77
"github.com/dl1998/go-logging/pkg/structuredlogger/handler"
88
"github.com/dl1998/go-logging/pkg/structuredlogger/logrecord"
9+
"time"
910
)
1011

1112
var rootLogger *Logger
@@ -303,6 +304,7 @@ func NewConfiguration(options ...Option) *Configuration {
303304
pairSeparator: " ",
304305
file: "",
305306
name: "root",
307+
timeFormat: time.RFC3339,
306308
}
307309

308310
for _, option := range options {

pkg/structuredlogger/structuredlogger_test.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,23 +1001,52 @@ func BenchmarkWithName(b *testing.B) {
10011001
}
10021002
}
10031003

1004+
// TestWithTimeFormat tests that WithTimeFormat sets the time format in the
1005+
// Configuration.
1006+
func TestWithTimeFormat(t *testing.T) {
1007+
configuration := NewConfiguration()
1008+
1009+
timeFormat := time.RFC3339
1010+
1011+
option := WithTimeFormat(timeFormat)
1012+
1013+
option(configuration)
1014+
1015+
testutils.AssertEquals(t, configuration.timeFormat, timeFormat)
1016+
}
1017+
1018+
// BenchmarkWithTimeFormat perform benchmarking of the WithTimeFormat().
1019+
func BenchmarkWithTimeFormat(b *testing.B) {
1020+
configuration := NewConfiguration()
1021+
1022+
timeFormat := time.RFC3339
1023+
1024+
option := WithTimeFormat(timeFormat)
1025+
1026+
for index := 0; index < b.N; index++ {
1027+
option(configuration)
1028+
}
1029+
}
1030+
10041031
// TestNewConfiguration tests that NewConfiguration creates a new Configuration.
10051032
func TestNewConfiguration(t *testing.T) {
10061033
tests := map[string]struct {
1007-
options []Option
1008-
expectedFromLevel level.Level
1009-
expectedToLevel level.Level
1010-
expectedTemplate map[string]string
1011-
expectedFile string
1012-
expectedName string
1034+
options []Option
1035+
expectedFromLevel level.Level
1036+
expectedToLevel level.Level
1037+
expectedTemplate map[string]string
1038+
expectedFile string
1039+
expectedName string
1040+
expectedTimeFormat string
10131041
}{
10141042
"Empty": {
1015-
options: []Option{},
1016-
expectedFromLevel: level.Warning,
1017-
expectedToLevel: level.Null,
1018-
expectedTemplate: template,
1019-
expectedFile: "",
1020-
expectedName: "root",
1043+
options: []Option{},
1044+
expectedFromLevel: level.Warning,
1045+
expectedToLevel: level.Null,
1046+
expectedTemplate: template,
1047+
expectedFile: "",
1048+
expectedName: "root",
1049+
expectedTimeFormat: time.RFC3339,
10211050
},
10221051
"Non Standard": {
10231052
options: []Option{
@@ -1029,15 +1058,17 @@ func TestNewConfiguration(t *testing.T) {
10291058
}),
10301059
WithFile("file.log"),
10311060
WithName("test"),
1061+
WithTimeFormat(time.DateTime),
10321062
},
10331063
expectedFromLevel: level.All,
10341064
expectedToLevel: level.Emergency,
10351065
expectedTemplate: map[string]string{
10361066
"level-number": "%(levelnr)",
10371067
"name": "%(name)",
10381068
},
1039-
expectedFile: "file.log",
1040-
expectedName: "test",
1069+
expectedFile: "file.log",
1070+
expectedName: "test",
1071+
expectedTimeFormat: time.DateTime,
10411072
},
10421073
}
10431074
for name, configuration := range tests {
@@ -1049,6 +1080,7 @@ func TestNewConfiguration(t *testing.T) {
10491080
testutils.AssertEquals(t, configuration.expectedTemplate, newConfiguration.template)
10501081
testutils.AssertEquals(t, configuration.expectedFile, newConfiguration.file)
10511082
testutils.AssertEquals(t, configuration.expectedName, newConfiguration.name)
1083+
testutils.AssertEquals(t, configuration.expectedTimeFormat, newConfiguration.timeFormat)
10521084
})
10531085
}
10541086
}

0 commit comments

Comments
 (0)