1
0
mirror of https://github.com/khorevaa/logos.git synced 2025-01-22 05:10:53 +02:00

fix: read cfg from env

This commit is contained in:
Aleksey Khorev 2021-02-02 00:01:37 +03:00
parent 96f9c24e4e
commit 7b6a08e894
8 changed files with 218 additions and 16 deletions

View File

@ -15,7 +15,7 @@ This project is a wrapper around the excellent logging framework zap.
* Appenders
- `Console`, *write to console*
- `File`, *any log file*
- `gelfupd`, *greylog logger*
- `GelfUpd`, *greylog logger*
- `RollingFile`, *rolling file writing & compress*
* Encoders
- `Console`, *colorful & formatting text for console*
@ -72,7 +72,7 @@ appenders:
encoder:
json:
loggerConfigs:
loggers:
root:
level: info
appender_refs:
@ -113,7 +113,7 @@ appenders:
info_level: blue+b
debug_level: green+b
loggerConfigs:
loggers:
root:
level: debug
appender_refs:

View File

@ -1,6 +1,8 @@
package config
import "github.com/khorevaa/logos/internal/common"
import (
"github.com/khorevaa/logos/internal/common"
)
const DefaultConfig = `
appenders:
@ -9,16 +11,11 @@ appenders:
target: stdout
encoder:
console:
time_encoder: ISO8601
loggerConfigs:
loggers:
root:
level: info
appender_refs:
- CONSOLE
logger:
- name: stdlog
level: info
add_caller: true
scan: false
scan_period: 1m
@ -26,7 +23,7 @@ scan_period: 1m
type Config struct {
Appenders map[string][]*common.Config `logos-config:"appenders"`
Loggers Loggers `logos-config:"loggerConfigs"`
Loggers Loggers `logos-config:"loggers"`
}
type ScanConfig struct {

View File

@ -24,7 +24,7 @@ func TestConfigFrom(t *testing.T) {
name: "simple",
config: struct {
Appenders map[string][]interface{} `logos-config:"appenders"`
Loggers Loggers `logos-config:"loggerConfigs"`
Loggers Loggers `logos-config:"loggers"`
}{
Appenders: map[string][]interface{}{
"console": {struct {
@ -71,3 +71,41 @@ func TestConfigFrom(t *testing.T) {
})
}
}
func TestConfigFromEnv(t *testing.T) {
tests := []struct {
name string
env []string
text []string
}{
{
name: "simple",
env: []string{"loggers.root.level=debug"},
text: []string{"hello world", "hello"},
},
}
//ParseConfigFromEnv("appenders.console.1.name=CONSOLE_TEST;" +
// "appenders.console.1.target=stdout;" +
// "appenders.console.1.no_color=true;" +
// "appenders.console.1.encoder.console;" +
// "loggers.logger.0.add_caller=true;" +
// "loggers.logger.0.level=debug;" +
// "loggers.logger.0.name=stdlog;" +
// "loggers.root.appender_refs.0=CONSOLE;" +
// "loggers.root.level=error")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := common.NewConfigFrom(tt.env)
//defConfig, err := common.NewConfigFrom(DefaultConfig)
//cfg, err = common.MergeConfigs(defConfig, cfg)
if err != nil {
t.Fatalf(err.Error())
}
})
}
}

7
errors.go Normal file
View File

@ -0,0 +1,7 @@
package logos
import "errors"
var (
ErrEnvConfigNotSet = errors.New("environment variable 'LOGOS_CONFIG' is not set")
)

View File

@ -20,7 +20,7 @@ appenders:
info_level: blue+b
debug_level: green+b
loggerConfigs:
loggers:
root:
level: debug
appender_refs:
@ -55,7 +55,7 @@ appenders:
encoder:
json:
loggerConfigs:
loggers:
root:
level: info
appender_refs:
@ -83,7 +83,7 @@ appenders:
info_level: blue+b
debug_level: green+b
loggerConfigs:
loggers:
root:
level: debug
appender_refs:

View File

@ -124,6 +124,10 @@ func (c *Config) HasField(name string) bool {
return c.access().HasField(name)
}
func (c *Config) FlattenedKeys(options ...ucfg.Option) []string {
return c.access().FlattenedKeys(options...)
}
func (c *Config) CountField(name string) (int, error) {
return c.access().CountField(name)
}

View File

@ -12,6 +12,7 @@ import (
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
@ -104,6 +105,17 @@ func init() {
panic(err)
}
envConfig, err := parseConfigFromEnv()
if err != nil && debug {
fmt.Printf("logos loading config from env err: %s", err)
}
if envConfig != nil {
rawConfig, err = common.MergeConfigs(rawConfig, envConfig)
if err != nil {
fmt.Printf("logos merge configs err: %s", err)
}
}
manager, err = newLogManager(rawConfig)
if err != nil {
@ -120,6 +132,37 @@ func init() {
}()
}
func parseConfigFromEnv() (*common.Config, error) {
configData := os.Getenv("LOGOS_CONFIG")
if configData == "" {
return nil, ErrEnvConfigNotSet
}
return parseConfigFromString(configData)
}
func parseConfigFromString(configData string) (*common.Config, error) {
newConfig := common.NewConfig()
data := strings.Split(configData, ";")
for _, strData := range data {
pathValue := strings.Split(strData, "=")
value := ""
path := pathValue[0]
if len(pathValue) == 2 {
value = pathValue[1]
}
err := newConfig.SetString(path, -1, value)
if debug {
fmt.Errorf("error loading config from path %s err <%s>", path, err)
}
}
return newConfig, nil
}
func InitWithConfigContent(content string) error {
initLocker.Lock()

View File

@ -1,8 +1,12 @@
package logos
import (
"github.com/khorevaa/logos/config"
"github.com/khorevaa/logos/internal/common"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"reflect"
"strings"
"testing"
)
@ -24,7 +28,7 @@ appenders:
max_size: 100
encoder:
json:
loggerConfigs:
loggers:
root:
level: info
appender_refs:
@ -63,3 +67,112 @@ loggerConfigs:
l2.Warn("hello world test/logger/v1", zap.Int("key", 123), zap.Bool("bool", false), zap.Any("bools", []bool{false, true, true}))
}
func Test_parseConfigFromString(t *testing.T) {
tests := []struct {
name string
args []string
want config.Config
wantErr bool
}{
{
"root debug",
[]string{
//"appenders.console.1.name=CONSOLE_TEST",
//"appenders.console.1.target=stdout",
//"appenders.console.1.no_color=true",
//"appenders.console.1.encoder.console",
//"loggers.logger.0.add_caller=true",
//"loggers.logger.0.level=debug",
//"loggers.logger.0.name=stdlog",
//"loggers.root.appender_refs.0=CONSOLE",
"loggers.root.level=debug",
},
config.Config{
Loggers: config.Loggers{
Root: config.RootLogger{
Level: "debug",
},
},
},
false,
},
{
"add logger",
[]string{
//"appenders.console.1.name=CONSOLE_TEST",
//"appenders.console.1.target=stdout",
//"appenders.console.1.no_color=true",
//"appenders.console.1.encoder.console",
"loggers.logger.0.add_caller=true",
"loggers.logger.0.level=debug",
"loggers.logger.0.name=github.com/khorevaa/logos",
"loggers.logger.0.appender_refs.0=CONSOLE",
},
config.Config{
Loggers: config.Loggers{
Logger: []config.LoggerConfig{{
Level: "debug",
AddCaller: true,
Name: "github.com/khorevaa/logos",
AppenderRefs: []string{"CONSOLE"},
},
},
},
},
false,
},
{
"add appender",
[]string{
"appenders.console.0.name=CONSOLE_TEST",
"appenders.console.0.target=stdout",
"appenders.console.0.no_color=true",
"appenders.console.0.encoder.console",
"loggers.logger.0.add_caller=true",
"loggers.logger.0.level=debug",
"loggers.logger.0.name=github.com/khorevaa/logos",
"loggers.logger.0.appender_refs.0=CONSOLE_TEST",
},
config.Config{
Appenders: map[string][]*common.Config{
"console": {
&common.Config{},
},
},
Loggers: config.Loggers{
Logger: []config.LoggerConfig{{
Level: "debug",
AddCaller: true,
Name: "github.com/khorevaa/logos",
AppenderRefs: []string{"CONSOLE"},
},
},
},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseConfigFromString(strings.Join(tt.args, ";"))
if (err != nil) != tt.wantErr {
t.Errorf("parseConfigFromString() error = %v, wantErr %v", err, tt.wantErr)
return
}
var cfg config.Config
err = got.Unpack(&cfg)
if (err != nil) != tt.wantErr {
t.Errorf("parseConfigFromString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(cfg, tt.want) {
t.Errorf("parseConfigFromString() got = %v, want %v", cfg, tt.want)
}
})
}
}