✨ Separate formatters in config
This commit is contained in:
parent
99f6dc3f8c
commit
da3b524925
6 changed files with 56 additions and 11 deletions
|
|
@ -27,8 +27,14 @@ dotFolder = true
|
||||||
[output.stdout]
|
[output.stdout]
|
||||||
enabled = true
|
enabled = true
|
||||||
[output.stdout.config]
|
[output.stdout.config]
|
||||||
# Whether to output as JSON. Maybe useful to pipe elsewhere.
|
# Formatter to use when outputting to stdout
|
||||||
json = false
|
formatter = "plain"
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
|
||||||
|
[formatters.json]
|
||||||
|
# Set to true to pretty print JSON output
|
||||||
|
pretty_print = false
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ func Load() (Config, error) {
|
||||||
}
|
}
|
||||||
env.Parse(&c)
|
env.Parse(&c)
|
||||||
c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"])
|
c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"])
|
||||||
|
c.Formatters["json"] = loadJsonFormat(c.Formatters["json"])
|
||||||
|
|
||||||
l := ""
|
l := ""
|
||||||
for k, v := range Overrides {
|
for k, v := range Overrides {
|
||||||
|
|
@ -77,3 +78,21 @@ func (oo Outputs) Stdout() (s Stdout, enabled bool) {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadJsonFormat(c map[string]any) map[string]any {
|
||||||
|
jf := JsonFormat{}
|
||||||
|
mapst.Decode(c, &jf)
|
||||||
|
env.Parse(&jf)
|
||||||
|
mapst.Decode(jf, &c)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ff Formatters) Json() (jf JsonFormat) {
|
||||||
|
o, ok := ff["json"]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mapst.Decode(o, &jf)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,12 +65,25 @@ func TestStdoutMissing(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStdoutLoad(t *testing.T) {
|
func TestStdoutLoad(t *testing.T) {
|
||||||
os.Setenv("LOG_STDOUT_JSON", "true")
|
os.Setenv("LOG_STDOUT_FORMATTER", "json")
|
||||||
defer os.Unsetenv("LOG_STDOUT_JSON")
|
defer os.Unsetenv("LOG_STDOUT_FORMATTER")
|
||||||
os.Setenv("LOG_STDOUT_ENABLED", "true")
|
os.Setenv("LOG_STDOUT_ENABLED", "true")
|
||||||
defer os.Unsetenv("LOG_STDOUT_ENABLED")
|
defer os.Unsetenv("LOG_STDOUT_ENABLED")
|
||||||
c, _ := Load()
|
c, _ := Load()
|
||||||
std, en := c.Outputs.Stdout()
|
std, en := c.Outputs.Stdout()
|
||||||
assert.True(t, en)
|
assert.True(t, en)
|
||||||
assert.True(t, std.Json)
|
assert.Equal(t, "json", std.Formatter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormatJson(t *testing.T) {
|
||||||
|
ff := Formatters{
|
||||||
|
"json": map[string]any{"pretty_print": true},
|
||||||
|
}
|
||||||
|
|
||||||
|
js := ff.Json()
|
||||||
|
assert.True(t, js.PrettyPrint)
|
||||||
|
|
||||||
|
ff = Formatters{}
|
||||||
|
js = ff.Json()
|
||||||
|
assert.False(t, js.PrettyPrint)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Input Input
|
Input Input
|
||||||
Outputs Outputs `toml:"output"`
|
Outputs Outputs `toml:"output"`
|
||||||
|
Formatters Formatters
|
||||||
}
|
}
|
||||||
|
|
||||||
type Input struct {
|
type Input struct {
|
||||||
|
|
@ -20,9 +21,15 @@ type Output struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stdout struct {
|
type Stdout struct {
|
||||||
Json bool `env:"LOG_STDOUT_JSON" mapstructure:"json"`
|
Formatter string `env:"LOG_STDOUT_FORMATTER" mapstructure:"formatter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type stdoutEnabled struct {
|
type stdoutEnabled struct {
|
||||||
Enabled bool `env:"LOG_STDOUT_ENABLED"`
|
Enabled bool `env:"LOG_STDOUT_ENABLED"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Formatters map[string]map[string]any
|
||||||
|
|
||||||
|
type JsonFormat struct {
|
||||||
|
PrettyPrint bool `env:"LOG_JSON_PRETTY_PRINT" mapstructure:"pretty_print"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"codeberg.org/danjones000/my-log/config"
|
"codeberg.org/danjones000/my-log/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type formatMaker func(oo config.Outputs) (Formatter, error)
|
type formatMaker func(config.Formatters) (Formatter, error)
|
||||||
|
|
||||||
var formatterMap = map[string]formatMaker{
|
var formatterMap = map[string]formatMaker{
|
||||||
"plain": newPlain,
|
"plain": newPlain,
|
||||||
|
|
@ -19,7 +19,7 @@ func New(kind string) (f Formatter, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if make, ok := formatterMap[kind]; ok {
|
if make, ok := formatterMap[kind]; ok {
|
||||||
return make(conf.Outputs)
|
return make(conf.Formatters)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New("unimplemented")
|
return nil, errors.New("unimplemented")
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"codeberg.org/danjones000/my-log/tools"
|
"codeberg.org/danjones000/my-log/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newPlain(oo config.Outputs) (Formatter, error) {
|
func newPlain(ff config.Formatters) (Formatter, error) {
|
||||||
return &PlainText{}, nil
|
return &PlainText{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue