logger

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2016 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllLevels = []Level{
	Debug,
	Info,
	Warning,
	Error,
	Fatal,
}

AllLevels contains all possible variants of Level.

Functions

This section is empty.

Types

type Config

type Config struct {
	// @default info
	//
	// Minimal log level the logger will output its messages. All below will be ignored.
	Level Level `json:"level" yaml:"level"`

	// @default info
	//
	// If the service prints something to “stdout“ instance will logged with instance level.
	StdoutLevel Level `json:"stdoutLevel" yaml:"stdoutLevel"`

	// @default error
	//
	// If the service prints something to “stderr“ instance will logged with instance level.
	StderrLevel Level `json:"stderrLevel" yaml:"stderrLevel"`

	// @default "console"
	//
	// Target file of the logger. The file will be created if not exist - but not the parent directory.
	//
	// If instance value is set to “console“ the whole output will go to “stdout“ or to “stderr“ on every log level
	// above or equal to {@ref .Level#Warning warning}.
	Filename values.String `json:"filename" yaml:"filename"`

	// @default 500
	//
	// Maximum size in megabytes of the log file before it gets rotated.
	//
	// This is ignored if {@ref #Filename filename} os set to “console“.
	MaxSizeInMb values.NonNegativeInteger `json:"maxSizeInMb" yaml:"maxSizeInMb"`

	// @default 500
	//
	// Maximum number of old log files to retain.
	//
	// This is ignored if {@ref #Filename filename} os set to “console“.
	MaxBackups values.NonNegativeInteger `json:"maxBackups" yaml:"maxBackups"`

	// @default 1
	//
	// Maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc.
	//
	// This is ignored if {@ref #Filename filename} os set to “console“.
	MaxAgeInDays values.NonNegativeInteger `json:"maxAgeInDays" yaml:"maxAgeInDays"`

	// @default "%d{YYYY-MM-DD HH:mm:ss} [%-5.5p] [%c] %m%n%P{%m}"
	//
	// Pattern how to format the log messages to output with.
	Pattern Pattern `json:"pattern" yaml:"pattern"`
}

Description

A logger handles every output generated by the daemon itself, the process or other parts controlled by the daemon.

func NewConfig

func NewConfig() Config

NewConfig creates a new instance of Config.

func (*Config) BeforeUnmarshalYAML

func (instance *Config) BeforeUnmarshalYAML() error

BeforeUnmarshalYAML is used until yaml unmarshalling. Do not call directly.

func (Config) Validate

func (instance Config) Validate() error

Validate do validate action on this object and return an error object if any.

type Entry

type Entry struct {
	Time     time.Time
	Message  string
	Priority Level
	Category string
	Stack    stack.Stack
	Uptime   time.Duration
	Problem  interface{}
}

Entry represents an entry to be logged.

func NewEntry

func NewEntry(framesToSkip int, problem interface{}, category string, prioriy Level, time time.Time, message string, uptime time.Duration) Entry

NewEntry creates a new instance of Entry.

func (Entry) Format

func (e Entry) Format(pattern Pattern, framesToSkip int) (string, error)

Format formats the current entry using the given pattern.

type FormatError

type FormatError struct {
	Message  string
	Position int
}

FormatError represents an error if a given pattern contains wrong arguments.

func NewFormatError

func NewFormatError(position int, message string, a ...interface{}) FormatError

NewFormatError creates a new instance of FormatError.

func (FormatError) Error

func (e FormatError) Error() string

type Level

type Level int

Description

Represents a level for logging with a {@ref .Config Logger}

const (
	// @id debug
	// Used for debugging proposes. This level is only required you something goes wrong and you need more information.
	Debug Level = 200

	// @id info
	// This is the regular level. Every normal message will be logged with instance level.
	Info Level = 300

	// @id warning
	// If a problem appears but the program is still able to continue its work, instance level is used.
	Warning Level = 400

	// @id error
	// If a problem appears and the program is not longer able to continue its work, instance level is used.
	Error Level = 500

	// @id fatal
	// This level is used on dramatic problems.
	Fatal Level = 600
)

func (Level) CheckedString

func (instance Level) CheckedString() (string, error)

CheckedString is like String but return also an optional error if there are some validation errors.

func (Level) DisplayForLogging

func (instance Level) DisplayForLogging() string

DisplayForLogging returns a string that could be used to display this level in log messages.

func (Level) IsIndicatingProblem

func (instance Level) IsIndicatingProblem() bool

IsIndicatingProblem returns true if this level indicates a problem.

func (Level) MarshalJSON

func (instance Level) MarshalJSON() ([]byte, error)

MarshalJSON is used until json marshalling. Do not call directly.

func (Level) MarshalYAML

func (instance Level) MarshalYAML() (interface{}, error)

MarshalYAML is used until yaml marshalling. Do not call directly.

func (*Level) Set

func (instance *Level) Set(value string) error

Set the given string to current object from a string. Return an error object if there are some problems while transforming the string.

func (Level) String

func (instance Level) String() string

func (*Level) UnmarshalJSON

func (instance *Level) UnmarshalJSON(b []byte) error

UnmarshalJSON is used until json unmarshalling. Do not call directly.

func (*Level) UnmarshalYAML

func (instance *Level) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is used until yaml unmarshalling. Do not call directly.

func (Level) Validate

func (instance Level) Validate() error

Validate do validate action on this object and return an error object if any.

type Logger

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

Logger represents a logger to log events to different sources like console or files.

func NewLogger

func NewLogger(conf Config, name string, syncGroup *usync.Group) (*Logger, error)

NewLogger creates a new instance of Logger.

func (*Logger) Close

func (i *Logger) Close()

Close will close this logger and all of its resources.

func (*Logger) EntryFor

func (i *Logger) EntryFor(framesToSkip int, problem interface{}, priority Level, time time.Time, message string) Entry

EntryFor creates a new entry for given parameters using the current Logger instance.

func (Logger) IsOpen

func (i Logger) IsOpen() bool

IsOpen returns true if the current logger is still open and usable.

func (*Logger) Log

func (i *Logger) Log(level Level, pattern interface{}, args ...interface{})

Log logs the given pattern with the given level.

func (*Logger) LogAdvanced added in v0.1.6

func (i *Logger) LogAdvanced(framesToSkip int, problem interface{}, level Level, pattern interface{}, args ...interface{})

LogAdvanced logs a problem with the given pattern and level.

func (*Logger) LogProblem

func (i *Logger) LogProblem(problem interface{}, level Level, pattern interface{}, args ...interface{})

LogProblem logs a problem with the given pattern and level.

func (*Logger) NewOutputStreamWrapperFor added in v0.1.6

func (i *Logger) NewOutputStreamWrapperFor(l Level) io.Writer

NewOutputStreamWrapperFor creates a writer to use for redirect every output to a logger.

func (*Logger) Stderr

func (i *Logger) Stderr() io.Writer

Stderr creates a writer to use for redirect every Stderr output to a logger.

func (*Logger) Stdin

func (i *Logger) Stdin() io.Reader

Stdin creates a Reader to use for redirect every Stdin input.

func (*Logger) Stdout

func (i *Logger) Stdout() io.Writer

Stdout creates a writer to use for redirect every Stdout output to a logger.

func (Logger) Uptime

func (i Logger) Uptime() time.Duration

Uptime returns the uptime duration of this logger.

type Pattern

type Pattern string

Description

A flexible pattern string.

The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers.

*You are free to insert any literal text within the conversion pattern.*

Each conversion specifier starts with a percent sign (“%“) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification. The following is a simple example.

Let the conversion pattern be "%d{YYYY-MM-DD HH:mm:ss} [%-5p]: %m%n" and assume that the log4j environment was set to use a PatternLayout. Then the statements: ``` LOG debug Message 1 LOG warn Message 2 ```

would yield the output ``` 2016-01-09 14:59:30 [DEBUG] Message 1 2016-01-09 14:59:31 [WARN ] Message 2 ```

Note that there is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier %-5p means the priority of the logging event should be left justified to a width of five characters. The recognized conversion characters are

Conversion patterns

* “%d[{<dateFormat>}]“: Prints out the date of when the log event was created. Possible patterns:

  • Month
  • “M“: 1 2 ... 12
  • “MM“: 01 01 ... 12
  • “Mo“: 1st 2nd ... 12th
  • “MMM“: Jan Feb ... Dec
  • “MMMM“: January February ... December
  • Day of Month
  • “D“: 1 2 ... 31
  • “DD“: 01 02 ... 31
  • “Do“: 1st 2nd ... 31st
  • Day of Week
  • “ddd“: Sun Mon ... Sat
  • “dddd“: Sunday Monday ... Saturday
  • Year
  • “YY“: 70 71 ... 12
  • “YYYY“: 1970 1971 ... 2012
  • Hour
  • “H“: 0 1 2 ... 23
  • “HH“: 00 01 02 .. 23
  • “h“: 1 2 ... 12
  • “hh“: 01 02 ... 12
  • Minute
  • “m“: 0 1 2 ... 59
  • “mm“: 00 01 02 ... 59
  • Second
  • “s“: 0 1 2 ... 59
  • “ss“: 00 01 02 ... 59
  • AM / PM
  • “A“: AM PM
  • “a“: am pm
  • Timezone
  • “Z“: -07:00 -06:00 ... +07:00
  • “ZZ“: -0700 -0600 ... +0700

* “%m“: The log message. * “%c[{<maximumNumberOfElements>}]“: Holds the logging category. Normally instance is the name of the logger or the service. If you do not specify “maximumNumberOfElements“ the full name is displayed. If instance is for example “%c{2}“ and the name of the category is “a.b.c“ then the output result is “b.c“. * “%F[{<maximumNumberOfPathElements>}]“: Holds the source file that logs instance event. If you do not specify “maximumNumberOfPathElements“ the full file name is displayed. If instance is for example “%F{2}“ and the file name is “/a/b/c.go“ then the output result is “b/c.go“. * “%l“: Holds the source location of the log event. * “%L“: Holds the line number where the log event was created. * “%C[{<maximumNumberOfElements>}]“: Holds the source code package. If you do not specify “maximumNumberOfElements“ the full name is displayed. If instance is for example “%C{2}“ and the name of the package is “a.b.c“ then the output result is “b.c“. * “%M“: Holds the method name where the log event was created. * “%p“: Holds the priority or better called log level. * “%P[{<subFormatPattern>}]“: Stacktrace of the location where a problem was raised that caused instance log message. * “%r“: Uptime of the logger. * “%n“: Prints out a new line character. * “%%“: Prints out a “%“ character.

func (Pattern) CheckedString

func (instance Pattern) CheckedString() (string, error)

CheckedString is like String but return also an optional error if there are some validation errors.

func (Pattern) MarshalYAML

func (instance Pattern) MarshalYAML() (interface{}, error)

MarshalYAML is used until yaml marshalling. Do not call directly.

func (*Pattern) Set

func (instance *Pattern) Set(value string) error

Set the given string to current object from a string. Return an error object if there are some problems while transforming the string.

func (Pattern) String

func (instance Pattern) String() string

func (*Pattern) UnmarshalYAML

func (instance *Pattern) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is used until yaml unmarshalling. Do not call directly.

func (Pattern) Validate

func (instance Pattern) Validate() error

Validate do validate action on this object and return an error object if any.

type Writer

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

Writer represents a log writer. A writer of this type is synchronized and could be used from different threads and contexts.

func NewWriter added in v0.1.6

func NewWriter(filename values.String, writer *lumberjack.Logger) *Writer

NewWriter creates a new Write for given filename.

func (*Writer) Close

func (instance *Writer) Close()

Close closes this Writer and all of its resources. After this a usage is not longer possible.

func (*Writer) Write

func (instance *Writer) Write(what []byte, stderr bool) (int, error)

Write writes the given content to writer drain.

Jump to

Keyboard shortcuts

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