Giter VIP home page Giter VIP logo

Comments (10)

vishr avatar vishr commented on August 21, 2024

@Mr-Dai Can you provide an example to debug?

from gommon.

Mr-Dai avatar Mr-Dai commented on August 21, 2024

Test file main.go:

package main

import (
    "github.com/labstack/echo"
    "github.com/labstack/gommon/log"
)

func main() {
    e := echo.New()
    e.Logger.SetLevel(log.INFO)
    e.Logger.Infof("Hello, World!")
}

Run it with go run main.go, got:

{"time":"2018-03-12T10:57:54.8390043+08:00","level":"INFO","prefix":"echo","file":"proc.go","line":"185","message":"Hello, World!"}

This is proc.go, basically it's where the go runtime invokes the main function.

from gommon.

vishr avatar vishr commented on August 21, 2024

Thanks for the information, I have reverted the changed.

from gommon.

baba2k avatar baba2k commented on August 21, 2024

@vishr I have the issuse with caller level 2, because I use the log command:

import (
   "github.com/labstack/gommon/log"
)
func main() {
   log.Info("Hello, World!")
}

With caller level 3 all is fine again. All my projects where I used the logger show the wrong file and line now

This code in log.go could fix it:

_, file, line,_ := runtime.Caller(2)
    if strings.Contains(file, "github.com/labstack/gommon/log/log.go") {
        _, file, line,_ = runtime.Caller(3)
    }

from gommon.

vishr avatar vishr commented on August 21, 2024

@alexandrestein Can you please help me with it?

from gommon.

weissra avatar weissra commented on August 21, 2024

I have the same problem in my project.

func main() {
// Echo instance
e := echo.New()

// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())

//correct line etc
e.Logger.Info("Testlog")
//wrong line etc
log.Info("Wrong log")

}

e.Logger.Info produces correct line number:
{"time":"...","level":"INFO","prefix":"echo","file":"main.go","line":"20","message":"Testlog"}

log.Info produces wrong file and line number:
{"time":"...","level":"INFO","prefix":"-","file":"log.go","line":"285","message":"Wrong log"}

baba2k`s change fixes it locally.

from gommon.

Mr-Dai avatar Mr-Dai commented on August 21, 2024

@baba2k I think the better way to fix this is to store an extraLevel field in logger struct, which defaults to 0, and change the runtime.Caller invocation in log.go to

_, file, line, _ := runtime.Caller(2 + l.extraLevel)

Another way to do this is to add a calldepth parameter to the log method, like the log.Output method from the standard library.

Point is, this should become a generic parameter/configuration, instead of just adding special processing just for invocations from github.com/labstack/gommon/log/log.go.

from gommon.

alexandrestein avatar alexandrestein commented on August 21, 2024

@vishr I made a pull request about this issue #21.

The problem is that when you use the default logger you add an other layer.
So you need to switch between level 3 and 2 if it's default logger or not.

Look at the pull request and check the test file I call the default logger directly.

I think that merging the pull request would solve the issue.

from gommon.

alexandrestein avatar alexandrestein commented on August 21, 2024

Hey @vishr.
It does fix the issue of the caller level.

But same kind of issue could be reintroduce in the future.

In the test, you are not checking that the give response display the correct file or line.
I think this must be check to prevent any future bug related to this.

from gommon.

vishr avatar vishr commented on August 21, 2024

@alexaandru thanks for pointing that out, I will put the test back soon.

from gommon.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.