Is this the proper way to implement a multi-output logger?

Hey guys,

I needed to write my own lightweight GO multi-output logger for a work project and I’ve come up with https://github.com/asticode/go-logger.

Basically the projects works like this:

// Create the logger
l, e := logger.NewLogger("myproject", logger.LevelDebug, "%message%")

// Add a stream handler
h, e := logger.NewHandlerStream("")
l.AddHandler("stream", h)

// Add a syslog handler
h, e = logger.NewHandlerSyslog("[%level%]%message%", "syslog_id")
l.AddHandler("syslog", h)

// Add a file handler
h, e = logger.NewHandlerFile("", "/path/to/file")
l.AddHandler("file", h)

// Log a warning message
l.Warning("This is a warning message")

And this will output the message to the specified handlers :slight_smile:

I’m not here to advertise at all and I’m rather looking for Gopher’s
opinions (good or bad) on my code and my implementation since I’d like
to know whether I’ve done things right or whether I’ve gone completely
off tracks.

My points of emphasis would be:

  • Have I made good use of interfaces?
  • Have I made good use of const and global vars ?
  • Have I made good use of GO project structure ?
  • Have I made good use of GO unit testing package?

I really thank you in advance for the time you’ll put in reviewing my project.

Cheers

Quentin

Looks alright, but I would have expected some use of io.MultiWriter somewhere, since that’s certainly the “easiest” way to output to multiple places at once.

Nice job making your own package though. :wink:

1 Like

Too big interface for my taste.
If you want to use this concurrently, must protect the handlers map from concurrent use/modification.

The easiest would be just have a SetHandler, and not expose Handlers directly.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.