Review My Logging Libraries

Hello i am new to go.

I created super simple libraries for logging and colored log viewing for myself.

Thanks in advance for your precious comments!

  • I would use io.Writer for output.

  • there is no tests.

  • I wouldn’t compare level in all the logs functions, I would check it in the instance log function.

  • move formatting and level checking in one place.

      func Debugf(messageFormat string, values ...interface{}) {
          instance.log(DebugTraceLevel, message, values)
1 Like

Thank you!

I updated public logging functions:

func Debug(message string) {
	log(DebugTraceLevel, debugPrefix, message)

func Debugf(messageFormat string, values ...interface{}) {
	log(DebugTraceLevel, debugPrefix, messageFormat, values...)

And privte log function:

func log(traceLevel int, prefix string, messageFormat string, values ...interface{}) {
	//check initialization

	//check trace level
	if instance.traceLevel > traceLevel {

	defer instance.mutex.Unlock()

	//create formatted message
	message := fmt.Sprintf(messageFormat, values...)
	formattedMessage := fmt.Sprintf("[%s][%s]: %s%s", time.Now().Format(timeFormat), prefix, message, newLine)


Is there any chance to use only one Sprintf function?

I will write tests and also update write method after understanding the difference between writing methods.

Değerli zamanınız için teşekkürler.

Best regards

you can combine message formatter and others into one format and pass all the values. But I would keep them separated, one is actually message, other ones(time, prefix) are for tracing.

:tr: Rica ederim.

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