log

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2020 License: BSD-2-Clause Imports: 7 Imported by: 0

README

log

import "github.com/cuberat/go-log"

Overview

Package log implements a logger that logs to a file or any io.Writer. If the io.Writer looks like syslog, then certain parts of the log line will not be generated, as syslog is expected to cover those.

This logger looks like syslog, in that it allows for specifying severities when logging. Unlike syslog, however, instead of setting a default severity for logging, a severity threshold is specified at logger creation time that limits logging to that severity and anything more important. This provides a convenient way to control the verbosity of logging.

The intention is to enable this to be a drop-in replacement, interface-wise, for the standard log/syslog or log. In addition, it provides most of the methods offered by the standard log interface.

You may create a new logger using New() or NewFromFile(), or, if you want to use the default logger, just call methods directly on the package, which will write to os.Stderr.

A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

The interface for this logging package is in an alpha state. That is, it may have some non backward compatible changes in upcoming minor releases.

Installation:

go get github.com/cuberat/go-log

Index

Package files

log.go logger.go

func Alert

func Alert(m string) error

Logs a message with severity LOG_ALERT.

func Alertf

func Alertf(format string, v ...interface{}) error

Logs a message with severity LOG_ALERT. Arguments are handled in the manner of fmt.Printf.

func Crit

func Crit(m string) error

Logs a message with severity LOG_CRIT.

func Critf

func Critf(format string, v ...interface{}) error

Logs a message with severity LOG_CRIT. Arguments are handled in the manner of fmt.Printf.

func Debug

func Debug(m string) error

Logs a message with severity LOG_DEBUG.

func Debugf

func Debugf(format string, v ...interface{}) error

Logs a message with severity LOG_DEBUG. Arguments are handled in the manner of fmt.Printf.

func Emerg

func Emerg(m string) error

Logs a message with severity LOG_EMERG.

func Emergf

func Emergf(format string, v ...interface{}) error

Logs a message with severity LOG_EMERG. Arguments are handled in the manner of fmt.Printf.

func Err

func Err(m string) error

Logs a message with severity LOG_ERR.

func Errf

func Errf(format string, v ...interface{}) error

Logs a message with severity LOG_ERR. Arguments are handled in the manner of fmt.Printf.

func Errorf

func Errorf(format string, v ...interface{}) error

Returns an error like fmt.Errorf, but prepended with the source file name and line number.

func ErrorfDepth

func ErrorfDepth(
    call_depth int,
    format string,
    v ...interface{},
) error

Returns an error like Errorf, but allows you to specify a call depth. For instance, passing a value of 1 as the call_depth will cause the source file and line number to correspond to where the enclosing function is called, instead of where ErrorDepth is called. ErrorDepth(0, ...) is equivalent to calling Errorf.

func Fatal

func Fatal(v ...interface{})

Equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, v ...interface{})

Equivalent to Printf() followed by a call to os.Exit(1).

func Fatalln

func Fatalln(v ...interface{})

Equivalent to Println() followed by a call to os.Exit(1).

func Info

func Info(m string) error

Logs a message with severity LOG_INFO.

func Infof

func Infof(format string, v ...interface{}) error

Logs a message with severity LOG_INFO. Arguments are handled in the manner of fmt.Printf.

func Notice

func Notice(m string) error

Logs a message with severity LOG_NOTICE.

func Noticef

func Noticef(format string, v ...interface{}) error

Logs a message with severity LOG_NOTICE. Arguments are handled in the manner of fmt.Printf.

func Panic

func Panic(v ...interface{})

Equivalent to Print() followed by a call to panic().

func Panicf

func Panicf(format string, v ...interface{})

Equivalent to Printf() followed by a call to panic().

func Panicln

func Panicln(v ...interface{})

Equivalent to Println() followed by a call to panic().

func Print

func Print(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Println.

func SetOutput

func SetOutput(w io.Writer)

Sets the writer where logging output should go for the default logger.

func SetPrefix

func SetPrefix(prefix string)

Sets the prefix to add to the beginning of each log line (after the timestamp) for the default logger.

func SetSeverityThreshold

func SetSeverityThreshold(sev_thresh Severity)

Sets the severity threshold for the default logger. Anything less important (further down the list of severities) will not be logged.

func SetTimestampFunc

func SetTimestampFunc(f TimestampFunc)

Sets the timestamp generator function for the default logger. This will be called to generate the timestamp for each log line.

func Warning

func Warning(m string) error

Logs a message with severity LOG_WARNING.

func Warningf

func Warningf(format string, v ...interface{}) error

Logs a message with severity LOG_WARNING. Arguments are handled in the manner of fmt.Printf.

type Logger

type Logger struct {
    // contains filtered or unexported fields
}

A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method or to the Writer's severity-related method, if it implements the SyslogLike interface. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

func New
func New(w io.Writer, sev_thresh Severity, prefix string) *Logger

Creates a logger from an io.Writer, with the given severity threshold and prefix string.

func NewFromFile
func NewFromFile(file_path string, sev_thresh Severity, prefix string) (*Logger,
    error)

Creates a logger that will write to the specified file path, with the given severity threshold and prefix string

func (*Logger) Alert
func (l *Logger) Alert(m string) error

Logs a message with severity LOG_ALERT.

func (*Logger) Alertf
func (l *Logger) Alertf(format string, v ...interface{}) error

Logs a message with severity LOG_ALERT. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Crit
func (l *Logger) Crit(m string) error

Logs a message with severity LOG_CRIT.

func (*Logger) Critf
func (l *Logger) Critf(format string, v ...interface{}) error

Logs a message with severity LOG_CRIT. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debug
func (l *Logger) Debug(m string) error

Logs a message with severity LOG_DEBUG.

func (*Logger) Debugf
func (l *Logger) Debugf(format string, v ...interface{}) error

Logs a message with severity LOG_DEBUG. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Emerg
func (l *Logger) Emerg(m string) error

Logs a message with severity LOG_EMERG.

func (*Logger) Emergf
func (l *Logger) Emergf(format string, v ...interface{}) error

Logs a message with severity LOG_EMERG. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Err
func (l *Logger) Err(m string) error

Logs a message with severity LOG_ERR.

func (*Logger) Errf
func (l *Logger) Errf(format string, v ...interface{}) error

Logs a message with severity LOG_ERR. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorf
func (l *Logger) Errorf(format string, v ...interface{}) error

Returns an error like fmt.Errorf, but prepended with the source file name and line number.

func (*Logger) ErrorfDepth
func (l *Logger) ErrorfDepth(
    call_depth int,
    format string,
    v ...interface{},
) error

Returns an error like Errorf, but allows you to specify a call depth. For instance, passing a value of 1 as the call_depth will cause the source file and line number to correspond to where the enclosing function is called, instead of where ErrorDepth is called. ErrorDepth(0, ...) is equivalent to calling Errorf.

func (*Logger) Fatal
func (l *Logger) Fatal(v ...interface{})

Equivalent to Print() followed by a call to os.Exit(1).

func (*Logger) Fatalf
func (l *Logger) Fatalf(format string, v ...interface{})

Equivalent to Printf() followed by a call to os.Exit(1).

func (*Logger) Fatalln
func (l *Logger) Fatalln(v ...interface{})

Equivalent to Println() followed by a call to os.Exit(1).

func (*Logger) Info
func (l *Logger) Info(m string) error

Logs a message with severity LOG_INFO.

func (*Logger) Infof
func (l *Logger) Infof(format string, v ...interface{}) error

Logs a message with severity LOG_INFO. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Notice
func (l *Logger) Notice(m string) error

Logs a message with severity LOG_NOTICE.

func (*Logger) Noticef
func (l *Logger) Noticef(format string, v ...interface{}) error

Logs a message with severity LOG_NOTICE. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Panic
func (l *Logger) Panic(v ...interface{})

Equivalent to Print() followed by a call to panic().

func (*Logger) Panicf
func (l *Logger) Panicf(format string, v ...interface{})

Equivalent to Printf() followed by a call to panic().

func (*Logger) Panicln
func (l *Logger) Panicln(v ...interface{})

Equivalent to Println() followed by a call to panic().

func (*Logger) Print
func (l *Logger) Print(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf
func (l *Logger) Printf(format string, v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println
func (l *Logger) Println(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetOutput
func (l *Logger) SetOutput(w io.Writer)

Sets the writer where logging output should go.

func (*Logger) SetPrefix
func (l *Logger) SetPrefix(prefix string)

Sets the prefix to add to the beginning of each log line (after the timestamp).

func (*Logger) SetSeverityThreshold
func (l *Logger) SetSeverityThreshold(sev_thresh Severity)

Sets the severity threshold. Anything less important (further down the list of severities) will not be logged.

func (*Logger) SetTimestampFunc
func (l *Logger) SetTimestampFunc(f TimestampFunc)

Sets the timestamp generator function. This will be called to generate the timestamp for each log line.

func (*Logger) Warning
func (l *Logger) Warning(m string) error

Logs a message with severity LOG_WARNING.

func (*Logger) Warningf
func (l *Logger) Warningf(format string, v ...interface{}) error

Logs a message with severity LOG_WARNING. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Write
func (l *Logger) Write(b []byte) (int, error)

Writes a log message.

type Severity

type Severity int

The Severity type.

const (
    LOG_EMERG Severity = iota
    LOG_ALERT
    LOG_CRIT
    LOG_ERR
    LOG_WARNING
    LOG_NOTICE
    LOG_INFO
    LOG_DEBUG
)

Severities to be passed to New() or SetSeverityThreshold().

func SeverityFromString
func SeverityFromString(sev_string string) (Severity, error)

Converts a severity name to a Severity that can be passed to New() and SetSeverityThreshold(). This is useful for allowing specification of the severity on the command line an creating a logger that uses that threshold.

type SyslogLike

type SyslogLike interface {
    Alert(m string) error
    Crit(m string) error
    Debug(m string) error
    Emerg(m string) error
    Err(m string) error
    Info(m string) error
    Notice(m string) error
    Warning(m string) error
    Write(b []byte) (int, error)
}

If the io.Writer passed to New() or SetOutput() implements the SyslogLike interface, it is treated as if it actually is a standard log/syslog logger.

type TimestampFunc

type TimestampFunc func() string

Timestamp generator function type.


Generated by godoc2md

Documentation

Overview

Package log implements a logger that logs to a file or any io.Writer. If the io.Writer looks like syslog, then certain parts of the log line will not be generated, as syslog is expected to cover those.

This logger looks like syslog, in that it allows for specifying severities when logging. Unlike syslog, however, instead of setting a default severity for logging, a severity threshold is specified at logger creation time that limits logging to that severity and anything more important. This provides a convenient way to control the verbosity of logging.

The intention is to enable this to be a drop-in replacement, interface-wise, for the standard log/syslog or log. In addition, it provides most of the methods offered by the standard log interface.

You may create a new logger using New() or NewFromFile(), or, if you want to use the default logger, just call methods directly on the package, which will write to os.Stderr.

A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

The interface for this logging package is in an alpha state. That is, it may have some non backward compatible changes in upcoming minor releases.

Installation:

go get github.com/cuberat/go-log

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(m string) error

Logs a message with severity LOG_ALERT.

func Alertf

func Alertf(format string, v ...interface{}) error

Logs a message with severity LOG_ALERT. Arguments are handled in the manner of fmt.Printf.

func Crit

func Crit(m string) error

Logs a message with severity LOG_CRIT.

func Critf

func Critf(format string, v ...interface{}) error

Logs a message with severity LOG_CRIT. Arguments are handled in the manner of fmt.Printf.

func Debug

func Debug(m string) error

Logs a message with severity LOG_DEBUG.

func Debugf

func Debugf(format string, v ...interface{}) error

Logs a message with severity LOG_DEBUG. Arguments are handled in the manner of fmt.Printf.

func Emerg

func Emerg(m string) error

Logs a message with severity LOG_EMERG.

func Emergf

func Emergf(format string, v ...interface{}) error

Logs a message with severity LOG_EMERG. Arguments are handled in the manner of fmt.Printf.

func Err

func Err(m string) error

Logs a message with severity LOG_ERR.

func Errf

func Errf(format string, v ...interface{}) error

Logs a message with severity LOG_ERR. Arguments are handled in the manner of fmt.Printf.

func Errorf added in v1.1.0

func Errorf(format string, v ...interface{}) error

Returns an error like `fmt.Errorf`, but prepended with the source file name and line number.

func ErrorfDepth added in v1.1.0

func ErrorfDepth(
	call_depth int,
	format string,
	v ...interface{},
) error

Returns an error like `Errorf`, but allows you to specify a call depth. For instance, passing a value of 1 as the call_depth will cause the source file and line number to correspond to where the enclosing function is called, instead of where `ErrorDepth` is called. `ErrorDepth(0, ...)` is equivalent to calling `Errorf`.

func Fatal

func Fatal(v ...interface{})

Equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, v ...interface{})

Equivalent to Printf() followed by a call to os.Exit(1).

func Fatalln

func Fatalln(v ...interface{})

Equivalent to Println() followed by a call to os.Exit(1).

func Info

func Info(m string) error

Logs a message with severity LOG_INFO.

func Infof

func Infof(format string, v ...interface{}) error

Logs a message with severity LOG_INFO. Arguments are handled in the manner of fmt.Printf.

func Notice

func Notice(m string) error

Logs a message with severity LOG_NOTICE.

func Noticef

func Noticef(format string, v ...interface{}) error

Logs a message with severity LOG_NOTICE. Arguments are handled in the manner of fmt.Printf.

func Panic

func Panic(v ...interface{})

Equivalent to Print() followed by a call to panic().

func Panicf

func Panicf(format string, v ...interface{})

Equivalent to Printf() followed by a call to panic().

func Panicln

func Panicln(v ...interface{})

Equivalent to Println() followed by a call to panic().

func Print

func Print(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Println.

func SetOutput

func SetOutput(w io.Writer)

Sets the writer where logging output should go for the default logger.

func SetPrefix

func SetPrefix(prefix string)

Sets the prefix to add to the beginning of each log line (after the timestamp) for the default logger.

func SetSeverityThreshold

func SetSeverityThreshold(sev_thresh Severity)

Sets the severity threshold for the default logger. Anything less important (further down the list of severities) will not be logged.

func SetTimestampFunc

func SetTimestampFunc(f TimestampFunc)

Sets the timestamp generator function for the default logger. This will be called to generate the timestamp for each log line.

func Warning

func Warning(m string) error

Logs a message with severity LOG_WARNING.

func Warningf

func Warningf(format string, v ...interface{}) error

Logs a message with severity LOG_WARNING. Arguments are handled in the manner of fmt.Printf.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method or to the Writer's severity-related method, if it implements the SyslogLike interface. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

func New

func New(w io.Writer, sev_thresh Severity, prefix string) *Logger

Creates a logger from an io.Writer, with the given severity threshold and prefix string.

func NewFromFile

func NewFromFile(file_path string, sev_thresh Severity, prefix string) (*Logger,
	error)

Creates a logger that will write to the specified file path, with the given severity threshold and prefix string

func (*Logger) Alert

func (l *Logger) Alert(m string) error

Logs a message with severity LOG_ALERT.

func (*Logger) Alertf

func (l *Logger) Alertf(format string, v ...interface{}) error

Logs a message with severity LOG_ALERT. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Crit

func (l *Logger) Crit(m string) error

Logs a message with severity LOG_CRIT.

func (*Logger) Critf

func (l *Logger) Critf(format string, v ...interface{}) error

Logs a message with severity LOG_CRIT. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debug

func (l *Logger) Debug(m string) error

Logs a message with severity LOG_DEBUG.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{}) error

Logs a message with severity LOG_DEBUG. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Emerg

func (l *Logger) Emerg(m string) error

Logs a message with severity LOG_EMERG.

func (*Logger) Emergf

func (l *Logger) Emergf(format string, v ...interface{}) error

Logs a message with severity LOG_EMERG. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Err

func (l *Logger) Err(m string) error

Logs a message with severity LOG_ERR.

func (*Logger) Errf

func (l *Logger) Errf(format string, v ...interface{}) error

Logs a message with severity LOG_ERR. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorf added in v1.1.0

func (l *Logger) Errorf(format string, v ...interface{}) error

Returns an error like `fmt.Errorf`, but prepended with the source file name and line number.

func (*Logger) ErrorfDepth added in v1.1.0

func (l *Logger) ErrorfDepth(
	call_depth int,
	format string,
	v ...interface{},
) error

Returns an error like `Errorf`, but allows you to specify a call depth. For instance, passing a value of 1 as the call_depth will cause the source file and line number to correspond to where the enclosing function is called, instead of where `ErrorDepth` is called. `ErrorDepth(0, ...)` is equivalent to calling `Errorf`.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Equivalent to Print() followed by a call to os.Exit(1).

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Equivalent to Printf() followed by a call to os.Exit(1).

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Equivalent to Println() followed by a call to os.Exit(1).

func (*Logger) Info

func (l *Logger) Info(m string) error

Logs a message with severity LOG_INFO.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{}) error

Logs a message with severity LOG_INFO. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Notice

func (l *Logger) Notice(m string) error

Logs a message with severity LOG_NOTICE.

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...interface{}) error

Logs a message with severity LOG_NOTICE. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Equivalent to Print() followed by a call to panic().

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Equivalent to Printf() followed by a call to panic().

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

Equivalent to Println() followed by a call to panic().

func (*Logger) Print

func (l *Logger) Print(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println

func (l *Logger) Println(v ...interface{}) error

Prints to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

Sets the writer where logging output should go.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

Sets the prefix to add to the beginning of each log line (after the timestamp).

func (*Logger) SetSeverityThreshold

func (l *Logger) SetSeverityThreshold(sev_thresh Severity)

Sets the severity threshold. Anything less important (further down the list of severities) will not be logged.

func (*Logger) SetTimestampFunc

func (l *Logger) SetTimestampFunc(f TimestampFunc)

Sets the timestamp generator function. This will be called to generate the timestamp for each log line.

func (*Logger) Warning

func (l *Logger) Warning(m string) error

Logs a message with severity LOG_WARNING.

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{}) error

Logs a message with severity LOG_WARNING. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Write

func (l *Logger) Write(b []byte) (int, error)

Writes a log message.

type Severity

type Severity int

The Severity type.

const (
	LOG_EMERG Severity = iota
	LOG_ALERT
	LOG_CRIT
	LOG_ERR
	LOG_WARNING
	LOG_NOTICE
	LOG_INFO
	LOG_DEBUG
)

Severities to be passed to New() or SetSeverityThreshold().

func SeverityFromString

func SeverityFromString(sev_string string) (Severity, error)

Converts a severity name to a Severity that can be passed to New() and SetSeverityThreshold(). This is useful for allowing specification of the severity on the command line an creating a logger that uses that threshold.

type SyslogLike

type SyslogLike interface {
	Alert(m string) error
	Crit(m string) error
	Debug(m string) error
	Emerg(m string) error
	Err(m string) error
	Info(m string) error
	Notice(m string) error
	Warning(m string) error
	Write(b []byte) (int, error)
}

If the io.Writer passed to New() or SetOutput() implements the SyslogLike interface, it is treated as if it actually is a standard log/syslog logger.

type TimestampFunc

type TimestampFunc func() string

Timestamp generator function type.

Directories

Path Synopsis
test_utils

Jump to

Keyboard shortcuts

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