Skip to content

main, configuration, doc: allow logging formatter to be configured - #293

Merged
stevvooe merged 3 commits into
distribution:masterfrom
stevvooe:log-formatter-configuration
Mar 25, 2015
Merged

stevvooe merged 3 commits into
distribution:masterfrom
stevvooe:log-formatter-configuration

Conversation

@stevvooe

Copy link
Copy Markdown
Collaborator

This changeset simply adds hooks into the configuration system to support multiple different kinds of output formats. These formatters are provided by logrus and include options such as "text" and "json". The configuraiton documentation has been updated accordingly.

The logrus dependency has been updated as an order of maintenance.

The logging system now has a separate configuration section. The default configuration would be as follows:

log:
  level: info
  formatter: text

This configuration would yield the standard text output, as shown below when output to a TTY:

$ ./bin/registry cmd/registry/config.yml
INFO[0000] endpoint local-8082 disabled, skipping        app.id=c2156935-05a3-4c11-86f1-b33e5a51ce17 version=v2.0.0-alpha.3-43-g3b9cbb7
INFO[0000] endpoint local-8083 disabled, skipping        app.id=c2156935-05a3-4c11-86f1-b33e5a51ce17 version=v2.0.0-alpha.3-43-g3b9cbb7
INFO[0000] listening on :5000                            app.id=c2156935-05a3-4c11-86f1-b33e5a51ce17 version=v2.0.0-alpha.3-43-g3b9cbb7
INFO[0000] debug server listening localhost:5001

Using the environment variable REGISTRY_LOG_FORMATTER, we can take a peak at the other output formats. The logrus package defines an alternative, yet simple json version:

$ REGISTRY_LOG_FORMATTER=json ./bin/registry cmd/registry/config.yml
{"level":"debug","msg":"using \"json\" logging formatter","time":"2015-03-23T20:20:55-07:00"}
{"app.id":"555f797d-e867-4742-9ff8-3d487207d549","level":"info","msg":"endpoint local-8082 disabled, skipping","time":"2015-03-23T20:20:55-07:00","version":"v2.0.0-alpha.3-43-g3b9cbb7"}
{"app.id":"555f797d-e867-4742-9ff8-3d487207d549","level":"info","msg":"endpoint local-8083 disabled, skipping","time":"2015-03-23T20:20:55-07:00","version":"v2.0.0-alpha.3-43-g3b9cbb7"}
{"app.id":"555f797d-e867-4742-9ff8-3d487207d549","level":"info","msg":"listening on :5000","time":"2015-03-23T20:20:55-07:00","version":"v2.0.0-alpha.3-43-g3b9cbb7"}
{"level":"info","msg":"debug server listening localhost:5001","time":"2015-03-23T20:20:55-07:00"}

The standard logstash output is supported, as well:

$ REGISTRY_LOG_FORMATTER=logstash ./bin/registry cmd/registry/config.yml
{"@timestamp":"2015-03-23T20:21:34-07:00","@version":1,"level":"debug","message":"using \"logstash\" logging formatter","type":"registry"}
{"@timestamp":"2015-03-23T20:21:34-07:00","@version":1,"app.id":"4c1f0020-9a23-4938-8a26-c17b4f31522a","level":"info","message":"endpoint local-8082 disabled, skipping","type":"registry","version":"v2.0.0-alpha.3-43-g3b9cbb7"}
{"@timestamp":"2015-03-23T20:21:34-07:00","@version":1,"app.id":"4c1f0020-9a23-4938-8a26-c17b4f31522a","level":"info","message":"endpoint local-8083 disabled, skipping","type":"registry","version":"v2.0.0-alpha.3-43-g3b9cbb7"}
{"@timestamp":"2015-03-23T20:21:34-07:00","@version":1,"app.id":"4c1f0020-9a23-4938-8a26-c17b4f31522a","level":"info","message":"listening on :5000","type":"registry","version":"v2.0.0-alpha.3-43-g3b9cbb7"}
{"@timestamp":"2015-03-23T20:21:34-07:00","@version":1,"level":"info","message":"debug server listening localhost:5001","type":"registry"}

Formatters can easily be added in future changes.

This PR also has support for static fields in the log lines. Let's say we add this to the configuration:

log:
  level: debug
  fields:
    service: registry
    environment: development

We now get a contextual key of "environment" and "service" on each log line:

$ ./bin/registry cmd/registry/config.yml
INFO[0000] endpoint local-8082 disabled, skipping        app.id=c088bad3-4a4c-418a-aa1e-7b8d0cc8414e environment=development service=registry version=v2.0.0-alpha.3-53-ga75f0f2.m
INFO[0000] endpoint local-8083 disabled, skipping        app.id=c088bad3-4a4c-418a-aa1e-7b8d0cc8414e environment=development service=registry version=v2.0.0-alpha.3-53-ga75f0f2.m
INFO[0000] listening on :5000                            app.id=c088bad3-4a4c-418a-aa1e-7b8d0cc8414e environment=development service=registry version=v2.0.0-alpha.3-53-ga75f0f2.m
INFO[0000] debug server listening localhost:5001

This works with environment variables, as well:

$ REGISTRY_LOG_FIELDS_FOO=bar ./bin/registry cmd/registry/config.yml
INFO[0000] endpoint local-8082 disabled, skipping        app.id=8fd84d78-49df-4f75-9055-240119a947cd environment=development foo=bar service=registry version=v2.0.0-alpha.3-53-ga75f0f2.m
INFO[0000] endpoint local-8083 disabled, skipping        app.id=8fd84d78-49df-4f75-9055-240119a947cd environment=development foo=bar service=registry version=v2.0.0-alpha.3-53-ga75f0f2.m
INFO[0000] listening on :5000                            app.id=8fd84d78-49df-4f75-9055-240119a947cd environment=development foo=bar service=registry version=v2.0.0-alpha.3-53-ga75f0f2.m
INFO[0000] debug server listening localhost:5001

Note that with this change, the "loglevel" option is now deprecated.

Closes #286.

@stevvooe stevvooe added this to the Registry/2.0.0-rc milestone Mar 24, 2015
@stevvooe

Copy link
Copy Markdown
Collaborator Author

cc @avinson @dmp42 @BrianBland

@dmp42

dmp42 commented Mar 24, 2015

Copy link
Copy Markdown
Contributor

Like it. LGTM!

@avinson

avinson commented Mar 24, 2015

Copy link
Copy Markdown
Contributor

For the JSON formatter, can we initialize like this by default:

log.SetFormatter(&log.JSONFormatter{Service: "registry"})

The "Type" field has special functionality in both Logstash and Heka so we may not want to override that by default. Still, we need a unique way of identifying different services. Either by default or provide a way in the config to add additional fields would be very helpful.

Thinking it over.. having it be configurable would be ideal so you can set Type, Service or something else without bothering the distribution team again :)

Something like:

log:
  level: info
  formatter: json
  fields:
    type: registry
    service: registry
    environment: staging

@stevvooe

Copy link
Copy Markdown
Collaborator Author

@avinson That is much preferable.

@dmp42

dmp42 commented Mar 24, 2015

Copy link
Copy Markdown
Contributor

@avinson That is much preferable.

Indeed.
You should never, ever, bother the Distribution team.
Specially @stevvooe before morning coffee :D.

@amylindburg amylindburg mentioned this pull request Mar 24, 2015
4 tasks done
This changeset simply adds hooks into the configuration system to support
multiple different kinds of output formats. These formatters are provided by
logrus and include options such as "text" and "json". The configuraiton
documentation has been updated accordingly.

Signed-off-by: Stephen J Day <[email protected]>
@BrianBland

Copy link
Copy Markdown
Contributor

LGTM

I presume we'll be removing the loglevel configuration option sometime before/around the "stable" tag?

@stevvooe

Copy link
Copy Markdown
Collaborator Author

@BrianBland That is purely up to you. I can remove it with this PR, if you're okay with it.

@BrianBland

Copy link
Copy Markdown
Contributor

Let's keep it around for a bit longer...

@stevvooe
stevvooe force-pushed the log-formatter-configuration branch 2 times, most recently from 7ec585c to 9fee680 Compare March 25, 2015 00:39
To allow flexibility in log message context information, this changeset
provides the ability to configure static fields that are included in the
context. Such fields can be set via configuration or environment variables.

Signed-off-by: Stephen J Day <[email protected]>
@stevvooe
stevvooe force-pushed the log-formatter-configuration branch from 9fee680 to ac73963 Compare March 25, 2015 01:47
@avinson

avinson commented Mar 25, 2015

Copy link
Copy Markdown
Contributor

LGTM

stevvooe added a commit that referenced this pull request Mar 25, 2015
main, configuration, doc: allow logging formatter to be configured
@stevvooe
stevvooe merged commit e9c69ff into distribution:master Mar 25, 2015
@stevvooe
stevvooe deleted the log-formatter-configuration branch March 25, 2015 20:34
@stevvooe stevvooe modified the milestones: Registry/2.0.0-rc, Registry/2.0 Mar 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add option to enable logrus JSON output

4 participants