-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the function CreateLoggerFromConfig to create a logger from the c…
…onfig file (#33)
- Loading branch information
Showing
6 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |