-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TT-12355] added log format config, func and test cases #6344
Conversation
PR Reviewer Guide 🔍
|
API Changes --- prev.txt 2024-09-05 10:52:10.917725882 +0000
+++ current.txt 2024-09-05 10:52:07.753711925 +0000
@@ -5633,6 +5633,10 @@
// If not set or left empty, it will default to `info`.
LogLevel string `json:"log_level"`
+ // You can now configure the log format to be either the standard or json format
+ // If not set or left empty, it will default to `standard`.
+ LogFormat string `json:"log_format"`
+
// Section for configuring OpenTracing support
// Deprecated: use OpenTelemetry instead.
Tracer Tracer `json:"tracing"`
@@ -8554,8 +8558,6 @@
func (gw *Gateway) InitHostCheckManager(ctx context.Context, store storage.Handler)
-func (gw *Gateway) InitializeRPCCache()
-
func (gw *Gateway) LoadAPI(specs ...*APISpec) (out []*APISpec)
func (gw *Gateway) LoadDefinitionsFromRPCBackup() ([]*APISpec, error)
@@ -10857,25 +10859,57 @@
FUNCTIONS
func Get() *logrus.Logger
+ Get returns the default configured logger.
+
func GetRaw() *logrus.Logger
+ GetRaw is used internally. Should likely be removed first, do not rely on
+ it.
+
func LoadTranslations(thing map[string]interface{})
LoadTranslations takes a map[string]interface and flattens it to
- map[string]string Because translations have been loaded - we internally
- override log the formatter Nested entries are accessible using dot notation.
- example: `{"foo": {"bar": "baz"}}` flattened: `foo.bar: baz`
+ map[string]string. Because translations have been loaded - we internally
+ override log the formatter. Nested entries are accessible using dot
+ notation.
+
+ Example: `{"foo": {"bar": "baz"}}` Flattened: `foo.bar: baz`
+func NewFormatter(format string) logrus.Formatter
TYPES
+type JSONFormatter struct {
+ // TimestampFormat sets the format used for marshaling timestamps.
+ // The format to use is the same than for time.Format or time.Parse from the standard
+ // library.
+ // The standard Library already provides a set of predefined format.
+ TimestampFormat string
+
+ // DisableTimestamp allows disabling automatic timestamps in output.
+ DisableTimestamp bool
+
+ // DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
+ DataKey string
+}
+ JSONFormatter formats logs into parsable json.
+
+func (f *JSONFormatter) Format(entry *logrus.Entry) ([]byte, error)
+ Format renders a single log entry
+
type RawFormatter struct{}
+ RawFormatter returns the logrus entry message as bytes.
func (f *RawFormatter) Format(entry *logrus.Entry) ([]byte, error)
+ Format returns the entry.Message as a []byte.
type TranslationFormatter struct {
- *logrus.TextFormatter
+ logrus.Formatter
}
+ TranslationFormatter handles message reformatting with translations.
func (t *TranslationFormatter) Format(entry *logrus.Entry) ([]byte, error)
+ Format will translate the log message based on the message code. This is a
+ HTTP response code if provided. The message is usually just "Finished" for
+ those cases, this would likely produce a better log message.
# Package: ./regexp
|
PR Code Suggestions ✨
|
231c753
to
4185f30
Compare
I have covered the change with some benchmarks.
This shows improved performance under |
a037ebc
to
a0c4423
Compare
9448ea9
to
1c83a47
Compare
Quality Gate failedFailed conditions See analysis details on SonarCloud Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
44e7e65
to
1bd596b
Compare
1bd596b
to
40af82b
Compare
Quality Gate failedFailed conditions See analysis details on SonarCloud Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
/release to release-5.3 |
Working on it! Note that it can take a few minutes. |
### **FR Jira Ticket** <!-- Provide a general summary of your changes in the Title above --> https://tyktech.atlassian.net/browse/TT-12355 ## Description - Added the `TYK_GW_LOGFORMAT` Gateway config option - The Tyk Gateway will determine which format should be used for STDOUT logs - If the `TYK_GW_LOGFORMAT` is set to `json` the Gateway will output logs in JSON format - if the `TYK_GW_LOGFORMAT` is set to `""` empty string or any other string value it will default the TEXT format Additionally, TYK_LOGFORMAT is supported, as is TYK_LOGLEVEL (and TYK_GW_LOGLEVEL). Note that there is some global use from the log package, I don't think we can set json output from json config, as it would be picked up too late. The one case to cover is to make sure this passes with only config enabled: ``` # TYK_GW_LOGFORMAT=json ./tyk 2>&1 | head -n 1 {"level":"info","msg":"Tyk API Gateway v5.3.0-dev","prefix":"main","time":"2024-07-10T15:16:11+02:00"} # TYK_LOGFORMAT=json ./tyk 2>&1 | head -n 1 {"level":"info","msg":"Tyk API Gateway v5.3.0-dev","prefix":"main","time":"2024-07-10T15:16:14+02:00"} # ./tyk 2>&1 | head -n 1 {"level":"info","msg":"Tyk API Gateway v5.3.0-dev","prefix":"main","time":"2024-07-10T15:45:02+02:00"} ``` The above confirms reading from env and from config. ## Related Issue <!-- This project only accepts pull requests related to open issues. --> <!-- If suggesting a new feature or change, please discuss it in an issue first. --> <!-- If fixing a bug, there should be an issue describing it with steps to reproduce. --> <!-- OSS: Please link to the issue here. Tyk: please create/link the JIRA ticket. --> ## Motivation and Context The option to determine which STDOUT log format will open the opportunity for customers to easily parse the STDOUT logs and open up opportunity for our team to easily write tests and parse as needed. ## How This Has Been Tested - Manual testing - Unit testing - Benchmarks added ___ ### **PR Type** Enhancement, Tests ___ ### **Description** - Added the `TYK_GW_LOGFORMAT` Gateway config option to determine log format. - Implemented `NewFormatter` function to switch between JSON and Text log formats. - Modified log initialization to use the new formatter function. - Added unit tests to verify the correct formatter is used based on the input format. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement </strong></td><td><table> <tr> <td> <details> <summary><strong>log.go</strong><dd><code>Add log format configuration and initialization</code> </dd></summary> <hr> log/log.go <li>Added <code>NewFormatter</code> function to handle log format based on <br><code>TYK_GW_LOGFORMAT</code> environment variable.<br> <li> Modified the default log formatter initialization to use <code>NewFormatter</code>.<br> <br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6344/files#diff-894630a7ed925c7768a861c4465bc9ad393f7937df7cac5c77ab123c03921aee">+17/-6</a> </td> </tr> </table></td></tr><tr><td><strong>Tests </strong></td><td><table> <tr> <td> <details> <summary><strong>log_test.go</strong><dd><code>Add unit tests for log formatter configuration</code> </dd></summary> <hr> log/log_test.go <li>Added unit tests for <code>NewFormatter</code> function.<br> <li> Tested JSON and Text formatters based on input format.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6344/files#diff-86c6c547a3e3c686a598e339acecbf5190185d424d96f9b9a0fff3d8513a90eb">+61/-0</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions --------- Co-authored-by: Long Le <tyk-longle@Tyk-longle-MacBook-Pro.local> Co-authored-by: Tit Petric <tit@tyk.io> (cherry picked from commit 63c26ed)
@titpetric Succesfully merged PR |
…test cases (#6344) [TT-12355] added log format config, func and test cases (#6344) ### **FR Jira Ticket** <!-- Provide a general summary of your changes in the Title above --> https://tyktech.atlassian.net/browse/TT-12355 ## Description - Added the `TYK_GW_LOGFORMAT` Gateway config option - The Tyk Gateway will determine which format should be used for STDOUT logs - If the `TYK_GW_LOGFORMAT` is set to `json` the Gateway will output logs in JSON format - if the `TYK_GW_LOGFORMAT` is set to `""` empty string or any other string value it will default the TEXT format Additionally, TYK_LOGFORMAT is supported, as is TYK_LOGLEVEL (and TYK_GW_LOGLEVEL). Note that there is some global use from the log package, I don't think we can set json output from json config, as it would be picked up too late. The one case to cover is to make sure this passes with only config enabled: ``` # TYK_GW_LOGFORMAT=json ./tyk 2>&1 | head -n 1 {"level":"info","msg":"Tyk API Gateway v5.3.0-dev","prefix":"main","time":"2024-07-10T15:16:11+02:00"} # TYK_LOGFORMAT=json ./tyk 2>&1 | head -n 1 {"level":"info","msg":"Tyk API Gateway v5.3.0-dev","prefix":"main","time":"2024-07-10T15:16:14+02:00"} # ./tyk 2>&1 | head -n 1 {"level":"info","msg":"Tyk API Gateway v5.3.0-dev","prefix":"main","time":"2024-07-10T15:45:02+02:00"} ``` The above confirms reading from env and from config. ## Related Issue <!-- This project only accepts pull requests related to open issues. --> <!-- If suggesting a new feature or change, please discuss it in an issue first. --> <!-- If fixing a bug, there should be an issue describing it with steps to reproduce. --> <!-- OSS: Please link to the issue here. Tyk: please create/link the JIRA ticket. --> ## Motivation and Context The option to determine which STDOUT log format will open the opportunity for customers to easily parse the STDOUT logs and open up opportunity for our team to easily write tests and parse as needed. ## How This Has Been Tested - Manual testing - Unit testing - Benchmarks added ___ ### **PR Type** Enhancement, Tests ___ ### **Description** - Added the `TYK_GW_LOGFORMAT` Gateway config option to determine log format. - Implemented `NewFormatter` function to switch between JSON and Text log formats. - Modified log initialization to use the new formatter function. - Added unit tests to verify the correct formatter is used based on the input format. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement </strong></td><td><table> <tr> <td> <details> <summary><strong>log.go</strong><dd><code>Add log format configuration and initialization</code> </dd></summary> <hr> log/log.go <li>Added <code>NewFormatter</code> function to handle log format based on <br><code>TYK_GW_LOGFORMAT</code> environment variable.<br> <li> Modified the default log formatter initialization to use <code>NewFormatter</code>.<br> <br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6344/files#diff-894630a7ed925c7768a861c4465bc9ad393f7937df7cac5c77ab123c03921aee">+17/-6</a> </td> </tr> </table></td></tr><tr><td><strong>Tests </strong></td><td><table> <tr> <td> <details> <summary><strong>log_test.go</strong><dd><code>Add unit tests for log formatter configuration</code> </dd></summary> <hr> log/log_test.go <li>Added unit tests for <code>NewFormatter</code> function.<br> <li> Tested JSON and Text formatters based on input format.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6344/files#diff-86c6c547a3e3c686a598e339acecbf5190185d424d96f9b9a0fff3d8513a90eb">+61/-0</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions --------- Co-authored-by: Long Le <tyk-longle@Tyk-longle-MacBook-Pro.local> Co-authored-by: Tit Petric <tit@tyk.io>
…unc and test cases (#6344)" (#6485) ### **User description** Reverts #6484 ___ ### **PR Type** enhancement, bug fix ___ ### **Description** - Removed the `LogFormat` configuration option from the codebase and related schema. - Refactored and renamed initialization functions in the gateway server code. - Simplified log formatter setup by removing the JSONFormatter and related functionality. - Removed the `github.com/goccy/go-json` dependency from the project. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>config.go</strong><dd><code>Remove LogFormat configuration option</code> </dd></summary> <hr> config/config.go - Removed the `LogFormat` configuration option. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-fe44f09c4d5977b5f5eaea29170b6a0748819c9d02271746a20d81a5f3efca17">+0/-4</a> </td> </tr> <tr> <td> <details> <summary><strong>server.go</strong><dd><code>Refactor and rename initialization functions</code> </dd></summary> <hr> gateway/server.go <li>Renamed <code>initRPCCache</code> to <code>InitializeRPCCache</code>.<br> <li> Renamed <code>initSystem</code> to <code>initialiseSystem</code>.<br> <li> Removed log format initialization.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-4652d1bf175a0be8f5e61ef7177c9666f23e077d8626b73ac9d13358fa8b525b">+21/-31</a> </td> </tr> <tr> <td> <details> <summary><strong>testutil.go</strong><dd><code>Update test setup function name</code> </dd></summary> <hr> gateway/testutil.go - Renamed `initSystem` to `initialiseSystem` in test setup. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-7aaf6ae49fb8f58a8c99d337fedd15b3e430dd928ed547e425ef429b10d28ce8">+1/-1</a> </td> </tr> <tr> <td> <details> <summary><strong>log.go</strong><dd><code>Simplify log formatter setup and remove JSONFormatter</code> </dd></summary> <hr> log/log.go <li>Removed JSONFormatter and related functionality.<br> <li> Simplified log formatter setup.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-894630a7ed925c7768a861c4465bc9ad393f7937df7cac5c77ab123c03921aee">+43/-59</a> </td> </tr> </table></td></tr><tr><td><strong>Configuration changes</strong></td><td><table> <tr> <td> <details> <summary><strong>schema.json</strong><dd><code>Remove log_format option from schema</code> </dd></summary> <hr> cli/linter/schema.json - Removed `log_format` configuration option from schema. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-103cec746d3e61d391c5a67c171963f66fea65d651d704d5540e60aa5d574f46">+0/-4</a> </td> </tr> </table></td></tr><tr><td><strong>Dependencies</strong></td><td><table> <tr> <td> <details> <summary><strong>go.mod</strong><dd><code>Remove go-json dependency</code> </dd></summary> <hr> go.mod - Removed dependency on `github.com/goccy/go-json`. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-33ef32bf6c23acb95f5902d7097b7a1d5128ca061167ec0716715b0b9eeaa5f6">+0/-1</a> </td> </tr> <tr> <td> <details> <summary><strong>go.sum</strong><dd><code>Remove go-json checksums</code> </dd></summary> <hr> go.sum - Removed checksums for `github.com/goccy/go-json`. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6485/files#diff-3295df7234525439d778f1b282d146a4f1ff6b415248aaac074e8042d9f42d63">+0/-2</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions
FR Jira Ticket
https://tyktech.atlassian.net/browse/TT-12355
Description
TYK_GW_LOGFORMAT
Gateway config optionTYK_GW_LOGFORMAT
is set tojson
the Gateway will output logs in JSON formatTYK_GW_LOGFORMAT
is set to""
empty string or any other string value it will default the TEXT formatAdditionally, TYK_LOGFORMAT is supported, as is TYK_LOGLEVEL (and TYK_GW_LOGLEVEL).
Note that there is some global use from the log package, I don't think we can set json output from json config, as it would be picked up too late. The one case to cover is to make sure this passes with only config enabled:
The above confirms reading from env and from config.
Related Issue
Motivation and Context
The option to determine which STDOUT log format will open the opportunity for customers to easily parse the STDOUT logs and open up opportunity for our team to easily write tests and parse as needed.
How This Has Been Tested
PR Type
Enhancement, Tests
Description
TYK_GW_LOGFORMAT
Gateway config option to determine log format.NewFormatter
function to switch between JSON and Text log formats.Changes walkthrough 📝
log.go
Add log format configuration and initialization
log/log.go
NewFormatter
function to handle log format based onTYK_GW_LOGFORMAT
environment variable.NewFormatter
.log_test.go
Add unit tests for log formatter configuration
log/log_test.go
NewFormatter
function.