log

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 4 Imported by: 32

README

ContainerSSH - Launch Containers on Demand

ContainerSSH Logging Library

Go Report Card LGTM Alerts

This library provides internal logging for ContainerSSH. Its functionality is very similar to how syslog does logging.

Note: This is a developer documentation.
The user documentation for ContainerSSH is located at containerssh.github.io.

Getting a logger

The main interface provided by this library is the Logger interface, which is described in logger.go.

You could use it like this:

type MyModule struct {
    logger log.Logger 
}

func (m * MyModule) DoSomething() {
    m.logger.Debug("This is a debug message")
}

The logger provides logging functions for the following levels:

  • Debug
  • Info
  • Notice
  • Warning
  • Error
  • Critical
  • Alert
  • Emergency

Each of these functions have the following 4 variants:

  • Debug logs a string message
  • Debuge logs an error
  • Debugd logs an arbitrary data structure (interface{})
  • Debugf performs a string formating with fmt.Sprintf before logging

In addition, the logger also provides a generic Log(...interface{}) function for compatibility that logs in the info log level.

Creating logger

The simplest way to create a logger is to use the convenience functions:

logger := standard.New()
loggerFactory := standard.NewFactory()

You can also create a custom pipeline if you wish:

writer          := os.Stdout
minimumLogLevel := log.LevelInfo
logFormatter    := ljson.NewLJsonLogFormatter()
p := pipeline.NewLoggerPipeline(minimumLogLevel, logFormatter, writer)
p.Warning("test") 

This will create a pipeline that writes log messages to the standard output in newline-delimited JSON format. You can, of course, also implement your own log formatter by implementing the interface in formatter/formatter.go.

Plugging the go logger in

This package also provides the facility to plug in the go logger. This can be done by creating a logger as follows:

goLogWriter := log.NewGoLogWriter(logger)
goLogger := goLog.New(goLogWriter, "", 0)
goLogger.Println("Hello world!")

If you want to change the log facility globally:

goLogWriter := log.NewGoLogWriter(logger)
log.SetOutput(goLogWriter)
log.Println("Hello world!")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGoLogWriter added in v0.9.1

func NewGoLogWriter(backendLogger Logger) io.Writer

NewGoLogWriter creates an adapter for the go logger that writes using the Log method of the logger.

Types

type Level

type Level int8

Level syslog-style log level identifiers

const (
	LevelDebug     Level = 7
	LevelInfo      Level = 6
	LevelNotice    Level = 5
	LevelWarning   Level = 4
	LevelError     Level = 3
	LevelCritical  Level = 2
	LevelAlert     Level = 1
	LevelEmergency Level = 0
)

Supported values for Level

func (Level) String

func (level Level) String() (LevelString, error)

String Convert the int level to the string representation

func (Level) Validate

func (level Level) Validate() error

Validate if the log level has a valid value

type LevelString

type LevelString string

LevelString is a type for supported log level strings

const (
	LevelDebugString     LevelString = "debug"
	LevelInfoString      LevelString = "info"
	LevelNoticeString    LevelString = "notice"
	LevelWarningString   LevelString = "warning"
	LevelErrorString     LevelString = "error"
	LevelCriticalString  LevelString = "crit"
	LevelAlertString     LevelString = "alert"
	LevelEmergencyString LevelString = "emerg"
)

List of valid string values for log levels

func (LevelString) ToLevel

func (level LevelString) ToLevel() (Level, error)

ToLevel convert the string level to the int representation

type Logger

type Logger interface {
	SetLevel(level Level)
	Debug(message string)
	Debuge(err error)
	Debugd(data interface{})
	Debugf(format string, args ...interface{})
	Info(message string)
	Infoe(err error)
	Infod(data interface{})
	Infof(format string, args ...interface{})
	Notice(message string)
	Noticee(err error)
	Noticed(data interface{})
	Noticef(format string, args ...interface{})
	Warning(message string)
	Warninge(err error)
	Warningd(data interface{})
	Warningf(format string, args ...interface{})
	Error(message string)
	Errore(err error)
	Errord(data interface{})
	Errorf(format string, args ...interface{})
	Critical(message string)
	Criticale(err error)
	Criticald(data interface{})
	Criticalf(format string, args ...interface{})
	Alert(message string)
	Alerte(err error)
	Alertd(data interface{})
	Alertf(format string, args ...interface{})
	Emergency(message string)
	Emergencye(err error)
	Emergencyd(data interface{})
	Emergencyf(format string, args ...interface{})
	Log(...interface{})
}

Logger The logger interface provides logging facilities on various levesl

type LoggerFactory

type LoggerFactory interface {
	Make(level Level) Logger
}

LoggerFactory is a factory to create a logger on demand

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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