Skip to content

Conversation

cfc4n
Copy link
Member

@cfc4n cfc4n commented Aug 11, 2025

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 the event package and cleaning up logging behavior in the event worker.

Event Logging Refactor and Code Organization:

  • Added a new CollectorWriter type and a NewCollectorWriter constructor to the event package, encapsulating the logic for writing logs using zerolog. This replaces the previously inline event writer implementation.
  • Updated imports in cli/cmd/root.go to use the new event package for event logging.
  • Replaced the old eventCollectorWriter implementation in cli/cmd/root.go with the new event.CollectorWriter, and removed the redundant code. [1] [2]

Event Worker and Logging Behavior:

  • Modified the event worker's Display method to check for the new CollectorWriter 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:

  • Commented out a redundant log line in probe_openssl.go to prevent duplicate or unnecessary logging.
  • Added required imports to the event package to support the new writer functionality.
  • Improved log writer initialization in runModule for clarity and maintainability. [1] [2]

…ror handling

Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
@cfc4n cfc4n requested a review from Copilot August 11, 2025 14:20
@cfc4n cfc4n added 🐞 bug Something isn't working fix bug fix PR labels Aug 11, 2025
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 11, 2025
Copy link

@Copilot Copilot AI left a 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

Comment on lines +199 to +203
_, 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 {
Copy link
Preview

Copilot AI Aug 11, 2025

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.

Suggested change
_, 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) {
Copy link
Preview

Copilot AI Aug 11, 2025

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.

Suggested change
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.

Comment on lines +59 to +65
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}
Copy link
Preview

Copilot AI Aug 11, 2025

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.

Suggested change
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.

@dosubot dosubot bot added the improve label Aug 11, 2025
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
Copy link

🔧 Debug Build Complete

📦 Download Links:

⏰ Files will be retained for 7 days, please download and test promptly.

@cfc4n cfc4n merged commit c793886 into master Aug 11, 2025
10 checks passed
@cfc4n cfc4n deleted the bugfix/event-log-split branch August 11, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working fix bug fix PR improve size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant