-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: refactor event logging to use new CollectorWriter and improve error handling #821
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
Conversation
…ror handling Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
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.
Pull Request Overview
This PR refactors event logging by introducing a reusable CollectorWriter
type in the event package, improving code organization and error handling. The changes consolidate event logging logic and remove duplicate code while maintaining the same functionality.
- Introduces a new
CollectorWriter
type with constructor in the event package to encapsulate zerolog-based event writing - Removes redundant inline event writer implementation from CLI root command
- Updates event worker to handle the new
CollectorWriter
type with conditional logging behavior
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
user/event/ievent.go | Adds new CollectorWriter type and constructor with zerolog integration |
cli/cmd/root.go | Removes old eventCollectorWriter type and uses new event.CollectorWriter |
pkg/event_processor/iworker.go | Updates Display method to handle new CollectorWriter type conditionally |
user/module/probe_openssl.go | Comments out redundant logging statement |
_, ok := ew.processor.logger.(event.CollectorWriter) | ||
if ok { | ||
// 直接写入日志 | ||
err = ew.writeToChan(fmt.Sprintf("PID:%d, Comm:%s, Src:%s:%d, Dest:%s:%d,\n%s", eb.PID, eb.PName, eb.SrcIP, eb.SrcPort, eb.DstIP, eb.DstPort, b)) | ||
} else { |
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.
Type assertion is checking for the concrete type event.CollectorWriter
but the logger field likely holds a pointer or interface. This assertion will always fail if logger
is of type *event.CollectorWriter
or an interface. Consider checking for the correct type or using a type switch.
_, ok := ew.processor.logger.(event.CollectorWriter) | |
if ok { | |
// 直接写入日志 | |
err = ew.writeToChan(fmt.Sprintf("PID:%d, Comm:%s, Src:%s:%d, Dest:%s:%d,\n%s", eb.PID, eb.PName, eb.SrcIP, eb.SrcPort, eb.DstIP, eb.DstPort, b)) | |
} else { | |
switch ew.processor.logger.(type) { | |
case event.CollectorWriter, *event.CollectorWriter: | |
// 直接写入日志 | |
err = ew.writeToChan(fmt.Sprintf("PID:%d, Comm:%s, Src:%s:%d, Dest:%s:%d,\n%s", eb.PID, eb.PName, eb.SrcIP, eb.SrcPort, eb.DstIP, eb.DstPort, b)) | |
default: |
Copilot uses AI. Check for mistakes.
logger *zerolog.Logger | ||
} | ||
|
||
func (e CollectorWriter) Write(p []byte) (n int, err error) { |
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.
The Write
method should use a pointer receiver (e *CollectorWriter)
instead of a value receiver to be consistent with the io.Writer
interface implementation and avoid unnecessary copying of the struct.
func (e CollectorWriter) Write(p []byte) (n int, err error) { | |
func (e *CollectorWriter) Write(p []byte) (n int, err error) { |
Copilot uses AI. Check for mistakes.
func NewCollectorWriter(logger *zerolog.Logger) CollectorWriter { | ||
if logger == nil { | ||
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} | ||
lg := zerolog.New(consoleWriter).With().Timestamp().Logger() | ||
logger = &lg | ||
} | ||
return CollectorWriter{logger: logger} |
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.
The constructor should return a pointer *CollectorWriter
instead of a value to be consistent with the receiver type recommendation and to avoid unnecessary struct copying.
func NewCollectorWriter(logger *zerolog.Logger) CollectorWriter { | |
if logger == nil { | |
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} | |
lg := zerolog.New(consoleWriter).With().Timestamp().Logger() | |
logger = &lg | |
} | |
return CollectorWriter{logger: logger} | |
func NewCollectorWriter(logger *zerolog.Logger) *CollectorWriter { | |
if logger == nil { | |
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} | |
lg := zerolog.New(consoleWriter).With().Timestamp().Logger() | |
logger = &lg | |
} | |
return &CollectorWriter{logger: logger} |
Copilot uses AI. Check for mistakes.
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
🔧 Debug Build Complete 📦 Download Links: ⏰ Files will be retained for 7 days, please download and test promptly. |
This pull request refactors how event logging is handled by introducing a reusable
CollectorWriter
type for event logging, removes redundant code, and updates the event processing logic to use the new writer. It also improves code organization by moving event writer logic into theevent
package and cleaning up logging behavior in the event worker.Event Logging Refactor and Code Organization:
CollectorWriter
type and aNewCollectorWriter
constructor to theevent
package, encapsulating the logic for writing logs usingzerolog
. This replaces the previously inline event writer implementation.cli/cmd/root.go
to use the newevent
package for event logging.eventCollectorWriter
implementation incli/cmd/root.go
with the newevent.CollectorWriter
, and removed the redundant code. [1] [2]Event Worker and Logging Behavior:
Display
method to check for the newCollectorWriter
type and write log output directly if present, otherwise encoding and writing the payload as before. This clarifies the responsibility of the worker and avoids unnecessary output.Minor Improvements and Cleanups:
probe_openssl.go
to prevent duplicate or unnecessary logging.event
package to support the new writer functionality.runModule
for clarity and maintainability. [1] [2]