Skip to content

2x Simplest logging

ccpaging edited this page Nov 14, 2019 · 2 revisions

Using nxlog4go for Testing Environment

nxlog4go is very simple to use without any configuring, setting. For example:

package main

import (
	"time"
	log "github.com/ccpaging/nxlog4go"
)


func main() {
	log.Fine("This should be omitted as default.")
	log.Debug("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
	log.Info("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
	log.Warn("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
	log.Critical("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
}

Import nxlog4go package

import (
    logger "github.com/ccpaging/nxlog4go"
}

Package Standard Logging

  • Standard logging
    logger.Debug(format, args...)
    logger.Info(format, args...)
    logger.Warn(format, args...)
    logger.Error(format, args...)
    ...
  • With key, value...
    logger.With(key, value...).Debug(format, args...)
    logger.With(key, value...).Info(format, args...)
    logger.With(key, value...).Warn(format, args...)
    logger.With(key, value...).Error(format, args...)
    ...
  • Compatible go std lib
    logger.Print(v ...interface{})
    logger.Println(v ...interface{})
    logger.Printf(format string, v ...interface{})
    ...

User Logger

  • Creating
    log := logger.NewLogger(key, value...)
  • Getting package standard logger
    log := logger.StdLogger()
  • Setting
    log.SetOutput(w io.Writer)
    log.SetPrefix(prefix string)
    log.SetLayout(layout Layout)

    log.Set(key, val...)
    log.SetOptions(key, val) error
  • Setting filter level
    log.Set("level", logger.INFO)
    log.Debug("This should be not omitted now.")

The logging message which level is lower than INFO will be ignored.

Entry Logging

  • Creating and using Entry
    std := logger.StdLogger()
    elog := logger.NewEntry(std)
    elog.Prefix = "prefix"
    elog.Source = "foo"
  • Entry logging
    elog.Debug(message, key, value...)
    elog.Info(message, key, value...)
    elog.Warn(message, key, value...)
    elog.Error(message, key, value...)
    ...

See also:

example.go

Clone this wiki locally