Add logmonitor to configuration

This commit is contained in:
Ryan Smith
2025-03-31 08:59:49 -07:00
parent c3ca87c7af
commit d3f7cb4e86

View File

@@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"github.com/r-smith/deceptifeed/internal/logmonitor"
"github.com/r-smith/deceptifeed/internal/logrotate"
)
@@ -87,6 +88,7 @@ type Config struct {
Servers []Server `xml:"honeypotServers>server"`
ThreatFeed ThreatFeed `xml:"threatFeed"`
FilePath string `xml:"-"`
Monitor *logmonitor.Monitor `xml:"-"`
}
// Server represents a honeypot server with its relevant settings.
@@ -215,6 +217,8 @@ func validateRegexRules(rules Rules) error {
// path if none is provided.
func (c *Config) InitializeLoggers() error {
const maxSize = 50
c.Monitor = logmonitor.New()
openedLogFiles := make(map[string]*slog.Logger)
for i := range c.Servers {
@@ -243,8 +247,12 @@ func (c *Config) InitializeLoggers() error {
return err
}
// Create a new logger.
logger := slog.New(slog.NewJSONHandler(file, &slog.HandlerOptions{
// Create a JSON logger with two writers: one writes to disk using file
// rotation, the other writes to a channel for live monitoring.
logger := slog.New(
slog.NewJSONHandler(
io.MultiWriter(file, c.Monitor),
&slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove 'message' and 'log level' fields from output.
if a.Key == slog.MessageKey || a.Key == slog.LevelKey {
@@ -252,7 +260,9 @@ func (c *Config) InitializeLoggers() error {
}
return a
},
}))
},
),
)
c.Servers[i].Logger = logger
c.Servers[i].LogFile = file