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

🚀 Add Logger interface and fiberlog #2499

Merged
merged 10 commits into from
Jun 26, 2023
Merged

Conversation

Skyenought
Copy link
Member

@Skyenought Skyenought commented Jun 8, 2023

Description

Adding an interface for extending the log library

// AllLogger is the combination of Logger, FormatLogger, CtxLogger and ControlLogger.
// Custom extensions can be made through AllLogger
type AllLogger interface {
	Logger
	FormatLogger
	CtxLogger
	ControlLogger
        WithLogger
}

Pre-set log levels in advance

// Logger is a logger interface that provides logging function with levels.
type Logger interface {
	Trace(v ...interface{})
	Debug(v ...interface{})
	Info(v ...interface{})
	Warn(v ...interface{})
	Error(v ...interface{})
	Fatal(v ...interface{})
	Panic(v ...interface{})
}

Provides a way to print logs, with default standard output on the console

fiberlog.Info("Hello World!")
fiberlog.Trace("Hello World!")
fiberlog.Debug("Hello World!")
fiberlog.Warn("Hello World!")
fiberlog.Error("Hello World!")
fiberlog.Infow("", "key", "value")
2023/06/08 15:16:35.692976 main.go:12: [Info] Hello World!
2023/06/08 15:16:35.694025 main.go:13: [Trace] Hello World!
2023/06/08 15:16:35.694034 main.go:14: [Debug] Hello World!
2023/06/08 15:16:35.694043 main.go:16: [Warn] Hello World!
2023/06/08 15:16:35.694047 main.go:17: [Error] Hello World!
2023/06/08 15:16:35.694047 main.go:17: [Info] key=value

Example

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
	
	"os"
)

func main() {
	app := fiber.New()
	// set the level of logs below which logs will not be output.
	// Levels:
	// 0 - Trace, 1 - Debug, 2 - Info,  3 - Warn, 4 - Error, 5 - Fatal, 6 - Panic
	log.SetLevel(fiberlog.LevelNotice)
        // If user customs a logger
	log.SetLogger(customLogger)
	f, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	// set the output destination for the logger.
	fiberlog.SetOutput(f)
	
	app.Get("/hello/world", func(c *fiber.Ctx) error {
		fiberlog.Info("Hello World!")
		return c.SendString("Hello World 👋!")
	})
	app.Get("/:option/world", func(c *fiber.Ctx) error {
		fiberlog.Error(c.Params("option") + " World!")
		return c.SendString(c.Params("option") + " World 👋!")
	})
	log.Fatal(app.Listen(":3000"))
}

Fixes # (issue)
#2433

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

@Skyenought Skyenought force-pushed the feat/log branch 8 times, most recently from e0eaa3b to 044bf4d Compare June 9, 2023 01:35
@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought Can you check the issues in the CI

@Skyenought
Copy link
Member Author

i don't know what's happening, How did ci go wrong when I just added extra code?
The error is not what I changed.

@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought Let me check... It's related to go 1.20.5 so it's not this PR.

@Skyenought
Copy link
Member Author

@gaby thx

@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought The setup-go action hasnt updated their manifest to support 1.20.5, thus why it fails

https://github.com/actions/go-versions/blob/main/versions-manifest.json

Rel actions/go-versions#75

@Skyenought
Copy link
Member Author

trace debug info warn error fatal panic Is this enough log level in total?

@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought Can you merge main into your branch.

@Skyenought
Copy link
Member Author

Unfortunately there still seems to be a problem with the ci settings 🤔

If fiberlog is written without problems, I will try to replace all the places in fiber that use log with fiberlog

@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought Try again, the Pr was reverted. That part should pass now. Just merge main into your branch

@Skyenought
Copy link
Member Author

Skyenought commented Jun 9, 2023

@Skyenought Try again, the Pr was reverted. That part should pass now. Just merge main into your branch

QQ截图20230609202607

I've updated the branch, but it doesn't seem to work. @gaby

@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought Try again, the Pr was reverted. That part should pass now. Just merge main into your branch

QQ截图20230609202607

I've updated the branch, but it doesn't seem to work. @gaby

Those are not the latest changes, pull again.

@gaby
Copy link
Member

gaby commented Jun 9, 2023

You are missing the last commit from here: https://github.com/gofiber/fiber/commits/master

@Skyenought
Copy link
Member Author

Skyenought commented Jun 9, 2023

sorry! @gaby

@gaby
Copy link
Member

gaby commented Jun 9, 2023

@Skyenought Have you tried running the tests locally with -race ? There's a race condition

@Skyenought
Copy link
Member Author

Skyenought commented Jun 9, 2023

 cd fiberlog/
❯ go test ./... -v -race
=== RUN   Test_DefaultLogger              
--- PASS: Test_DefaultLogger (0.00s)      
=== RUN   Test_DefaultFormatLogger        
--- PASS: Test_DefaultFormatLogger (0.00s)
=== RUN   Test_CtxLogger                  
--- PASS: Test_CtxLogger (0.00s)          
=== RUN   Test_SetLevel
--- PASS: Test_SetLevel (0.00s)
=== RUN   Test_DefaultSystemLogger
--- PASS: Test_DefaultSystemLogger (0.00s)
=== RUN   Test_SetLogger
--- PASS: Test_SetLogger (0.00s)
PASS
ok      github.com/gofiber/fiber/v2/fiberlog    (cached)

The code that appears as DATA RACE is not the code I wrote. @gaby

@gaby
Copy link
Member

gaby commented Jun 9, 2023

 cd fiberlog/
❯ go test ./... -v -race
=== RUN   Test_DefaultLogger              
--- PASS: Test_DefaultLogger (0.00s)      
=== RUN   Test_DefaultFormatLogger        
--- PASS: Test_DefaultFormatLogger (0.00s)
=== RUN   Test_CtxLogger                  
--- PASS: Test_CtxLogger (0.00s)          
=== RUN   Test_SetLevel
--- PASS: Test_SetLevel (0.00s)
=== RUN   Test_DefaultSystemLogger
--- PASS: Test_DefaultSystemLogger (0.00s)
=== RUN   Test_SetLogger
--- PASS: Test_SetLogger (0.00s)
PASS
ok      github.com/gofiber/fiber/v2/fiberlog    (cached)

The code that appears as DATA RACE is not the code I wrote. @gaby

Correct but app.go now uses fiberlog and that affects other parts of the code. The code in that folder is fine test wise. Now you have to test/fix the rest of the library since several parts import app.go

@Skyenought
Copy link
Member Author

GET

@Skyenought
Copy link
Member Author

Is this related to Fiber using an older golangci-lint version?

I've triggered too many errchecks (I've solved golangci-lint, it's too strict), and now I'm thinking why the benchmark didn't pass😥

@gaby
Copy link
Member

gaby commented Jun 18, 2023

Is this related to Fiber using an older golangci-lint version?

I've triggered too many errchecks (I've solved golangci-lint, it's too strict), and now I'm thinking why the benchmark didn't pass😥

The benchmark thing is a known issue, since the runners can be unpredictible sometimes.

Not checking errors is a real issue though

@Skyenought
Copy link
Member Author

ok,I see that those errors can be ignored, otherwise I wouldn't have ruled them out

@Skyenought
Copy link
Member Author

After this PR merge, I will make the zap and zerolog changes available as soon as possible, and add examples to recipes

@ReneWerner87
Copy link
Member

After this PR merge, I will make the zap and zerolog changes available as soon as possible, and add examples to recipes

What do you mean with "zap and zero log changes"?

Btw can you post the benchmark numbers here, for the history

@Skyenought
Copy link
Member Author

After this PR merge, I will make the zap and zerolog changes available as soon as possible, and add examples to recipes

What do you mean with "zap and zero log changes"?

Btw can you post the benchmark numbers here, for the history

I mean it will be in fiberzap and fiberzerolog to add support for log packages

@ReneWerner87
Copy link
Member

Ok, so a wrapper for the interface

Don't forget the benchmark numbers

@Skyenought
Copy link
Member Author

Ok, so a wrapper for the interface

Don't forget the benchmark numbers

It looks like the performance of fiber/log is similar to that of the native log package.

// BenchmarkDefaultLoggerInfo-2       88129             26447 ns/op
func BenchmarkDefaultLoggerInfo(b *testing.B) {
	for n := 0; n < b.N; n++ {
		DefaultLogger().Info("test")
	}
}

// BenchmarkLogPrintln-2              62989             26672 ns/op
func BenchmarkLogPrintln(b *testing.B) {
	l := log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)
	for n := 0; n < b.N; n++ {
		l.Println("[Info] test")
	}
}

// BenchmarkDefaultLoggerInfof-2              56754             26381 ns/op
func BenchmarkDefaultLoggerInfof(b *testing.B) {
	for n := 0; n < b.N; n++ {
		DefaultLogger().Infof("test: %s", "log")
	}
}

// BenchmarkLogPrintf-2       49744             27731 ns/op
func BenchmarkLogPrintf(b *testing.B) {
	l := log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)
	for n := 0; n < b.N; n++ {
		l.Printf("[Info] test %s", "log")
	}
}

@ReneWerner87
Copy link
Member

Can you add the info about the allocations

@Skyenought
Copy link
Member Author

Skyenought commented Jun 20, 2023

goos: linux                                   
goarch: amd64                                 
pkg: github.com/gofiber/fiber/v2/log          
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
BenchmarkDefaultLoggerInfo-2              625828              1716 ns/op             561 B/op          4 allocs/op
BenchmarkDefaultLoggerInfof-2             668976              1929 ns/op             608 B/op          3 allocs/op
BenchmarkLogPrintln-2                     944955              1330 ns/op             564 B/op          3 allocs/op
BenchmarkLogPrintf-2                      846170              1303 ns/op             603 B/op          3 allocs/op

@gaby
Copy link
Member

gaby commented Jun 20, 2023

@Skyenought Can it be compared to this fiberzerolog? https://github.com/gofiber/contrib/tree/main/fiberzerolog

@Skyenought
Copy link
Member Author

@Skyenought Can it be compared to this fiberzerolog? https://github.com/gofiber/contrib/tree/main/fiberzerolog

The defaultLogger is only used to build in a logging specification, I think it's just as good as the native log, the zerolog adaptation is not yet complete, I can provide the fiberzap

// 1581740              1119 ns/op               2 B/op          0 allocs/op
func BenchmarkNativeZap(b *testing.B) {
	log, _ := zap.NewProduction()
	for i := 0; i < b.N; i++ {
		log.Info("Hello, World!")
	}
}

// 1244044              1406 ns/op              18 B/op          1 allocs/op
func BenchmarkFiberZap(b *testing.B) {
	log, _ := zap.NewProduction()
	logger := NewLogger(LoggerConfig{
		SetLogger: log,
	})
	fiberlog.SetLogger(logger)
	for i := 0; i < b.N; i++ {
		fiberlog.Info("Hello, World!")
	}
}

@gaby
Copy link
Member

gaby commented Jun 22, 2023

@Skyenought Can it be compared to this fiberzerolog? https://github.com/gofiber/contrib/tree/main/fiberzerolog

The defaultLogger is only used to build in a logging specification, I think it's just as good as the native log, the zerolog adaptation is not yet complete, I can provide the fiberzap

// 1581740              1119 ns/op               2 B/op          0 allocs/op
func BenchmarkNativeZap(b *testing.B) {
	log, _ := zap.NewProduction()
	for i := 0; i < b.N; i++ {
		log.Info("Hello, World!")
	}
}

// 1244044              1406 ns/op              18 B/op          1 allocs/op
func BenchmarkFiberZap(b *testing.B) {
	log, _ := zap.NewProduction()
	logger := NewLogger(LoggerConfig{
		SetLogger: log,
	})
	fiberlog.SetLogger(logger)
	for i := 0; i < b.N; i++ {
		fiberlog.Info("Hello, World!")
	}
}

@Skyenought What is missing to support Zerolog?

@Skyenought
Copy link
Member Author

i am busy now,i think adapter for zerolog need to wait 1 week

@gaby
Copy link
Member

gaby commented Jun 23, 2023

i am busy now,i think adapter for zerolog need to wait 1 week

No worries, thanks! 💪

@ReneWerner87 ReneWerner87 merged commit fefc533 into gofiber:master Jun 26, 2023
17 checks passed
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.

None yet

4 participants