logs

package
v2.1.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DEBUG = iota
	INFO
	ERROR
)

Variables

View Source
var GoIdToRequestIdMap = new(sync.Map)

Functions

func GetDeleteFromMapFunc added in v1.2.0

func GetDeleteFromMapFunc(key interface{}) func()

func GetGoID added in v1.2.0

func GetGoID() uint64

func GetNewRequestContext added in v1.2.0

func GetNewRequestContext(actionName string) resources.RequestContext

func InitFileLogger

func InitFileLogger(level Level, filePath string, rotateSize int, params LoggerParams) func()

InitFileLogger initializes the global logger with a file writer to filePath and set at level. It returns a function that clears the global logger. If the global logger is already initialized InitFileLogger panics.

Example

Init a logger to file "/tmp/ubiquity.log" at DEBUG level

package main

import (
	"fmt"
	"github.com/IBM/ubiquity/utils/logs"
	"os"
)

var filePath = "/tmp/ubiquity.log"

var createFilePathSuccess = "Create file path success"
var createFilePathFail = "Create file path fail"

func main() {
	//Example Test, shows how to use the logs.InitFileLogger
	defer logs.InitFileLogger(logs.DEBUG, filePath, 50, logs.LoggerParams{})()
	if _, err := os.Stat(filePath); err == nil {
		fmt.Println(createFilePathSuccess)
	} else {
		fmt.Println(createFilePathFail)
	}
}
Output:

Create file path success

func InitLogger added in v1.0.0

func InitLogger(level Level, filePath string, params LoggerParams) func()

InitLogger initializes the global logger with a file writer to filePath and stdout and set at level. It returns a function that clears the global logger. If the global logger is already initialized InitLogger panics.

func InitStdoutLogger

func InitStdoutLogger(level Level, params LoggerParams) func()

InitStdoutLogger initializes the global logger with stdout and set at level. It returns a function that clears the global logger. If the global logger is already initialized InitStdoutLogger panics.

Example

Init a logger to stdout at DEBUG level

package main

import (
	"github.com/IBM/ubiquity/utils/logs"
)

func main() {
	defer logs.InitStdoutLogger(logs.DEBUG, logs.LoggerParams{})()
}
Output:

Types

type Args

type Args []nameValue

Args provides a way to safely pass additional params in a name=value format to the formatted log string.

Example

Safely pass name=value pairs to be logged with the string.

package main

import (
	"github.com/IBM/ubiquity/utils/logs"
)

func main() {
	logger := logs.GetLogger()
	logger.Info("the info message", logs.Args{{"name1", "value1"}, {"name2", "value2"}, {"name3", "value3"}})
	// (date) (time) INFO (PID) (filename)(line-number) (package-name)(function-name) the info message [[{name1=value1}] [{name2=value2}] [{name3=value3}]]
}
Output:

type Level

type Level int

func GetLogLevelFromString added in v0.4.0

func GetLogLevelFromString(level string) Level

GetLogLevelFromString translates string log level to Level type It returns the level for one of: "debug" / "info" / "error" If there is no match, default is INFO

type Logger

type Logger interface {
	// Debug writes the string and args at DEBUG level
	Debug(str string, args ...Args)
	// Info writes the string and args at INFO level
	Info(str string, args ...Args)
	// Error writes the string and args at ERROR level
	Error(str string, args ...Args)
	// ErrorRet writes the string and args at ERROR level, adding error=err to the args.
	// It returns err, so its convenient to log the error and return it
	ErrorRet(err error, str string, args ...Args) error
	// Trace is used to write log message at upon entering and exiting the function.
	// Unlike other Logger methods, it does not get a string for message.
	// Instead, it writes only ENTER and EXIT
	// It writes the ENTER message when called, and returns a function that writes the EXIT message,
	// so it can be used with defer in 1 line
	Trace(level Level, args ...Args) func()
	// Warning writes the string and args at WARNING level
	Warning(str string, args ...Args)
}

Logger is the interface that wraps the basic log methods

Example (ErrorRet)
package main

import (
	"errors"
	"github.com/IBM/ubiquity/utils/logs"
)

func main() error {
	logger := logs.GetLogger()
	if err := errors.New("some-error"); err != nil {
		return logger.ErrorRet(err, "failed")
	}
	return nil
	// (date) (time) ERROR (PID) (filename)(line-number) (package-name)(function-name) failed [[{err=some-error}]]
}
Output:

Example (Trace)
package main

import (
	"fmt"
	"github.com/IBM/ubiquity/utils/logs"
)

func main() {
	defer logs.GetLogger().Trace(logs.INFO)()
	fmt.Println("doing stuff")
	// (date) (time) INFO (PID) (filename)(line-number) (package-name)(function-name) ENTER []
	// doing stuff
	// (date) (time) INFO (PID) (filename)(line-number) (package-name)(function-name) EXIT []
}
Output:

func GetLogger

func GetLogger() Logger

GetLogger returns the global logger. If the global logger is not initialized GetLogger panics.

type LoggerParams added in v1.2.0

type LoggerParams struct {
	ShowGoid bool
	ShowPid  bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL