-
Notifications
You must be signed in to change notification settings - Fork 82
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
verbose output #46
base: master
Are you sure you want to change the base?
verbose output #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2016 The Linux Foundation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logger | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Context is a copy of Context from the stdlib context package. | ||
type Context interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not define a new context type. |
||
context.Context | ||
} | ||
|
||
// Background returns a non-nil, empty Context. The background context | ||
// provides a single key, "instance.id" that is globally unique to the | ||
// process. | ||
func Background() context.Context { | ||
return context.Background() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2016 The Linux Foundation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an example of a context-integrated logger that is threadsafe: https://github.com/docker/containerd/blob/master/log/context.go#L28. However, please let me know what the intent of this package and we can get it into shape. |
||
|
||
import ( | ||
// TODO(xiekeyang): The adaptation should be checked for openSUSE | ||
// on non-x86_64 architectures. If encounting problem, the golang | ||
// version should be updated accordingly. | ||
"context" | ||
"os" | ||
|
||
"github.com/Sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
// G is an alias for GetLogger. | ||
// | ||
// We may want to define this locally to a package to get package tagged log | ||
// messages. | ||
G = GetLogger | ||
|
||
// LogEntry provides a public and standard logger instance. | ||
LogEntry = logrus.NewEntry(logrus.StandardLogger()) | ||
) | ||
|
||
type ( | ||
loggerKey struct{} | ||
) | ||
|
||
// WithLogger returns a new context with the provided logger. Use in | ||
// combination with logger.WithField(s) for great effect. | ||
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context { | ||
return context.WithValue(ctx, loggerKey{}, logger) | ||
} | ||
|
||
// GetLogger retrieves the current logger from the context. If no logger is | ||
// available, the default logger is returned. | ||
func GetLogger(ctx context.Context) *logrus.Entry { | ||
logger := ctx.Value(loggerKey{}) | ||
|
||
if logger == nil { | ||
return LogEntry | ||
} | ||
|
||
return logger.(*logrus.Entry) | ||
} | ||
|
||
// EnableDebugMode enables a selectable debug mode. | ||
func EnableDebugMode(debug bool) { | ||
if debug { | ||
LogEntry.Logger.Level = logrus.DebugLevel | ||
} | ||
} | ||
|
||
func init() { | ||
LogEntry.Logger.Out = os.Stderr | ||
} |
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.
This doesn't make sense. This should just use a regular context. Don't define a new
Context
type.Please follow the pattern in
containerd
. It is well-healed and does exactly what is needed here.