Skip to content
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

✨ support logging color for custom logging formats #1513

Merged
merged 1 commit into from Sep 28, 2021
Merged

✨ support logging color for custom logging formats #1513

merged 1 commit into from Sep 28, 2021

Conversation

amalshaji
Copy link
Contributor

Support logger coloring for custom logging formats

Add a function to check for a valid custom format. A valid format is one having both ${status} and ${method}. Fixed #1467

@ReneWerner87
Copy link
Member

can you please provide unittests that test the new code in use and make sure what we expect happens

@amalshaji
Copy link
Contributor Author

amalshaji commented Sep 3, 2021

How to test for colors in output? Or test the newly added function?

@ReneWerner87
Copy link
Member

Use a buffer as weitter in the configuration and check the generated output.

@ReneWerner87
Copy link
Member

@amalshaji any progress ?
you could test the feature in the test and define a buffer as the writer in the config and then check the created output
-> color output has a special prefix and suffix

@amalshaji
Copy link
Contributor Author

working on it now

@amalshaji
Copy link
Contributor Author

if validCustomFormat(cfg.Format) && cfg.Output == nil {
    cfg.enableColors = true
}

If I use buf as Output, the colors won't be enabled!

@ReneWerner87
Copy link
Member

ok then use my function to capture the output
https://github.com/gofiber/fiber/blob/master/app_test.go#L1263-L1291

@ReneWerner87
Copy link
Member

@amalshaji can you please create the tests

@amalshaji
Copy link
Contributor Author

I'm using your function to capture the output. The following code is used for testing the output:

func Test_Logger_Custom_Format_Color(t *testing.T) {
	output := captureOutput(func() {
		app := fiber.New()
		app.Use(New(Config{
			Format: "",
		}))
		app.Test(httptest.NewRequest("GET", "/test", nil))
	})
	fmt.Println("captured Output: ", output)
	utils.AssertEqual(t, 5, 5)
}

When the format is "", I get:

❯ go test -timeout 30s -run ^Test_Logger_Custom_Format_Color
captured Output:  18:32:03 | 404 |      0s |         0.0.0.0 | GET     | /test

PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   0.004s

When I use a custom template like ${status}, I get:

❯ go test -timeout 30s -run ^Test_Logger_Custom_Format_Color
404captured Output:
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   0.005s

No output is being captured. Note the 404 printed to stdout.

So I decided to disable the validCustomTemplate function and use the previous check, i.e., if cfg.Format == "" && ...

Got the same output as the empty format one. Unfortunately, the output does not capture any color tags. Also, no color tests were done previously, so I can't refer.

@amalshaji
Copy link
Contributor Author

go run main.go prints color to output. But fiber dev doesn't. Is this related to my problem?

image

@ReneWerner87
Copy link
Member

Yes should be, you can open a issue in https://github.com/gofiber/cli

@ReneWerner87
Copy link
Member

@amalshaji that means we do not need the pull request ? the normally come the colors yes or ?

@amalshaji
Copy link
Contributor Author

Only when the format is "" (Empty). The issue was to support colors for custom format. I can't figure out how to test for colors.

@ReneWerner87 ReneWerner87 merged commit afa53ae into gofiber:master Sep 28, 2021
@ReneWerner87
Copy link
Member

@amalshaji thx for the contribution and effort

have unfortunately also found no way

from the code it looks good

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.

🚀 I want to use color in the custom logger middleware
2 participants