logs

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2017 License: Apache-2.0 Imports: 5 Imported by: 30

Documentation

Index

Examples

Constants

View Source
const (
	DEBUG = iota
	INFO
	ERROR
)

Variables

This section is empty.

Functions

func InitFileLogger

func InitFileLogger(level Level, filePath string) 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 (
	"github.com/IBM/ubiquity/utils/logs"
)

func main() {
	defer logs.InitFileLogger(logs.DEBUG, "/tmp/ubiquity.log")()
}
Output:

func InitStdoutLogger

func InitStdoutLogger(level Level) 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)()
}
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()
}

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.

Jump to

Keyboard shortcuts

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