This repository has been archived by the owner on Feb 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Janelle Law
committed
Nov 2, 2023
1 parent
b2a111e
commit 61e4eb7
Showing
12 changed files
with
123 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
"log/slog" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
const ( | ||
numGoPluginSegments = 4 // hclog timestamp + hclog level + go-plugin name + log message | ||
logDelimiter = " " | ||
) | ||
|
||
var ( | ||
logLevelPattern = `\(` + strings.Join(levelString, "|") + `\)` | ||
pluginNamePattern = pluginGroupPrefix + `\.\w+` | ||
) | ||
|
||
// forwards plugin logs to their hosts. Retrieve these logs with the "debug" cli | ||
type remoteFileWriter struct { | ||
logForwarder *slog.Logger | ||
w io.Writer | ||
mu *sync.Mutex | ||
} | ||
|
||
func InitPluginWriter(agentId string) io.Writer { | ||
PluginFileWriter.mu.Lock() | ||
defer PluginFileWriter.mu.Unlock() | ||
|
||
if PluginFileWriter.w != nil { | ||
return PluginFileWriter.w | ||
} | ||
|
||
f := WriteOnlyFile(agentId) | ||
writer := f.(io.Writer) | ||
PluginFileWriter.w = writer | ||
PluginFileWriter.logForwarder = newPluginLogForwarder(writer).WithGroup("forwarded") | ||
return writer | ||
} | ||
|
||
func (f remoteFileWriter) Write(b []byte) (int, error) { | ||
if f.w == nil || f.logForwarder == nil { | ||
return 0, nil | ||
} | ||
|
||
// workaround to remove hclog's default DEBUG prefix on all non-hclog logs | ||
prefixes := strings.SplitN(string(b), logDelimiter, numGoPluginSegments) | ||
if len(prefixes) != numGoPluginSegments { | ||
f.logForwarder.Info(string(b)) | ||
return len(b), nil | ||
} | ||
|
||
forwardedLog := prefixes[numGoPluginSegments-1] | ||
|
||
levelPattern := regexp.MustCompile(logLevelPattern) | ||
level := levelPattern.FindString(forwardedLog) | ||
|
||
namePattern := regexp.MustCompile(pluginNamePattern) | ||
loggerName := namePattern.FindString(forwardedLog) | ||
|
||
namedLogger := f.logForwarder.WithGroup(loggerName) | ||
|
||
switch level { | ||
case levelString[0]: | ||
namedLogger.Debug(forwardedLog) | ||
case levelString[1]: | ||
namedLogger.Info(forwardedLog) | ||
case levelString[2]: | ||
namedLogger.Warn(forwardedLog) | ||
case levelString[3]: | ||
namedLogger.Error(forwardedLog) | ||
default: | ||
namedLogger.Debug(forwardedLog) | ||
} | ||
|
||
return len(b), nil | ||
} | ||
|
||
func newPluginLogForwarder(w io.Writer) *slog.Logger { | ||
dropForwardedPrefixes := func(groups []string, a slog.Attr) slog.Attr { | ||
if a.Key == slog.SourceKey { | ||
return slog.Attr{} | ||
} | ||
return a | ||
} | ||
|
||
options := &slog.HandlerOptions{ | ||
AddSource: true, | ||
Level: slog.LevelDebug, | ||
ReplaceAttr: dropForwardedPrefixes, | ||
} | ||
|
||
return slog.New(newProtoHandler(w, options)) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.