Skip to content

Commit

Permalink
Add the function CreateLoggerFromConfig to create a logger from the c…
Browse files Browse the repository at this point in the history
…onfig file (#33)
  • Loading branch information
mstmdev authored May 10, 2024
1 parent e638745 commit 70ec50c
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 1 deletion.
81 changes: 81 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package log

import (
"fmt"
"os"
"strings"

"github.com/no-src/log/level"
"gopkg.in/yaml.v3"
)

const (
consoleLoggerType = "console"
fileLoggerType = "file"
emptyLoggerType = "empty"
)

// CreateLoggerFromConfig create a logger from config file
func CreateLoggerFromConfig(configFile string) (Logger, error) {
data, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
var conf config
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return createLoggers(conf.Loggers)
}

func createLoggers(configs []loggerConfig) (Logger, error) {
var loggers []Logger
for _, logConf := range configs {
logger, err := createLogger(logConf)
if err != nil {
return nil, err
}
loggers = append(loggers, logger)
}
length := len(loggers)
if length == 0 {
return NewEmptyLogger(), nil
} else if length == 1 {
return loggers[0], nil
} else {
return NewMultiLogger(loggers...), nil
}
}

func createLogger(logConf loggerConfig) (Logger, error) {
var logger Logger
loggerType := strings.ToLower(logConf.Type)
switch loggerType {
case consoleLoggerType:
lvl, err := level.ParseLevel(logConf.Level)
if err != nil {
return nil, err
}
logger = NewConsoleLogger(lvl)
case fileLoggerType:
opt, err := toFileLoggerOption(logConf)
if err != nil {
return nil, err
}
fl, err := NewFileLoggerWithOption(opt)
if err != nil {
return nil, err
}
logger = fl
case emptyLoggerType:
logger = NewEmptyLogger()
default:
return nil, fmt.Errorf("unsupported logger type: %s", logConf.Type)
}

if loggerType != emptyLoggerType && logger != nil && logConf.Sample < 1 {
logger = NewDefaultSampleLogger(logger, logConf.Sample)
}
return logger, nil
}
15 changes: 15 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package log

import (
"testing"
)

func TestCreateLoggerFromConfig(t *testing.T) {
logger, err := CreateLoggerFromConfig("./testdata/conf.yaml")
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(logger)
defer Close()
testLogs(t)
}
38 changes: 38 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package log

import (
"time"

"github.com/no-src/log/level"
"github.com/no-src/log/option"
)

type config struct {
Loggers []loggerConfig `yaml:"loggers"`
}

type loggerConfig struct {
// common fields
Name string `yaml:"name"`
Type string `yaml:"type"`
Level string `yaml:"level"`
Format string `yaml:"format"`
TimeFormat string `yaml:"time-format"`
Sample float64 `yaml:"sample"`

// file logger fields
LogDir string `yaml:"log-dir"`
LogFilePrefix string `yaml:"log-file-prefix"`
AutoFlush bool `yaml:"auto-flush"`
AutoFlushInterval time.Duration `yaml:"auto-flush-interval"`
SplitByDate bool `yaml:"split-by-date"`
}

func toFileLoggerOption(logConf loggerConfig) (opt option.FileLoggerOption, err error) {
lvl, err := level.ParseLevel(logConf.Level)
if err != nil {
return opt, err
}
opt = option.NewFileLoggerOption(lvl, logConf.LogDir, logConf.LogFilePrefix, logConf.AutoFlush, logConf.AutoFlushInterval, logConf.SplitByDate)
return opt, nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/no-src/log

go 1.19
go 1.19

require gopkg.in/yaml.v3 v3.0.1
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 20 additions & 0 deletions testdata/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
loggers:
- name: "console logger"
type: "console"
level: "debug"
format: "text"
time-format: "2006-01-02 15:04:05"
sample: 1
- name: "file logger"
type: "file"
level: "info"
format: "json"
time-format: "2006-01-02 15:04:05"
log-dir: "./logs"
log-file-prefix: "conf_logger_"
auto-flush: true
auto-flush-interval: 3s
split-by-date: true
sample: 0.5
- name: "empty logger"
type: "empty"

0 comments on commit 70ec50c

Please sign in to comment.