-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg: when run in Envoy, log with Envoy's logger (#68)
Signed-off-by: spacewander <[email protected]>
- Loading branch information
1 parent
8f6a438
commit 95ce187
Showing
6 changed files
with
89 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//go:build so | ||
|
||
package log | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
"github.com/go-logr/logr/funcr" | ||
|
||
"mosn.io/moe/pkg/filtermanager/api" | ||
) | ||
|
||
func init() { | ||
// Name of this file guarantees that SetLogger runs after DefaultLogger init. | ||
SetLogger(DefaultLogger.WithSink(&EnvoyLogSink{ | ||
Formatter: funcr.NewFormatter(funcr.Options{}), | ||
})) | ||
} | ||
|
||
type EnvoyLogSink struct { | ||
funcr.Formatter | ||
} | ||
|
||
func (l *EnvoyLogSink) Init(info logr.RuntimeInfo) { | ||
} | ||
|
||
func (l *EnvoyLogSink) Enabled(level int) bool { | ||
// We don't use V-level log | ||
return true | ||
} | ||
|
||
func (l *EnvoyLogSink) Info(level int, msg string, keysAndValues ...any) { | ||
prefix, s := l.Formatter.FormatInfo(level, msg, keysAndValues) | ||
if prefix != "" { | ||
api.LogInfof("[%s] %s", prefix, s) | ||
} else { | ||
api.LogInfo(s) | ||
} | ||
} | ||
|
||
func (l *EnvoyLogSink) Error(err error, msg string, keysAndValues ...any) { | ||
prefix, s := l.Formatter.FormatError(err, msg, keysAndValues) | ||
if prefix != "" { | ||
api.LogErrorf("[%s] %s", prefix, s) | ||
} else { | ||
api.LogError(s) | ||
} | ||
} | ||
|
||
func (l *EnvoyLogSink) WithValues(keysAndValues ...any) logr.LogSink { | ||
nl := &EnvoyLogSink{ | ||
Formatter: l.Formatter, // copy of Formatter | ||
} | ||
nl.Formatter.AddValues(keysAndValues) | ||
return nl | ||
} | ||
|
||
func (l *EnvoyLogSink) WithName(name string) logr.LogSink { | ||
nl := &EnvoyLogSink{ | ||
Formatter: l.Formatter, | ||
} | ||
nl.Formatter.AddName(name) | ||
return nl | ||
} |