Skip to content

Commit

Permalink
code(pkg/logger/logger) - add log level into config and add logging f…
Browse files Browse the repository at this point in the history
…or content from connector
  • Loading branch information
PxyUp committed Jan 7, 2024
1 parent 256d9bb commit acb9af9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ go run cmd/fitter/main.go --path=./examples/config_api.json
1. **--path** - string[config.yaml] - path for the configuration of the Fitter
2. **--verbose** - bool[false] - enable logging
3. **--plugins** - string[""] - [path for plugins for Fitter](https://github.com/PxyUp/fitter/blob/master/examples/plugin/README.md)

4. **--log-level** - enum["info", "error", "debug", "fatal"] - set log level(only if verbose set to true)

# How to use Fitter_CLI

Expand All @@ -128,6 +128,7 @@ go run cmd/cli/main.go --path=./examples/cli/config_cli.json
4. **--verbose** - bool[false] - enable logging
5. **--omit-error-pretty** - bool[false] - Provide pure value if pretty is invalid
6. **--plugins** - string[""] - [path for plugins for Fitter](https://github.com/PxyUp/fitter/blob/master/examples/plugin/README.md)
7. **--log-level** - enum["info", "error", "debug", "fatal"] - set log level(only if verbose set to true)

```bash
./fitter_cli_${VERSION} --path=./examples/cli/config_cli.json --copy=true
Expand Down Expand Up @@ -543,7 +544,7 @@ Common of the field
```go
type Field struct {
BaseField *BaseField `json:"base_field" yaml:"base_field"`
ObjectConfig *ObjectConfig `yaml:"object_config" yaml:"object_config"`
ObjectConfig *ObjectConfig `json:"object_config" yaml:"object_config"`
ArrayConfig *ArrayConfig `json:"array_config" yaml:"array_config"`

FirstOf []*Field `json:"first_of" yaml:"first_of"`
Expand Down
3 changes: 2 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func main() {
verboseFlag := flag.Bool("verbose", false, "Provide logger")
omitPrettyErrorFlag := flag.Bool("omit-error-pretty", false, "Provide pure value if pretty is invalid")
pluginsFlag := flag.String("plugins", "", "Provide plugins folder")
logLevel := flag.String("log-level", "info", "Level for logger")
flag.Parse()

log := logger.Null
if *verboseFlag {
log = logger.NewLogger()
log = logger.NewLogger(*logLevel)
}

if *pluginsFlag != "" {
Expand Down
3 changes: 2 additions & 1 deletion cmd/fitter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
filePath := flag.String("path", "config.yaml", "Path for config file yaml|json")
verboseFlag := flag.Bool("verbose", false, "Provide logger")
pluginsFlag := flag.String("plugins", "", "Provide plugins folder")
logLevel := flag.String("log-level", "info", "Level for logger")
flag.Parse()

if *pluginsFlag != "" {
Expand All @@ -67,7 +68,7 @@ func main() {
defer cancel()
lg := logger.Null
if *verboseFlag {
lg = logger.NewLogger()
lg = logger.NewLogger(*logLevel)
}
done := make(chan struct{})
go func() {
Expand Down
29 changes: 28 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Logger interface {
Infof(msg string, fields ...any)
Info(msg string)
Infow(msg string, fields ...string)
Debug(msg string)
Debugw(msg string, fields ...string)
Debugf(msg string, fields ...any)
Error(msg string)
Errorf(msg string, fields ...any)
Errorw(msg string, fields ...string)
Expand All @@ -33,6 +36,15 @@ func (n null) Info(msg string) {
func (n null) Infow(msg string, fields ...string) {
}

func (n null) Debugf(msg string, fields ...any) {
}

func (n null) Debug(msg string) {
}

func (n null) Debugw(msg string, fields ...string) {
}

func (n null) Error(msg string) {
}

Expand All @@ -50,6 +62,18 @@ type zapLogger struct {
logger *zap.Logger
}

func (z *zapLogger) Debug(msg string) {
z.logger.Debug(msg)
}

func (z *zapLogger) Debugw(msg string, fields ...string) {
z.logger.With(makeFields(fields)...).Debug(msg)
}

func (z *zapLogger) Debugf(msg string, fields ...any) {
z.logger.Debug(fmt.Sprintf(msg, fields...))
}

func makeFields(fields []string) []zap.Field {
zapField := make([]zap.Field, len(fields)/2)

Expand Down Expand Up @@ -90,11 +114,14 @@ func (z *zapLogger) Errorw(msg string, fields ...string) {
z.logger.With(makeFields(fields)...).Error(msg)
}

func NewLogger() *zapLogger {
func NewLogger(lvl string) *zapLogger {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.RFC3339TimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(config)
defaultLogLevel := zapcore.DebugLevel
if lvl != "" {
_ = defaultLogLevel.UnmarshalText([]byte(lvl))
}

return &zapLogger{
logger: zap.New(zapcore.NewTee(zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel)), zap.AddCaller()),
Expand Down
2 changes: 2 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func buildGeneratedField(parsedValue builder.Jsonable, field *config.GeneratedFi
return builder.Null()
}

logger.Debugw("connector answer", "content", string(body))

result, err := parserFactory(body, logger).Parse(field.Model.Model)
if err != nil {
return builder.Null()
Expand Down
2 changes: 2 additions & 0 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (p *processor) Process() (*parser.ParseResult, error) {
return nil, err
}

p.logger.Debugw("connector answer", "content", string(body))

result, err := p.parserFactory(body, p.logger).Parse(p.model)
if p.notifier != nil {
isArray := false
Expand Down

0 comments on commit acb9af9

Please sign in to comment.