-
Notifications
You must be signed in to change notification settings - Fork 726
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
Structured log: replace ngaut/log with logrus #612
Structured log: replace ngaut/log with logrus #612
Conversation
97fca69
to
b99587a
Compare
server/util.go
Outdated
for i := 0; i < cnt; i++ { | ||
fu := runtime.FuncForPC(pc[i] - 1) | ||
name := fu.Name() | ||
if !strings.Contains(name, "github.com/Sirupsen/logrus") && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract the contain check to a function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about a separate variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean defining a table variable for these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for standalone sub-package, a function is ok.
for mess up with server/util.go, another function may not be acceptable.
never mind 😹
I think we should move the log to pkg Btw, please add test for it. |
"move the log to pkg logutil package" is #602 😹 For that log setting up depends on Config, there will be a circular reference between logutil and server. 😞 |
You can define a LogConfig for the log, and then embed it into ServerConfig. This can decouple the logic. |
server/util.go
Outdated
} else { | ||
b = &bytes.Buffer{} | ||
} | ||
b.WriteString(entry.Time.Format("2006/01/02 15:04:05")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
define a const var for the time format
server/util.go
Outdated
} | ||
b.WriteString(entry.Time.Format("2006/01/02 15:04:05")) | ||
if file, ok := entry.Data["file"]; ok { | ||
b.WriteString(fmt.Sprintf(" %s:%v:", file, entry.Data["line"])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seem you can use fmt.Fprintf(b, format, args...) directly to reduce the allocation cost for fmt.Sprintf?
LogConfig comes up with |
Update:
Incompatible Changes:
|
@@ -56,10 +57,8 @@ type Config struct { | |||
// Etcd onlys support seoncds TTL, so here is second too. | |||
LeaderLease int64 `toml:"lease" json:"lease"` | |||
|
|||
// Log level. | |||
LogLevel string `toml:"log-level" json:"log-level"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seem this can cause compatibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any other package references pd/server?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
users have to change their config after updating. We should avoid this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Ansible deployment log-level is specified via command line argument: https://github.com/pingcap/tidb-ansible/blob/master/roles/pd/templates/run_pd.sh.j2
Shall we add a migration support for backward compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not only for ansible, some users deploy manually. As far as I know, we have only one user who deploys manually, so we should help him changing the config before updating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default behavior is not changed. Only matters when a user changes default log level or specifies log file path in pd.toml .
conf/config.toml
Outdated
tso-save-interval = "3s" | ||
|
||
[log] | ||
log-level = "info" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need log-file here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
previously, log-file is always specified via command line arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should add it but comment here.
pkg/logutil/log.go
Outdated
// Fire implements logrus.Hook interface | ||
// https://github.com/sirupsen/logrus/issues/63 | ||
func (hook *contextHook) Fire(entry *log.Entry) error { | ||
pc := make([]uintptr, 3, 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make([]uinptr, 3)
is enough.
func (rf *redirectFormatter) Flush() {} | ||
|
||
// isSKippedPackageName tests wether path name is on log library calling stack. | ||
func isSkippedPackageName(name string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a simple test for this function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
meaningless to add tests for such stupid library function calls.
Or else I'll make it a variable before if condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw: coverage already covers this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we only cover "github.com/Sirupsen/logrus" but miss "github.com/coreos/pkg/capnslog", how do we know it, at run time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log_test.go checks against "log.go"(all capnslog) and "log_test.go".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is nonintuitive. Please test it explicitly.
Rest LGTM |
…pd into structured-log-stage1-use-logrus
conf/config.toml
Outdated
@@ -18,7 +18,8 @@ lease = 3 | |||
tso-save-interval = "3s" | |||
|
|||
[log] | |||
log-level = "info" | |||
level = "info" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to update ansible now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave it what it is now
PTAL @disksing |
pkg/logutil/log_test.go
Outdated
entry, err = s.buf.ReadString('\n') | ||
c.Assert(err, IsNil) | ||
c.Assert(entry, Matches, logPattern) | ||
c.Assert(strings.Contains(entry, thisFilename), IsTrue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use "log_test.go" here directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK!
pkg/logutil/log.go
Outdated
case "warn": | ||
return log.WarnLevel | ||
case "warning": | ||
return log.WarnLevel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case "warn", "warning":
pkg/logutil/log.go
Outdated
// trim square brackets | ||
if len(logStr) > 2 && logStr[0] == '[' { | ||
logStr = logStr[1 : len(logStr)-1] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When will it starts with '['? And the last char will always be ']' if first is '['?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All logs from etcd are wrapped in []
pairs. and yes.
pkg/logutil/log.go
Outdated
case capnslog.DEBUG: | ||
log.Debugf(logStr) | ||
case capnslog.TRACE: | ||
log.Debugf(logStr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case sapnslog.DEBUG, sapnslog.TRACE:
pkg/logutil/log.go
Outdated
if k != "file" && k != "line" { | ||
fmt.Fprintf(b, " %v=%v", k, entry.Data[k]) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about for k, v := range entry.Data
?
+Update: add backward compatibility check + warning |
server/config.go
Outdated
@@ -175,6 +182,19 @@ func (c *Config) Parse(arguments []string) error { | |||
if err != nil { | |||
return errors.Trace(err) | |||
} | |||
|
|||
// Backward compatibility for toml config | |||
fmt.Printf("fuck %+v\n", c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for test .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆 fixed
server/config.go
Outdated
if c.LogFileDeprecated != "" && c.Log.Filename == "" { | ||
c.Log.Filename = c.LogFileDeprecated | ||
msg := fmt.Sprintf("log-file in %s is deprecated, use [log] instead", c.configFile) | ||
c.WarningMsgs = append(c.WarningMsgs, msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
output the warning message here directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignore it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be logged after logger is initialized. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about returning warning messages as []string
and log them afterwards?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returning the warning message seems better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current implementation is OK for future planning.
config.Parse()
, logutil.InitLogger()
may all update warning messages.
return log.AllLevels | ||
} | ||
|
||
func stringToLogLevel(level string) log.Level { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add test to cover all the conditions.
@@ -85,6 +88,9 @@ type Config struct { | |||
electionMs uint64 | |||
|
|||
configFile string | |||
|
|||
// For all warnings during parsing. | |||
WarningMsgs []string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/WarningMsgs/warningMsgs/s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public field here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, but can we use a function to do this? exporting fields seems strange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetWarningMsgs() 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LogWarningMsgs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugly and export a meaningless function with side effect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting WarningMsg is too, what is the WarningMsgs for? what I first see is that there is a WarningMsg config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
........
pkg/logutil/log_test.go
Outdated
@@ -41,6 +41,16 @@ func (s *testLogSuite) SetUpSuite(c *C) { | |||
s.buf = &bytes.Buffer{} | |||
} | |||
|
|||
func (s *testLogSuite) TestStringToLogLevel(c *C) { | |||
c.Assert(stringToLogLevel("fatal"), Equals, log.FatalLevel) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use table-driven test here.
let tt = []struct {
name string
level Level
} {
{"ERROR", log.ErrorLevel},
...
}
for _, t := range tt {
c.Assert()
}
LGTM |
LGTM PTAL @disksing |
Should we also do this in tidb repo? |
LGTM. |
I'll fix circleCI status. |
Changes:
review link: https://github.com/pingcap/pd/pull/612/files/18ba74fb8628ad0c7f1b75b70af8724bc3ba8840..b99587ae691568efde81b68572e688bac8827fa5 (without vendor changes)