Initial commit

This commit is contained in:
Miroslav Misek 2025-03-27 16:14:28 +01:00
commit 8b54ef7c42
7 changed files with 152 additions and 0 deletions

8
.idea/logging.iml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/logging.iml" filepath="$PROJECT_DIR$/.idea/logging.iml" />
</modules>
</component>
</project>

9
.idea/workspace.xml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectViewState">
<option name="autoscrollFromSource" value="true" />
<option name="autoscrollToSource" value="true" />
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
</project>

16
config.go Normal file
View File

@ -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",
}
}

13
go.mod Normal file
View File

@ -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
)

17
go.sum Normal file
View File

@ -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=

81
module.go Normal file
View File

@ -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
}