log

package
v0.0.0-...-00e7cc2 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2018 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package log provides convenience functions to allow more granular levels of logging Requirements:

  • structured logging - output will most likely be in json, but that shouldn't be known by the log creators
  • log levels - limited as possible but convenient and impossible to screw up
  • convenient setup and integration with standard baseLogger
  • ability to include system/container/service names in logs by default
  • include stack traces or other info

Requirements:

  • plain text output to local log file (io.Writer)

  • structured json output to CloudWatch (or similar service)

    inputs for each level: error OR printf style

Index

Constants

View Source
const (
	// FlagDebug is the flag for enabling messages in a logger for Debug messages.
	FlagDebug, _ = LevelFlag(1 << iota), iota
	// FlagAudit is the flag for enabling messages in a logger for Audit messages.
	FlagAudit, _
	// FlagInfo is the flag for enabling messages in a logger for Info messages.
	FlagInfo, _
	// FlagAccess is the flag for enabling messages in a logger for Access messages.
	FlagAccess, _
	// FlagWarn is the flag for enabling messages in a logger for Warn messages.
	FlagWarn, _
	// FlagError is the flag for enabling messages in a logger for Error messages.
	FlagError, _
	// FlagFatal is the flag for enabling messages in a logger for Fatal messages.
	FlagFatal, _
	// FlagAll has all log level flags set.
	FlagAll = (FlagFatal - 1) | FlagFatal
	// MaskDefault is set to only record Access, Error and Fatal messages.
	MaskDefault = FlagAccess | FlagError | FlagFatal
	// MaskVerbose filters everything not between Info and Fatal
	MaskVerbose = FlagInfo | FlagWarn | MaskDefault
	// MaskDebug filters nothing
	MaskDebug = FlagAll

	// LevelFatal string for identifying message that are Fatal.
	LevelFatal Level = "FATAL"
	// LevelError string for identifying message that are Error.
	LevelError Level = "ERROR"
	// LevelWarn string for identifying message that are Warn.
	LevelWarn Level = "WARN"
	// LevelAudit string for identifying message that are Audit.
	LevelAudit Level = "AUDIT"
	// LevelInfo string for identifying message that are Info.
	LevelInfo Level = "INFO"
	// LevelAccess string for identifying message that are Access.
	LevelAccess Level = "ACCESS"
	// LevelDebug string for identifying message that are for Debug.
	LevelDebug Level = "DEBUG"
)
View Source
const (

	// MaskEnv is the environment variable that stores the logging mask
	MaskEnv = "LOGGING_MASK"
)

Variables

This section is empty.

Functions

func Access

func Access(e error) error

Access logs a message regarding access information.

func Accessf

func Accessf(format string, a ...interface{}) string

Accessf logs a message regarding access information.

func AddOutput

func AddOutput(output Output)

AddOutput to the global logger

func Audit

func Audit(e error) error

Audit logs a message regarding normal events that are to be systematically recorded (ex: RMS messages).

func Auditf

func Auditf(format string, a ...interface{}) string

Auditf logs a message regarding normal events that are to be systematically recorded (ex: RMS messages).

func Debug

func Debug(e error) error

Debug logs a message regarding debug information.

func Debugf

func Debugf(format string, a ...interface{}) string

Debugf logs a message regarding debug inforrmation.

func DefaultOutput

func DefaultOutput(m Message)

DefaultOutput mimics the behavior of the default logging package by printing to StdErr. This is intended to be passed as one of the output functions to New() or Global().

func Error

func Error(e error) error

Error logs a message regarding a non-fatal internal failure representing a probable code/system issue.

func Errorf

func Errorf(format string, a ...interface{}) string

Errorf logs a message regarding a non-fatal internal failure representing a probable code/system issue.

func Fatal

func Fatal(e error) error

Fatal logs a message regarding a failure that is severe enough to warrant process termination.

func Fatalf

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

Fatalf logs a message regarding a failure that is severe enough to warrant process termination.

func Info

func Info(e error) error

Info logs a message regarding debug information.

func Infof

func Infof(format string, a ...interface{}) string

Infof logs a message regarding debug information.

func JSONOutput

func JSONOutput(m Message)

JSONOutput formats the log map into Json and then outputs to stderr (appropriate for aws production services)

func NewGlobal

func NewGlobal(id string, outputs ...Output)

NewGlobal configures the global logger (analog of New()). Also redirects the standard logger to output through the global logger. This should be used as the first action in main().

func Warn

func Warn(e error) error

Warn logs a message regarding a failure type that is expected to occasionally occur (ex: invalid arguments).

func Warnf

func Warnf(format string, a ...interface{}) string

Warnf logs a message regarding a failure type that is expected to occasionally occur (ex: invalid arguments).

Types

type Level

type Level string

Level represents the importance of a message.

func (Level) Convert

func (f Level) Convert() LevelFlag

Convert this level to a level flag

func (Level) Index

func (f Level) Index() (int, bool)

Index for the passed level in the outputs array.

type LevelFlag

type LevelFlag uint8

LevelFlag identifies the levels that a logger output accepts.

type Logger

type Logger interface {
	// New logger with a copied session and outputs as this logger. Changes to this logger
	// will not affect the new logger.
	New(id string) Logger
	// GetSessionID that is currently in use on this logger
	GetSessionID() string
	// SetSessionID that is currently in use on this logger
	SetSessionID(string)
	// Fatal logs a message regarding a failure that is severe enough to warrant process termination.
	Fatal(e error) error
	// Fatalf logs a message regarding a failure that is severe enough to warrant process termination.
	Fatalf(format string, a ...interface{}) string
	// Error logs a message regarding a non-fatal internal failure representing a probable code/system issue.
	Error(e error) error
	// Errorf logs a message regarding a non-fatal internal failure representing a probable code/system issue.
	Errorf(format string, a ...interface{}) string
	// Warn logs a message regarding a failure type that is expected to occasionally occur (ex: invalid arguments).
	Warn(e error) error
	// Warnf logs a message regarding a failure type that is expected to occasionally occur (ex: invalid arguments).
	Warnf(format string, a ...interface{}) string
	// Audit logs a message regarding normal events that are to be systematically recorded (ex: RMS messages).
	Audit(e error) error
	// Auditf logs a message regarding normal events that are to be systematically recorded (ex: RMS messages).
	Auditf(format string, a ...interface{}) string
	// Access logs a message regarding information about the state of a successfully executing system.
	Access(e error) error
	// Accessf logs a message regarding information about the state of a successfully executing system.
	Accessf(format string, a ...interface{}) string
	// Info logs a message regarding information about the state of a successfully executing system.
	Info(e error) error
	// Infof logs a message regarding information about the state of a successfully executing system.
	Infof(format string, a ...interface{}) string
	// Debug logs a message regarding debug information that would be overly verbose for an info message.
	Debug(e error) error
	// Debugf logs a message regarding debug information that would be overly verbose for an info message.
	Debugf(format string, a ...interface{}) string
	// AddOutput to this logger
	AddOutput(Output)
}

Logger is the tiered level logging interface

func Global

func Global() Logger

Global logger that is currently configured.

func New

func New(id string, outputs ...Output) Logger

New returns an implementation of the tiered logging interface

type Message

type Message struct {
	LogIdentifier string   `json:"LogIdentifier,omitempty"`
	SessionID     string   `json:"SessionID,omitempty"`
	Level         Level    `json:"Level,omitempty"`
	TimestampUnix int64    `json:"TimestampUnix,omitempty"`
	Timestamp     string   `json:"Timestamp,omitempty"`
	Caller        string   `json:"Caller,omitempty"`
	Message       string   `json:"Message,omitempty"`
	Stack         []string `json:"Stack,omitempty"`
	Error         error    `json:"Error,omitempty"`
}

Message encapsulates all fields that can possibly be a log message.

func (*Message) JSONString

func (m *Message) JSONString() string

JSONString representation of this message.

func (*Message) TTYString

func (m *Message) TTYString() string

TTYString for logging this message to console.

type Output

type Output interface {
	// Level returns the LevelFlag that this logger output accepts
	Level() LevelFlag
	// Log the message passed
	Log(message Message)
}

Output represents a single output in a sequential chain of log message receivers.

func FunctionFromEnv

func FunctionFromEnv() Output

FunctionFromEnv reads the environment variables and returns an appropriate log function

func NewOutput

func NewOutput(level LevelFlag, log func(Message)) Output

NewOutput for use in the log chain.

type StackLogger

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

StackLogger implments the tiered logging interface with a string stack for the messages

func NewStackLogger

func NewStackLogger() *StackLogger

NewStackLogger puts the log messages onto a stack

func (*StackLogger) Access

func (l *StackLogger) Access(e error) error

Access logs a message regarding access information.

func (*StackLogger) Accessf

func (l *StackLogger) Accessf(format string, a ...interface{}) string

Accessf logs a message regarding access information.

func (*StackLogger) AddOutput

func (l *StackLogger) AddOutput(output Output)

AddOutput StackLogger does not support this for obvious reasons

func (*StackLogger) Audit

func (l *StackLogger) Audit(e error) error

Audit logs a message regarding normal events that are to be systematically recorded (ex: RMS messages).

func (*StackLogger) Auditf

func (l *StackLogger) Auditf(format string, a ...interface{}) string

Auditf logs a message regarding normal events that are to be systematically recorded (ex: RMS messages).

func (*StackLogger) Debug

func (l *StackLogger) Debug(e error) error

Debug logs a message regarding debug information.

func (*StackLogger) Debugf

func (l *StackLogger) Debugf(format string, a ...interface{}) string

Debugf logs a message regarding debug information.

func (*StackLogger) Error

func (l *StackLogger) Error(e error) error

Error logs a message regarding a non-fatal internal failure representing a probable code/system issue.

func (*StackLogger) Errorf

func (l *StackLogger) Errorf(format string, a ...interface{}) string

Errorf logs a message regarding a non-fatal internal failure representing a probable code/system issue.

func (*StackLogger) Fatal

func (l *StackLogger) Fatal(e error) error

Fatal logs a message regarding a failure that is severe enough to warrant process termination.

func (*StackLogger) Fatalf

func (l *StackLogger) Fatalf(format string, a ...interface{}) string

Fatalf logs a message regarding a failure that is severe enough to warrant process termination.

func (*StackLogger) GetSessionID

func (l *StackLogger) GetSessionID() string

GetSessionID empty string

func (*StackLogger) Info

func (l *StackLogger) Info(e error) error

Info logs a message regarding debug information.

func (*StackLogger) Infof

func (l *StackLogger) Infof(format string, a ...interface{}) string

Infof logs a message regarding debug information.

func (*StackLogger) IsEmpty

func (l *StackLogger) IsEmpty() bool

IsEmpty checks if the stack has no messages

func (*StackLogger) New

func (l *StackLogger) New(id string) Logger

New returns this logger

func (*StackLogger) Pop

func (l *StackLogger) Pop() (string, error)

Pop returns the last message from the message stack

func (*StackLogger) SetSessionID

func (l *StackLogger) SetSessionID(id string)

SetSessionID noop

func (*StackLogger) Warn

func (l *StackLogger) Warn(e error) error

Warn logs a message regarding a failure type that is expected to occasionally occur (ex: invalid arguments).

func (*StackLogger) Warnf

func (l *StackLogger) Warnf(format string, a ...interface{}) string

Warnf logs a message regarding a failure type that is expected to occasionally occur (ex: invalid arguments).

type Tracer

type Tracer interface {
	Trace() []string
}

Tracer provides a stack trace

type TracerError

type TracerError interface {
	Tracer
	error
}

TracerError is an amalgamation of the Tracer and error interfaces

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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