Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit 548a4ae

Browse files
author
xiekeyang
committed
Provide contexted logger instance as public package
The logger instance, exported from logrus, is retrived through context value. It helps image tools to print out arbitrarily information. Besides that, it fires the output to I/O devices like stdout and stderr. Signed-off-by: xiekeyang <[email protected]>
1 parent 061159d commit 548a4ae

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

context/context.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package context
16+
17+
import (
18+
"golang.org/x/net/context"
19+
)
20+
21+
// Context is a copy of Context from the golang.org/x/net/context package.
22+
type Context interface {
23+
context.Context
24+
}
25+
26+
// Background returns a non-nil, empty Context. The background context
27+
// provides a single key, "instance.id" that is globally unique to the
28+
// process.
29+
func Background() context.Context {
30+
return context.Background()
31+
}

context/logger.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2016 The Linux Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package context
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"reflect"
21+
22+
"github.com/Sirupsen/logrus"
23+
"golang.org/x/net/context"
24+
)
25+
26+
var (
27+
// G is an alias for GetLogger.
28+
//
29+
// We may want to define this locally to a package to get package tagged log
30+
// messages.
31+
G = GetLogger
32+
33+
// LogEntry provides a public and standard logger instance.
34+
LogEntry = logrus.NewEntry(logrus.StandardLogger())
35+
)
36+
37+
type (
38+
loggerKey struct{}
39+
40+
stderrHook struct{}
41+
)
42+
43+
// Levels provide the method for logrus.Hook interface
44+
func (h *stderrHook) Levels() []logrus.Level {
45+
return []logrus.Level{
46+
logrus.PanicLevel,
47+
logrus.FatalLevel,
48+
logrus.ErrorLevel,
49+
logrus.WarnLevel,
50+
}
51+
}
52+
53+
// Fire provide the method for logrus.Hook interface
54+
func (h *stderrHook) Fire(entry *logrus.Entry) error {
55+
log := reflect.ValueOf(*entry.Logger).Interface().(logrus.Logger)
56+
entry.Logger = &log
57+
entry.Logger.Out = os.Stderr
58+
return nil
59+
}
60+
61+
// WithLogger returns a new context with the provided logger. Use in
62+
// combination with logger.WithField(s) for great effect.
63+
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
64+
return context.WithValue(ctx, loggerKey{}, logger)
65+
}
66+
67+
// GetLogger retrieves the current logger from the context. If no logger is
68+
// available, the default logger is returned.
69+
func GetLogger(ctx context.Context) *logrus.Entry {
70+
logger := ctx.Value(loggerKey{})
71+
72+
if logger == nil {
73+
return LogEntry
74+
}
75+
76+
return logger.(*logrus.Entry)
77+
}
78+
79+
// EnableDebugMode enables a selectable debug mode.
80+
func EnableDebugMode(debug bool) error {
81+
var level string
82+
if LogEntry == nil {
83+
return fmt.Errorf("logger entry instance not found")
84+
}
85+
if debug {
86+
level = "debug"
87+
} else {
88+
level = "info"
89+
}
90+
lvl, err := logrus.ParseLevel(level)
91+
if err != nil {
92+
return err
93+
}
94+
LogEntry.Logger.Level = lvl
95+
return nil
96+
}
97+
98+
func init() {
99+
LogEntry.Logger.Out = os.Stdout
100+
LogEntry.Logger.Hooks.Add(&stderrHook{})
101+
}

0 commit comments

Comments
 (0)