commit 8b54ef7c424a5273a0ce8bdc0e251a7f9937a93e Author: Miroslav Misek Date: Thu Mar 27 16:14:28 2025 +0100 Initial commit diff --git a/.idea/logging.iml b/.idea/logging.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/logging.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..909a26f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..0e0384f --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/config.go b/config.go new file mode 100644 index 0000000..78c2780 --- /dev/null +++ b/config.go @@ -0,0 +1,16 @@ +package logging + +type Config struct { + Level string `yaml:"level" envconfig:"LEVEL"` + Format string `yaml:"format" envconfig:"FORMAT"` + Destination string `yaml:"destination" envconfig:"DESTINATION"` + DestinationFilepath string `yaml:"destination_filepath" envconfig:"DESTINATION_FILEPATH"` +} + +func NewConfig() *Config { + return &Config{ + Level: "debug", + Format: "text", + Destination: "stdout", + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..247c0d1 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module netgarden.dev/maf/logging + +go 1.24.1 + +replace netgarden.dev/maf/maf => ../maf + +require netgarden.dev/maf/maf v0.0.0-20250327102624-f25d54ddf786 + +require ( + github.com/elliotchance/orderedmap v1.6.0 // indirect + github.com/kelseyhightower/envconfig v1.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b76b8f1 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/elliotchance/orderedmap v1.6.0 h1:xjn+kbbKXeDq6v9RVE+WYwRbYfAZKvlWfcJNxM8pvEw= +github.com/elliotchance/orderedmap v1.6.0/go.mod h1:wsDwEaX5jEoyhbs7x93zk2H/qv0zwuhg4inXhDkYqys= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +netgarden.dev/maf/maf v0.0.0-20250327102624-f25d54ddf786 h1:vVUqMfvo3L32OW1e9/nmrUwX8DfIcU+QVcKzALbBIy4= +netgarden.dev/maf/maf v0.0.0-20250327102624-f25d54ddf786/go.mod h1:iipeU46tEIAvyQvLBV3AvrNVUmNkx6r1mUKRrhPo/I0= diff --git a/module.go b/module.go new file mode 100644 index 0000000..8b0b77f --- /dev/null +++ b/module.go @@ -0,0 +1,81 @@ +package logging + +import ( + "errors" + "io" + "log/slog" + "netgarden.dev/maf/maf" + "os" + "strings" +) + +func NewModule() *Module { + return &Module{} +} + +type Module struct { + manager *maf.Manager + config *Config +} + +func (m *Module) GetID() string { + return "logging" +} + +func (m *Module) GetName() string { + return "logging" +} + +func (m *Module) SetManager(manager *maf.Manager) { + m.manager = manager +} + +func (m *Module) CreateConfig() interface{} { + return NewConfig() +} + +func (m *Module) SetConfig(config interface{}) { + m.config = config.(*Config) +} + +func (m *Module) setupLogging() error { + + var w io.Writer + var level slog.Level + var handler slog.Handler + + if strings.EqualFold("stdout", m.config.Destination) { + w = os.Stdout + } else { + return errors.New("Unknown logging destination: " + m.config.Destination) + } + + if strings.EqualFold("debug", m.config.Level) { + level = slog.LevelDebug + } else if strings.EqualFold("info", m.config.Level) { + level = slog.LevelInfo + } else if strings.EqualFold("warn", m.config.Level) { + level = slog.LevelWarn + } else if strings.EqualFold("error", m.config.Level) { + level = slog.LevelError + } else { + return errors.New("Unknown logging level: " + m.config.Level) + } + + options := &slog.HandlerOptions{ + Level: level, + } + + if strings.EqualFold("text", m.config.Format) { + handler = slog.NewTextHandler(w, options) + } else if strings.EqualFold("json", m.config.Format) { + handler = slog.NewJSONHandler(w, options) + } else { + return errors.New("Unknown logging format: " + m.config.Format) + } + + logger := slog.New(handler) + slog.SetDefault(logger) + + return nil +}