logger

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 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 uses to log error messages. All levels below are ignored.
	Level Level `json:"level" yaml:"level"`

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

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

	// @default "console"
	//
	// Target file of the logger. The file will be created if it does not exist - but not the parent directory.
	//
	// If the 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} is 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} is 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} is 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) UnmarshalYAML added in v1.0.0

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

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

func (Config) Validate

func (instance Config) Validate() error

Validate validates action on this object and return an error object if there are 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 purposes. This level is only required if 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, this instance level is used.
	Warning Level = 400

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

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

func (Level) CheckedString

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

CheckedString is like String but also returns an optional error if there are any validation errors.

func (Level) DisplayForLogging

func (instance Level) DisplayForLogging() string

DisplayForLogging returns a string that can 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 this method directly.

func (Level) MarshalYAML

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

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

func (*Level) Set

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

Set the given string to the current object from a string. Returns an error object if there are any 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 this method directly.

func (*Level) UnmarshalYAML

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

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

func (Level) Validate

func (instance Level) Validate() error

Validate validates actions on this object and returns an error object if there 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 closes 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 the 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 redirect every output to a logger.

func (*Logger) Stderr

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

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

func (*Logger) Stdout

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

Stdout creates a writer to 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.

If the conversion pattern is "%d{YYYY-MM-DD HH:mm:ss} [%-5p]: %m%n" and the log4j environment has been set to use a PatternLayout. Then the statement will be: ``` LOG debug Message 1 LOG warn Message 2 ```

and 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 log's creation date. Possible patterns are:

  • 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 the instance is the name of the logger or the service. If you do not specify “maximumNumberOfElements“ the full name is displayed. For example, if the instance is “%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 the instance event. If you do not specify “maximumNumberOfPathElements“ the full file name is displayed. For example, if the instance is “%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. For example, if the instance is “%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 the 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 also returns an optional error if there are any validation errors.

func (Pattern) MarshalYAML

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

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

func (*Pattern) Set

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

Set the given string to current object from a string. Returns an error object if there are any 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 this method directly.

func (Pattern) Validate

func (instance Pattern) Validate() error

Validate validates action on this object and returns an error object if there are 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 the given file name.

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 the writer drain.

Jump to

Keyboard shortcuts

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