log icon indicating copy to clipboard operation
log copied to clipboard

Question: Not printing line number on error

Open paritosh-gupta opened this issue 6 years ago • 1 comments

Hi I ran the tracing sample but it was not printing the line number. This is my current output.

INFO[0001] opening                   app=myapp env=prod path=Readme.md
 ERROR[0001] opening                   app=myapp duration=22.987µs env=prod error=open Readme.md: no such file or directory path=Readme.md
  INFO[0002] opening                   app=myapp env=prod path=Readme.md
 ERROR[0002] opening                   app=myapp duration=8.866µs env=prod error=open Readme.md: no such file or directory path=Readme.md

My code is (taken from the tracing examlple)

package main

import (
	"os"
	"time"

	"github.com/apex/log"
	"github.com/apex/log/handlers/text"
)

func work(ctx log.Interface) (err error) {
	path := "Readme.md"
	defer ctx.WithField("path", path).Trace("opening").Stop(&err)
	_, err = os.Open(path)
	return
}

func main() {
	log.SetHandler(text.New(os.Stderr))

	ctx := log.WithFields(log.Fields{
		"app": "myapp",
		"env": "prod",
	})

	for range time.Tick(time.Second) {
		_ = work(ctx)
	}
}

go version go1.8.3 darwin/amd64

paritosh-gupta avatar Sep 13 '17 21:09 paritosh-gupta

I have the same question about printing filename and line number. I tried stack sample to show filename and line number and it does not work. Here is my sample code:

package main

import (
        "time"

	"github.com/apex/log"
	"github.com/apex/log/handlers/cli"
	"github.com/pkg/errors"
)

func main() {
	log.SetHandler(cli.Default)

	ctx := log.WithField("filename", "")

	for range time.Tick(time.Millisecond * 200) {
		for range time.Tick(time.Millisecond * 200) {
			log.Info("upload")
			log.Info("upload complete")
			log.Warn("upload retry")
			ctx.WithError(errors.New("unauthorized")).Error("Why no line number?")
		}
	}
}

I saw this PR but seems it does not work: https://github.com/apex/log/commit/89ce6f11b2dc2a7603f3429d7b3076c526980841

halink0803 avatar Jul 25 '18 04:07 halink0803