Skip to content

Commit 9fce31b

Browse files
authored
Merge pull request #26 from dl1998/add-time-format-option
Add possibility to set time format of the logger
2 parents c71fb83 + ff920b6 commit 9fce31b

File tree

13 files changed

+2622
-53
lines changed

13 files changed

+2622
-53
lines changed

docs/architecture/diagrams/plantuml/class_diagram.plantuml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ package pkg {
183183
}
184184
struct baseLogger implements baseLoggerInterface {
185185
~ name : string
186+
~ timeFormat : string
186187
~ handlers : []handler.Interface
187188
+ Log(level : level.Level, message : string, parameters : ...any)
188189
+ Name() : string
@@ -232,6 +233,7 @@ package pkg {
232233
~ template : string
233234
~ file : string
234235
~ name : string
236+
~ timeFormat : string
235237
}
236238
stereotype Option <<func(*Configuration)>> {}
237239
class "<<module>>" {
@@ -240,12 +242,13 @@ package pkg {
240242
~ toLevel : level.Level
241243
~ template : string
242244
~ init()
243-
+ New(name : string) : *Logger
245+
+ New(name : string, timeFormat : string) : *Logger
244246
+ WithFromLevel(fromLevel : level.Level) : Option
245247
+ WithToLevel(toLevel : level.Level) : Option
246248
+ WithTemplate(template : string) : Option
247249
+ WithFile(file : string) : Option
248250
+ WithName(name : string) : Option
251+
+ WithTimeFormat(timeFormat : string) : Option
249252
+ NewConfiguration(options : ...Option) : *Configuration
250253
+ Configure(configuration : *Configuration)
251254
+ Name() : string
@@ -383,6 +386,7 @@ package pkg {
383386
}
384387
struct baseLogger implements baseLoggerInterface {
385388
~ name : string
389+
~ timeFormat : string
386390
~ handlers : []handler.Interface
387391
+ Log(level : level.Level, parameters : ...any)
388392
+ Name() : string
@@ -436,6 +440,7 @@ package pkg {
436440
~ pairSeparator : string
437441
~ file : string
438442
~ name : string
443+
~ timeFormat : string
439444
}
440445
stereotype Option <<func(*Configuration)>> {}
441446
class "<<module>>" {
@@ -444,7 +449,7 @@ package pkg {
444449
~ toLevel : level.Level
445450
~ template : map[string]string
446451
~ init()
447-
+ New(name : string) : *Logger
452+
+ New(name : string, timeFormat : string) : *Logger
448453
+ WithFromLevel(fromLevel : level.Level) : Option
449454
+ WithToLevel(toLevel : level.Level) : Option
450455
+ WithTemplate(template : map[string]string) : Option
@@ -454,6 +459,7 @@ package pkg {
454459
+ WithKeyValueDelimiter(keyValueDelimiter : string) : Option
455460
+ WithPairSeparator(pairSeparator : string) : Option
456461
+ WithName(name : string) : Option
462+
+ WithTimeFormat(timeFormat : string) : Option
457463
+ NewConfiguration(options : ...Option) : *Configuration
458464
+ Configure(configuration : *Configuration)
459465
+ Name() : string

docs/architecture/diagrams/plantuml/create_new_logger.plantuml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ participant logger
88
participant formatter
99
participant handler
1010

11-
main -> logger: New(name)
11+
main -> logger: New(name, timeFormat)
1212
activate logger
1313
deactivate logger
1414

322 KB
Loading
435 Bytes
Loading

docs/architecture/diagrams/svg/class_diagram.svg

Lines changed: 2537 additions & 1 deletion
Loading

docs/architecture/diagrams/svg/create_new_logger.svg

Lines changed: 23 additions & 23 deletions
Loading

examples/customlogger/main.go

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

1112
func main() {
12-
applicationLogger := logger.New("example")
13+
applicationLogger := logger.New("example", time.RFC3339)
1314

1415
applicationLogger.Debug("This message will not be displayed, because there are no handlers registered.")
1516

examples/customstructuredlogger/main.go

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

1112
func main() {
@@ -14,7 +15,7 @@ func main() {
1415
"level": "%(level)",
1516
}
1617

17-
applicationLogger := logger.New("example")
18+
applicationLogger := logger.New("example", time.RFC3339)
1819

1920
applicationLogger.Debug("message", "This message will not be displayed, because there are no handlers registered.")
2021

examples/logtofile/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/dl1998/go-logging/pkg/logger/formatter"
99
"github.com/dl1998/go-logging/pkg/logger/handler"
1010
"os"
11+
"time"
1112
)
1213

1314
func main() {
@@ -21,7 +22,7 @@ func main() {
2122
}
2223
}
2324

24-
applicationLogger := logger.New("file-logger")
25+
applicationLogger := logger.New("file-logger", time.RFC3339)
2526

2627
applicationFormatter := formatter.New("%(isotime) [%(level)] %(message)")
2728
fileHandler := handler.NewFileHandler(level.Warning, level.Null, applicationFormatter, fmt.Sprintf("%s/file.log", directory))

pkg/logger/logger.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ type baseLoggerInterface interface {
2929

3030
// baseLogger struct contains basic fields for the logger.
3131
type baseLogger struct {
32-
name string
33-
handlers []handler.Interface
32+
name string
33+
timeFormat string
34+
handlers []handler.Interface
3435
}
3536

3637
// Log logs interpolated message with the provided level.Level.
3738
func (logger *baseLogger) Log(level level.Level, message string, parameters ...any) {
38-
record := logrecord.New(logger.name, level, template, message, parameters, 3)
39+
record := logrecord.New(logger.name, level, logger.timeFormat, message, parameters, 3)
3940
for _, registeredHandler := range logger.handlers {
4041
registeredHandler.Write(record)
4142
}
@@ -98,11 +99,12 @@ type Logger struct {
9899
}
99100

100101
// New creates a new instance of the Logger.
101-
func New(name string) *Logger {
102+
func New(name string, timeFormat string) *Logger {
102103
return &Logger{
103104
baseLogger: &baseLogger{
104-
name: name,
105-
handlers: make([]handler.Interface, 0),
105+
name: name,
106+
timeFormat: timeFormat,
107+
handlers: make([]handler.Interface, 0),
106108
},
107109
}
108110
}
@@ -185,11 +187,12 @@ func (logger *Logger) Emergency(message string, parameters ...any) {
185187

186188
// Configuration struct contains configuration for the logger.
187189
type Configuration struct {
188-
fromLevel level.Level
189-
toLevel level.Level
190-
template string
191-
file string
192-
name string
190+
fromLevel level.Level
191+
toLevel level.Level
192+
template string
193+
file string
194+
name string
195+
timeFormat string
193196
}
194197

195198
// Option represents option for the Configuration.
@@ -230,6 +233,13 @@ func WithName(name string) Option {
230233
}
231234
}
232235

236+
// WithTimeFormat sets timeFormat for the Configuration.
237+
func WithTimeFormat(timeFormat string) Option {
238+
return func(configuration *Configuration) {
239+
configuration.timeFormat = timeFormat
240+
}
241+
}
242+
233243
// NewConfiguration creates a new instance of the Configuration.
234244
func NewConfiguration(options ...Option) *Configuration {
235245
newConfiguration := &Configuration{
@@ -257,7 +267,7 @@ func Configure(configuration *Configuration) {
257267
toLevel = configuration.toLevel
258268
template = configuration.template
259269

260-
newLogger := New(configuration.name)
270+
newLogger := New(configuration.name, configuration.timeFormat)
261271

262272
defaultFormatter := formatter.New(configuration.template)
263273

0 commit comments

Comments
 (0)