Skip to content

[FEATURE] Pretty-print logging #237

Closed

Description

@james-milligan

Requirements

Flagd currently prints structured logs in json format, when running flagd in the console this can cause the logs to be difficult to read.

the zap logger zap.Config struct contains the field Encoding which when set to console instead of json updates the logging structure as follows:

json

{"level":"info","ts":1671548378.306176,"caller":"runtime/runtime.go:84","msg":"configuration change (write) for flagKey headerColor (config/samples/example_flags.json)","component":"runtime"}
{"level":"info","ts":1671548378.3061912,"caller":"runtime/runtime.go:84","msg":"configuration change (write) for flagKey myBoolFlag (config/samples/example_flags.json)","component":"runtime"}
{"level":"info","ts":1671548378.306196,"caller":"runtime/runtime.go:84","msg":"configuration change (write) for flagKey myStringFlag (config/samples/example_flags.json)","component":"runtime"}
{"level":"info","ts":1671548378.306199,"caller":"runtime/runtime.go:84","msg":"configuration change (write) for flagKey myFloatFlag (config/samples/example_flags.json)","component":"runtime"}
{"level":"info","ts":1671548378.3062022,"caller":"runtime/runtime.go:84","msg":"configuration change (write) for flagKey myIntFlag (config/samples/example_flags.json)","component":"runtime"}

console

1.671549411055416e+09   info    runtime/runtime.go:84   configuration change (write) for flagKey myFloatFlag (config/samples/example_flags.json)        {"component": "runtime"}
1.6715494110554419e+09  info    runtime/runtime.go:84   configuration change (write) for flagKey myIntFlag (config/samples/example_flags.json)  {"component": "runtime"}
1.671549411055447e+09   info    runtime/runtime.go:84   configuration change (write) for flagKey myObjectFlag (config/samples/example_flags.json)       {"component": "runtime"}
1.671549411055451e+09   info    runtime/runtime.go:84   configuration change (write) for flagKey isColorYellow (config/samples/example_flags.json)      {"component": "runtime"}

This behaviour should be controlled via a viper flag, e.g. --log-encoding=console with a json default e.g. --log-encoding=json with a console default

Activity

  1. added
    enhancement
    Needs Triage
    on Dec 20, 2022
  2. mihirm21 commented on Jan 22, 2023

    @mihirm21
    Contributor

    Hi, can I be assigned this issue in case no one else is working on it, or should I directly submit my PR?

  3. added 2 commits that reference this issue on Jan 22, 2023
  4. mihirm21 commented on Jan 22, 2023

    @mihirm21
    Contributor

    Is the issue resolved now ?

  5. james-milligan commented on Jan 25, 2023

    @james-milligan
    ContributorAuthor

    Ive requested changes in the PR
    #302 (review)

  6. mihirm21 commented on Jan 25, 2023

    @mihirm21
    Contributor

    are you asking for changes in source files of zap encoder? other than that, I am not getting where to add this feature, plz help

  7. james-milligan commented on Jan 25, 2023

    @james-milligan
    ContributorAuthor

    You can introduce a new cli flag in cmd/start.go, mirroring one of the other existing flags and setting the default to "console"
    This flag value can be retrieved with viper.GetString(YOUR_FLAG_NAME) and passed into the constructor of the zap the logger setting the zap.Config Encoding property to the value passed on startup.

    This way we set the default to console while still allowing for developers to use structured json logging if they require it

  8. mihirm21 commented on Jan 25, 2023

    @mihirm21
    Contributor

    how do I pass this value(viper.GetString(flag name)) to the zap.config encoding parameter

  9. james-milligan commented on Jan 25, 2023

    @james-milligan
    ContributorAuthor

    One option is to update the NewZapLogger signature to the following:

    func NewZapLogger(level zapcore.Level, logFormat string) (*zap.Logger, error)
    

    or

    update the NewZapLogger signature to the following:

    func NewZapLogger(config zap.Config) (*zap.Logger, error)
    

    and apply the remaining default values within the NewZapLogger method, e.g.
    start.go:

    l, err := logger.NewZapLogger(zap.Config{
    	Encoding:         viper.GetString(flag name),
    	Level:                zap.NewAtomicLevelAt(level),
    })
    

    logger.go:

    func NewZapLogger(cfg zap.Config) (*zap.Logger, error) {
            if cfg.Encoding == "" {
                    cfg.Encoding = "console"
            }
            ...
            ...
            return cfg.Build()	
    }
    

    I am sure there are other options as well if you decide to take a different route

  10. mihirm21 commented on Jan 26, 2023

    @mihirm21
    Contributor

    Is the issue resolved now?

  11. beeme1mr commented on Jan 26, 2023

    @beeme1mr
    Member

    Hi @mihirm21, the issue will automatically be resolved once your PR is accepted and merged. Thanks for your contributions to the project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs Triageenhancementgood first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions