Deprecated: Function get_magic_quotes_gpc() is deprecated in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 99

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 619

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1169

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176
8000 Add caller offset to fix caller's problem #63. by bobcyw · Pull Request #530 · sirupsen/logrus · GitHub
Nothing Special   »   [go: up one dir, main page]

Skip to content

Conversation

bobcyw
Copy link
@bobcyw bobcyw commented Apr 23, 2017

After read the zap, I think add caller offset to entry is a good idea to solve #63.

Then we can use a hook like this

import (
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
	
	"github.com/bobcyw/logrus"
)

type CallerHook struct {
}

func (hook *CallerHook) Fire(entry *logrus.Entry) error {
	entry.Data["caller"] = hook.caller(entry)
	return nil
}

func (hook *CallerHook) Levels() []logrus.Level {
	return []logrus.Level{
		logrus.PanicLevel,
		logrus.FatalLevel,
		logrus.ErrorLevel,
		logrus.WarnLevel,
		logrus.InfoLevel,
		logrus.DebugLevel,
	}
}

func (hook *CallerHook) caller(entry *logrus.Entry) string {
	if _, file, line, ok := runtime.Caller(entry.CallerOffset); ok {
		return strings.Join([]string{filepath.Base(file), strconv.Itoa(line)}, ":")
	}
	// not sure what the convention should be here
	return ""
}

@bobcyw bobcyw changed the title add caller offset to fix caller's problem. add caller offset to fix caller's problem #63. Apr 23, 2017
@bobcyw bobcyw changed the title add caller offset to fix caller's problem #63. Add caller offset to fix caller's problem #63. Apr 23, 2017
@sirupsen
Copy link
Owner
sirupsen commented May 4, 2017

I like this idea a lot more than adding caller information to every entry, which is going to be too much data for those not using this feature. This is the only way to get the correct level in the hook? It cannot be hard-coded?

@bobcyw
Copy link
Author
bobcyw commented May 4, 2017
8000

I think there is some other trick to solve this problem. Such like keep all log function with same stack depth. Now when we invoke Println, the calling path is Println->Infoln->Info->log(stack depth is 3), but when we invoke Infoln, it's caller path is Infoln->Info->log(stack depth is 2). It always change. If we could keep their stack depth a fix number, we can solve this problem without CallerOffset.

@sirupsen
Copy link
Owner
sirupsen commented May 5, 2017

If you update this PR to just go directly to Log in all functions, perhaps we don't need to know anything about callers (since the stack depth will be the same) to implement a hook that adds the callee?

@bobcyw
Copy link
Author
bobcyw commented May 5, 2017

It's right. I'll update it this weekend.

@bobcyw
Copy link
Author
bobcyw commented May 5, 2017

I have complete it. But I find another problem some framework insert their own function object between the real caller and entry.log. Look at below stack info:
callernormal
This is a normal scenario.
callerwithcustomlayer
This is a special scenario. I found such like socket-io will insert their own layer.

So the caller hook should be well defined for special scenario.
Second picture is error. It occurs in reflection scenario. There is no relationship with this topic.

fun callIt(log *Logger){
	Invoke(log, "Println", "Hello")
}
func Invoke(any interface{}, name string, args ...interface{}) {
	inputs := make([]reflect.Value, len(args))
	for i, _ := range args {
		inputs[i] = reflect.ValueOf(args[i])
	}
	reflect.ValueOf(any).MethodByName(name
8000
).Call(inputs)
}

@sirupsen
Copy link
Owner

Sorry, not sure I understand. How does that happen, and what's the solution?

@bobcyw
Copy link
Author
bobcyw commented May 16, 2017

I think I find out the reason. If we use Logger instance, the call stack is
Logger.Println -> Entry.Println -> Entry.log. If we use Entry instance which is created by Logger.WithField, the call stack is Entry.Println -> Entry.log. So I update my PR to solve this problem. Use Logger.Println -> Entry.log instead of Logger.Println -> Entry.Println -> Entry.log.

@bobcyw
Copy link
Author
bobcyw commented May 16, 2017

I write a hook like this. Magic number 5 work well both with Logger and Entry.

import (
	"path/filepath"
	"runtime"
	"strconv"
	"strings"

	"github.com/bobcyw/logrus"
)

func NewNormalCaller() logrus.Hook {
	return &callerHook{}
}

type callerHook struct {
}

func (hook *callerHook) Fire(entry *logrus.Entry) error {
	entry.Data["caller"] = hook.caller(entry)
	return nil
}

func (hook *callerHook) Levels() []logrus.Level {
	return []logrus.Level{
		logrus.PanicLevel,
		logrus.FatalLevel,
		logrus.ErrorLevel,
		logrus.WarnLevel,
		logrus.InfoLevel,
		logrus.DebugLevel,
	}
}

func (hook *callerHook) caller(entry *logrus.Entry) string {
	if _, file, line, ok := runtime.Caller(5); ok {
		return strings.Join([]string{filepath.Base(file), strconv.Itoa(line)}, ":")
	} else {
		return ""
	}
}

@bobcyw
Copy link
Author
bobcyw commented Jun 14, 2017

Hi, @sirupsen , I have rebased this commit on the newest master branch. And solved the unstable caller problem. The solution is modifying not only entry's function but also public log's function.

@stale stale bot added the stale label Feb 26, 2020
@markphelps markphelps removed the stale label Feb 26, 2020
Repository owner deleted a comment from stale bot Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0