Compare commits
2 Commits
ezconf/v0.
...
hlog/v0.11
| Author | SHA1 | Date | |
|---|---|---|---|
| 1745458a95 | |||
| f3d6a01105 |
@@ -1,8 +1,24 @@
|
||||
package ezconf
|
||||
|
||||
// Integration is an interface that packages can implement to provide
|
||||
type Integration struct {
|
||||
Name string
|
||||
ConfigPointer any
|
||||
ConfigFunc func() (any, error)
|
||||
GroupName string
|
||||
}
|
||||
|
||||
func NewIntegration(name, groupname string, cfgptr any, cfgfunc func() (any, error)) *Integration {
|
||||
return &Integration{
|
||||
name,
|
||||
cfgptr,
|
||||
cfgfunc,
|
||||
groupname,
|
||||
}
|
||||
}
|
||||
|
||||
// IntegrationDepr is an interface that packages can implement to provide
|
||||
// easy integration with ezconf
|
||||
type Integration interface {
|
||||
type IntegrationDepr interface {
|
||||
// Name returns the name to use when registering the config
|
||||
Name() string
|
||||
|
||||
@@ -16,8 +32,34 @@ type Integration interface {
|
||||
GroupName() string
|
||||
}
|
||||
|
||||
// AddIntegration registers a package using an Integration object returned by another package
|
||||
func (cl *ConfigLoader) AddIntegration(integration *Integration) error {
|
||||
// Add config struct for tag parsing
|
||||
configPtr := integration.ConfigPointer
|
||||
if err := cl.AddConfigStruct(configPtr, integration.GroupName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add config function
|
||||
if err := cl.AddConfigFunc(integration.Name, integration.ConfigFunc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddIntegrations registers multiple integrations at once
|
||||
func (cl *ConfigLoader) AddIntegrations(integrations ...*Integration) error {
|
||||
for _, integration := range integrations {
|
||||
if err := cl.AddIntegration(integration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterIntegration registers a package that implements the Integration interface
|
||||
func (cl *ConfigLoader) RegisterIntegration(integration Integration) error {
|
||||
func (cl *ConfigLoader) RegisterIntegration(integration IntegrationDepr) error {
|
||||
// Add config struct for tag parsing
|
||||
configPtr := integration.ConfigPointer()
|
||||
if err := cl.AddConfigStruct(configPtr, integration.GroupName()); err != nil {
|
||||
@@ -33,7 +75,7 @@ func (cl *ConfigLoader) RegisterIntegration(integration Integration) error {
|
||||
}
|
||||
|
||||
// RegisterIntegrations registers multiple integrations at once
|
||||
func (cl *ConfigLoader) RegisterIntegrations(integrations ...Integration) error {
|
||||
func (cl *ConfigLoader) RegisterIntegrations(integrations ...IntegrationDepr) error {
|
||||
for _, integration := range integrations {
|
||||
if err := cl.RegisterIntegration(integration); err != nil {
|
||||
return err
|
||||
|
||||
@@ -210,5 +210,5 @@ func TestRegisterIntegrations_PartialFailure(t *testing.T) {
|
||||
|
||||
func TestIntegration_Interface(t *testing.T) {
|
||||
// Verify that mockIntegration implements Integration interface
|
||||
var _ Integration = (*mockIntegration)(nil)
|
||||
var _ IntegrationDepr = (*mockIntegration)(nil)
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import (
|
||||
// It can be populated from environment variables using ConfigFromEnv
|
||||
// or created programmatically.
|
||||
type Config struct {
|
||||
LogLevel Level // ENV LOG_LEVEL: Log level for the logger - trace, debug, info, warn, error, fatal, panic (default: info)
|
||||
LogOutput string // ENV LOG_OUTPUT: Output destination for logs - console, file, or both (default: console)
|
||||
LogDir string // ENV LOG_DIR: Directory path for log files (required when LOG_OUTPUT is "file" or "both")
|
||||
LogFileName string // ENV LOG_FILE_NAME: Name of the log file (required when LOG_OUTPUT is "file" or "both")
|
||||
LogAppend bool // ENV LOG_APPEND: Append to existing log file or overwrite (default: true)
|
||||
LogLevel Level `ezconf:"LOG_LEVEL,description:Log level for the logger - trace debug info warn error fatal panic,default:info"`
|
||||
LogOutput string `ezconf:"LOG_OUTPUT,description:Output destination for logs - console file or both,default:console"`
|
||||
LogDir string `ezconf:"LOG_DIR,description:Directory path for log files,required:when LOG_OUTPUT is file or both"`
|
||||
LogFileName string `ezconf:"LOG_FILE_NAME,description:Name of the log file,required:when LOG_OUTPUT is file or both"`
|
||||
LogAppend bool `ezconf:"LOG_APPEND,description:Append to existing log file or overwrite,default:true"`
|
||||
}
|
||||
|
||||
// ConfigFromEnv loads logger configuration from environment variables.
|
||||
|
||||
@@ -1,35 +1,9 @@
|
||||
package hlog
|
||||
|
||||
import "runtime"
|
||||
import "git.haelnorr.com/h/golib/ezconf"
|
||||
|
||||
// EZConfIntegration provides integration with ezconf for automatic configuration
|
||||
type EZConfIntegration struct{}
|
||||
|
||||
// PackagePath returns the path to the hlog package for source parsing
|
||||
func (e EZConfIntegration) PackagePath() string {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
// Return directory of this file
|
||||
return filename[:len(filename)-len("/ezconf.go")]
|
||||
}
|
||||
|
||||
// ConfigFunc returns the ConfigFromEnv function for ezconf
|
||||
func (e EZConfIntegration) ConfigFunc() func() (interface{}, error) {
|
||||
return func() (interface{}, error) {
|
||||
return ConfigFromEnv()
|
||||
}
|
||||
}
|
||||
|
||||
// Name returns the name to use when registering with ezconf
|
||||
func (e EZConfIntegration) Name() string {
|
||||
return "hlog"
|
||||
}
|
||||
|
||||
// GroupName returns the display name for grouping environment variables
|
||||
func (e EZConfIntegration) GroupName() string {
|
||||
return "HLog"
|
||||
}
|
||||
|
||||
// NewEZConfIntegration creates a new EZConf integration helper
|
||||
func NewEZConfIntegration() EZConfIntegration {
|
||||
return EZConfIntegration{}
|
||||
// NewEZConfIntegration creates a new EZConf integration
|
||||
func NewEZConfIntegration() *ezconf.Integration {
|
||||
return ezconf.NewIntegration("hlog", "HLog",
|
||||
&Config{}, func() (any, error) { return ConfigFromEnv() })
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ require (
|
||||
github.com/rs/zerolog v1.34.0
|
||||
)
|
||||
|
||||
require git.haelnorr.com/h/golib/ezconf v0.2.1
|
||||
|
||||
require (
|
||||
git.haelnorr.com/h/golib/env v0.9.1
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
git.haelnorr.com/h/golib/env v0.9.1 h1:2Vsj+mJKnO5f1Md1GO5v9ggLN5zWa0baCewcSHTjoNY=
|
||||
git.haelnorr.com/h/golib/env v0.9.1/go.mod h1:glUQVdA1HMKX1avTDyTyuhcr36SSxZtlJxKDT5KTztg=
|
||||
git.haelnorr.com/h/golib/ezconf v0.2.1 h1:axMyKtgO9Zk6E8CrYrLpMzifvpjz73yxCQq0lOtuhck=
|
||||
git.haelnorr.com/h/golib/ezconf v0.2.1/go.mod h1:rETDcjpcEyyeBgCiZSU617wc0XycwZSC5+IAOtXmwP8=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
|
||||
Reference in New Issue
Block a user