Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to set time format of the logger #26

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/architecture/diagrams/plantuml/class_diagram.plantuml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ package pkg {
}
struct baseLogger implements baseLoggerInterface {
~ name : string
~ timeFormat : string
~ handlers : []handler.Interface
+ Log(level : level.Level, message : string, parameters : ...any)
+ Name() : string
Expand Down Expand Up @@ -232,6 +233,7 @@ package pkg {
~ template : string
~ file : string
~ name : string
~ timeFormat : string
}
stereotype Option <<func(*Configuration)>> {}
class "<<module>>" {
Expand All @@ -240,12 +242,13 @@ package pkg {
~ toLevel : level.Level
~ template : string
~ init()
+ New(name : string) : *Logger
+ New(name : string, timeFormat : string) : *Logger
+ WithFromLevel(fromLevel : level.Level) : Option
+ WithToLevel(toLevel : level.Level) : Option
+ WithTemplate(template : string) : Option
+ WithFile(file : string) : Option
+ WithName(name : string) : Option
+ WithTimeFormat(timeFormat : string) : Option
+ NewConfiguration(options : ...Option) : *Configuration
+ Configure(configuration : *Configuration)
+ Name() : string
Expand Down Expand Up @@ -383,6 +386,7 @@ package pkg {
}
struct baseLogger implements baseLoggerInterface {
~ name : string
~ timeFormat : string
~ handlers : []handler.Interface
+ Log(level : level.Level, parameters : ...any)
+ Name() : string
Expand Down Expand Up @@ -436,6 +440,7 @@ package pkg {
~ pairSeparator : string
~ file : string
~ name : string
~ timeFormat : string
}
stereotype Option <<func(*Configuration)>> {}
class "<<module>>" {
Expand All @@ -444,7 +449,7 @@ package pkg {
~ toLevel : level.Level
~ template : map[string]string
~ init()
+ New(name : string) : *Logger
+ New(name : string, timeFormat : string) : *Logger
+ WithFromLevel(fromLevel : level.Level) : Option
+ WithToLevel(toLevel : level.Level) : Option
+ WithTemplate(template : map[string]string) : Option
Expand All @@ -454,6 +459,7 @@ package pkg {
+ WithKeyValueDelimiter(keyValueDelimiter : string) : Option
+ WithPairSeparator(pairSeparator : string) : Option
+ WithName(name : string) : Option
+ WithTimeFormat(timeFormat : string) : Option
+ NewConfiguration(options : ...Option) : *Configuration
+ Configure(configuration : *Configuration)
+ Name() : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ participant logger
participant formatter
participant handler

main -> logger: New(name)
main -> logger: New(name, timeFormat)
activate logger
deactivate logger

Expand Down
Binary file modified docs/architecture/diagrams/png/class_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/architecture/diagrams/png/create_new_logger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,538 changes: 2,537 additions & 1 deletion docs/architecture/diagrams/svg/class_diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 23 additions & 23 deletions docs/architecture/diagrams/svg/create_new_logger.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion examples/customlogger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"github.com/dl1998/go-logging/pkg/logger"
"github.com/dl1998/go-logging/pkg/logger/formatter"
"github.com/dl1998/go-logging/pkg/logger/handler"
"time"
)

func main() {
applicationLogger := logger.New("example")
applicationLogger := logger.New("example", time.RFC3339)

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

Expand Down
3 changes: 2 additions & 1 deletion examples/customstructuredlogger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
logger "github.com/dl1998/go-logging/pkg/structuredlogger"
"github.com/dl1998/go-logging/pkg/structuredlogger/formatter"
"github.com/dl1998/go-logging/pkg/structuredlogger/handler"
"time"
)

func main() {
Expand All @@ -14,7 +15,7 @@ func main() {
"level": "%(level)",
}

applicationLogger := logger.New("example")
applicationLogger := logger.New("example", time.RFC3339)

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

Expand Down
3 changes: 2 additions & 1 deletion examples/logtofile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/dl1998/go-logging/pkg/logger/formatter"
"github.com/dl1998/go-logging/pkg/logger/handler"
"os"
"time"
)

func main() {
Expand All @@ -21,7 +22,7 @@ func main() {
}
}

applicationLogger := logger.New("file-logger")
applicationLogger := logger.New("file-logger", time.RFC3339)

applicationFormatter := formatter.New("%(isotime) [%(level)] %(message)")
fileHandler := handler.NewFileHandler(level.Warning, level.Null, applicationFormatter, fmt.Sprintf("%s/file.log", directory))
Expand Down
34 changes: 22 additions & 12 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ type baseLoggerInterface interface {

// baseLogger struct contains basic fields for the logger.
type baseLogger struct {
name string
handlers []handler.Interface
name string
timeFormat string
handlers []handler.Interface
}

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

// New creates a new instance of the Logger.
func New(name string) *Logger {
func New(name string, timeFormat string) *Logger {
return &Logger{
baseLogger: &baseLogger{
name: name,
handlers: make([]handler.Interface, 0),
name: name,
timeFormat: timeFormat,
handlers: make([]handler.Interface, 0),
},
}
}
Expand Down Expand Up @@ -185,11 +187,12 @@ func (logger *Logger) Emergency(message string, parameters ...any) {

// Configuration struct contains configuration for the logger.
type Configuration struct {
fromLevel level.Level
toLevel level.Level
template string
file string
name string
fromLevel level.Level
toLevel level.Level
template string
file string
name string
timeFormat string
}

// Option represents option for the Configuration.
Expand Down Expand Up @@ -230,6 +233,13 @@ func WithName(name string) Option {
}
}

// WithTimeFormat sets timeFormat for the Configuration.
func WithTimeFormat(timeFormat string) Option {
return func(configuration *Configuration) {
configuration.timeFormat = timeFormat
}
}

// NewConfiguration creates a new instance of the Configuration.
func NewConfiguration(options ...Option) *Configuration {
newConfiguration := &Configuration{
Expand Down Expand Up @@ -257,7 +267,7 @@ func Configure(configuration *Configuration) {
toLevel = configuration.toLevel
template = configuration.template

newLogger := New(configuration.name)
newLogger := New(configuration.name, configuration.timeFormat)

defaultFormatter := formatter.New(configuration.template)

Expand Down
6 changes: 4 additions & 2 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/dl1998/go-logging/pkg/logger/logrecord"
"io"
"testing"
"time"
)

var (
Expand All @@ -19,6 +20,7 @@ var (
parameters = []any{
"test",
}
timeFormat = time.RFC3339
)

// MockLogger is used to mock baseLogger.
Expand Down Expand Up @@ -346,7 +348,7 @@ func BenchmarkBaseLogger_RemoveHandler(b *testing.B) {

// TestNew tests that New creates a new logger.
func TestNew(t *testing.T) {
newLogger := New(loggerName)
newLogger := New(loggerName, timeFormat)

testutils.AssertEquals(t, loggerName, newLogger.Name())

Expand All @@ -358,7 +360,7 @@ func TestNew(t *testing.T) {
// BenchmarkNew perform benchmarking of the New().
func BenchmarkNew(b *testing.B) {
for index := 0; index < b.N; index++ {
New(loggerName)
New(loggerName, timeFormat)
}
}

Expand Down
24 changes: 17 additions & 7 deletions pkg/structuredlogger/structuredlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type baseLoggerInterface interface {

// baseLogger struct contains basic fields for the logger.
type baseLogger struct {
name string
handlers []handler.Interface
name string
timeFormat string
handlers []handler.Interface
}

// Log logs interpolated message with the provided level.Level.
Expand All @@ -49,7 +50,7 @@ func (logger *baseLogger) Log(logLevel level.Level, parameters ...any) {
}
}

logRecord := logrecord.New(logger.name, logLevel, "", parametersMap, 3)
logRecord := logrecord.New(logger.name, logLevel, logger.timeFormat, parametersMap, 3)

for _, registeredHandler := range logger.handlers {
registeredHandler.Write(logRecord)
Expand Down Expand Up @@ -113,11 +114,12 @@ type Logger struct {
}

// New creates a new instance of the Logger.
func New(name string) *Logger {
func New(name string, timeFormat string) *Logger {
return &Logger{
baseLogger: &baseLogger{
name: name,
handlers: make([]handler.Interface, 0),
name: name,
timeFormat: timeFormat,
handlers: make([]handler.Interface, 0),
},
}
}
Expand Down Expand Up @@ -209,6 +211,7 @@ type Configuration struct {
pairSeparator string
file string
name string
timeFormat string
}

// Option represents option for the Configuration.
Expand Down Expand Up @@ -277,6 +280,13 @@ func WithName(name string) Option {
}
}

// WithTimeFormat sets timeFormat for the Configuration.
func WithTimeFormat(timeFormat string) Option {
return func(configuration *Configuration) {
configuration.timeFormat = timeFormat
}
}

// NewConfiguration creates a new instance of the Configuration.
func NewConfiguration(options ...Option) *Configuration {
newConfiguration := &Configuration{
Expand Down Expand Up @@ -312,7 +322,7 @@ func Configure(configuration *Configuration) {
toLevel = configuration.toLevel
template = configuration.template

newLogger := New(configuration.name)
newLogger := New(configuration.name, configuration.timeFormat)

var defaultFormatter formatter.Interface

Expand Down
Loading
Loading