Documentation ¶
Overview ¶
Package sypl provides a Simple Yet Powerful Logger built on top of the Golang logger. A sypl logger can have many `Output`s, and each `Output` is responsible for writing to a specified destination. Each Output can have multiple `Processor`s, which run in isolation manipulating the log message. The order of execution is according to the registering order. The above features allow sypl to fit into many different logging flows and needs.
Index ¶
- Variables
- func OutputsNames(outputs []*Output) string
- func ProcessorsNames(processors []*Processor) string
- type Casing
- type Color
- type FileRotationOptions
- type Message
- func (m *Message) AddTags(tags ...string)
- func (m *Message) ContainTag(tag string) bool
- func (m *Message) DeleteTag(tag string)
- func (m *Message) GetFlag() flag.Flag
- func (m *Message) GetID() string
- func (m *Message) GetLevel() level.Level
- func (m *Message) GetOriginalContent() string
- func (m *Message) GetOutput() *Output
- func (m *Message) GetProcessedContent() string
- func (m *Message) GetProcessor() *Processor
- func (m *Message) GetSypl() *Sypl
- func (m *Message) GetTags() []string
- func (m *Message) GetTimestamp() time.Time
- func (m *Message) SetFlag(flag flag.Flag)
- func (m *Message) SetOutput(output *Output)
- func (m *Message) SetProcessedContent(content string)
- func (m *Message) SetProcessor(processor *Processor)
- func (m *Message) SetSypl(sypl *Sypl)
- type Options
- type Output
- func Console(maxLevel level.Level, processors ...*Processor) *Output
- func File(path string, maxLevel level.Level, processors ...*Processor) *Output
- func FileBased(name string, path string, maxLevel level.Level, writer io.Writer, ...) *Output
- func FileWithRotation(path string, maxLevel level.Level, options *FileRotationOptions, ...) *Output
- func NewOutput(name string, maxLevel level.Level, writer io.Writer, processors ...*Processor) *Output
- func (o *Output) AddProcessor(processor *Processor) *Output
- func (o *Output) GetBuiltinLogger() *builtin.Builtin
- func (o *Output) GetMaxLevel() level.Level
- func (o *Output) GetName() string
- func (o *Output) GetProcessor(i int) *Processor
- func (o *Output) GetProcessors() []*Processor
- func (o *Output) GetStatus() bool
- func (o *Output) SetStatus(status bool)
- type Printer
- type Processor
- func ChangeFirstCharCase(casing Casing) *Processor
- func ColorizeBasedOnLevel(levelColorMap map[level.Level]Color) *Processor
- func ColorizeBasedOnWord(wordColorMap map[string]Color) *Processor
- func EnableDisableOutputs(status bool, names ...string) *Processor
- func EnableDisableProcessors(status bool, names ...string) *Processor
- func ForceBasedOnLevel(levels ...level.Level) *Processor
- func MuteBasedOnLevel(levels ...level.Level) *Processor
- func NewProcessor(name string, processorFunc ProcessorFunc) *Processor
- func PrefixBasedOnMask(timestampFormat string) *Processor
- func PrefixBasedOnMaskExceptForLevels(timestampFormat string, levels ...level.Level) *Processor
- func Prefixer(prefix string) *Processor
- func Suffixer(suffix string) *Processor
- type ProcessorFunc
- type Sypl
- func (sypl *Sypl) AddOutput(output *Output) *Sypl
- func (sypl *Sypl) Debug(args ...interface{}) *Sypl
- func (sypl *Sypl) Debugf(format string, args ...interface{}) *Sypl
- func (sypl *Sypl) Debugln(args ...interface{}) *Sypl
- func (sypl *Sypl) Error(args ...interface{}) *Sypl
- func (sypl *Sypl) Errorf(format string, args ...interface{}) *Sypl
- func (sypl *Sypl) Errorln(args ...interface{}) *Sypl
- func (sypl *Sypl) Fatal(args ...interface{})
- func (sypl *Sypl) Fatalf(format string, args ...interface{})
- func (sypl *Sypl) Fatalln(args ...interface{})
- func (sypl *Sypl) GetName() string
- func (sypl *Sypl) GetOutput(i int) *Output
- func (sypl *Sypl) GetOutputs() []*Output
- func (sypl *Sypl) Info(args ...interface{}) *Sypl
- func (sypl *Sypl) Infof(format string, args ...interface{}) *Sypl
- func (sypl *Sypl) Infoln(args ...interface{}) *Sypl
- func (sypl *Sypl) Print(level level.Level, args ...interface{}) *Sypl
- func (sypl *Sypl) PrintPretty(data interface{})
- func (sypl *Sypl) PrintWithOptions(options *Options, level level.Level, args ...interface{}) *Sypl
- func (sypl *Sypl) Printf(level level.Level, format string, args ...interface{}) *Sypl
- func (sypl *Sypl) PrintfWithOptions(options *Options, level level.Level, format string, args ...interface{}) *Sypl
- func (sypl *Sypl) Println(level level.Level, args ...interface{}) *Sypl
- func (sypl *Sypl) PrintlnPretty(data interface{})
- func (sypl *Sypl) PrintlnWithOptions(options *Options, level level.Level, args ...interface{}) *Sypl
- func (sypl *Sypl) Trace(args ...interface{}) *Sypl
- func (sypl *Sypl) Tracef(format string, args ...interface{}) *Sypl
- func (sypl *Sypl) Traceln(args ...interface{}) *Sypl
- func (sypl *Sypl) Warn(args ...interface{}) *Sypl
- func (sypl *Sypl) Warnf(format string, args ...interface{}) *Sypl
- func (sypl *Sypl) Warnln(args ...interface{}) *Sypl
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Red = color.New(color.FgRed).SprintFunc() BoldRed = color.New(color.FgRed, color.Bold).SprintFunc() Green = color.New(color.FgGreen).SprintFunc() BoldGreen = color.New(color.FgGreen, color.Bold).SprintFunc() Yellow = color.New(color.FgYellow).SprintFunc() BoldYellow = color.New(color.FgYellow, color.Bold).SprintFunc() )
Built-in available colors.
Functions ¶
func OutputsNames ¶
OutputsNames extract the names of the given outputs.
func ProcessorsNames ¶ added in v1.1.0
ProcessorsNames extract the names of the given processors.
Types ¶
type FileRotationOptions ¶
type FileRotationOptions lumberjack.Logger
FileRotationOptions for the log file.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message envelops the content storing references of the `Logger`, `Output` and used `Processor`s. The original content is also stored, and can be used - but no changed by `Processor`s.
func NewMessage ¶ added in v1.1.0
func NewMessage(sypl *Sypl, output *Output, processor *Processor, level level.Level, ct string) *Message
NewMessage creates a new message.
func (*Message) ContainTag ¶ added in v1.1.0
ContainTag verifies if `tags` contains the specified `tag`.
func (*Message) GetOriginalContent ¶ added in v1.1.0
GetOriginalContent returns the original, non-modified content.
func (*Message) GetProcessedContent ¶ added in v1.1.0
GetProcessedContent returns the content to be processed.
func (*Message) GetProcessor ¶ added in v1.1.0
GetProcessor returns the message's `Processor`.
func (*Message) GetTimestamp ¶ added in v1.1.0
GetTimestamp returns the message's `timestamp`.
func (*Message) SetProcessedContent ¶ added in v1.1.0
SetProcessedContent sets the processed content.
func (*Message) SetProcessor ¶ added in v1.1.0
SetProcessor sets message's `Processor`.
type Options ¶ added in v1.1.0
type Options struct { // Flag define behaviors. Flag flag.Flag // OutputsNames name of the outputs that should be used to print. OutputsNames []string // ProcessorsNames name of the processors that should be used. ProcessorsNames []string // Tags are indicators consumed by `Output`s and `Processor`s. Tags []string }
Options extends `PrintX` capabilities.
func NewDefaultOptions ¶ added in v1.1.0
func NewDefaultOptions() *Options
NewDefaultOptions creates a new set of options base on default values.
type Output ¶
type Output struct {
// contains filtered or unexported fields
}
output defines and output that could be a console, a file, or anything that implements io.Writer.
Notes: - Any message with a `level` beyond `maxLevel` will not be written. - Messages are processed according to the order `processors` are added.
func FileBased ¶
func FileBased(name string, path string, maxLevel level.Level, writer io.Writer, processors ...*Processor) *Output
FileBased is a specialized `output` that outputs to a file. If the usual, and common used "-" is used, it will behave as a Console writing to stdout and named "-".
func FileWithRotation ¶
func FileWithRotation( path string, maxLevel level.Level, options *FileRotationOptions, processors ...*Processor, ) *Output
FileWithRotation is a specialized `output` that outputs to the specified file, with rotation.
func NewOutput ¶
func NewOutput(name string, maxLevel level.Level, writer io.Writer, processors ...*Processor) *Output
NewOutput creates a new `output`.
Notes: - The created `output` is enabled by default. - processors can be added here, or later using the `AddProcessor` method. - This method is chainable.
func (*Output) GetBuiltinLogger ¶ added in v1.1.0
GetBuiltinLogger returns the output's built-in logger.
func (*Output) GetMaxLevel ¶ added in v1.1.0
GetName returns the output's `name`.
func (*Output) GetProcessor ¶ added in v1.1.0
GetProcessor returns the specified processor by its index.
func (*Output) GetProcessors ¶ added in v1.1.0
GetProcessors returns registered processors.
type Printer ¶
type Printer interface { // Print prints the message. Print(level level.Level, args ...interface{}) *Sypl // Printf prints the message according with the specified format. Printf(level level.Level, format string, args ...interface{}) *Sypl // Print prints the message, also adding a new line and the end. Println(level level.Level, args ...interface{}) *Sypl // Fatal prints the message, and exit with os.Exit(1). Fatal(level level.Level, args ...interface{}) // Fatalf prints the message according with the specified format, and exit // with os.Exit(1). Fatalf(level level.Level, format string, args ...interface{}) // Fatalln prints the message, also adding a new line and the end, and exit // with os.Exit(1). Fatalln(level level.Level, args ...interface{}) }
Printer defines possible printers.
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor processes messages. `Processor`s are self-contained algorithms that run in isolation. Any error, should be properly handled, within the processor context itself, and not bubbled up. Don't need to handle cases where message has no content - it's already done, see `sypl.Process`.
func ChangeFirstCharCase ¶
ChangeFirstCharCase changes message content's first char to the specified case.
Notes: - `casing` because `case` is a reserved word. - Order matters! If this comes after another processor like the Prefixer, it will change the case of the first char of the Prefix mask, not the message content!
func ColorizeBasedOnLevel ¶
ColorizeBasedOnLevel colorize messages based on the specified levels.
func ColorizeBasedOnWord ¶
ColorizeBasedOnWord colorize a messages with the specified colors if a message contains a specific word.
func EnableDisableOutputs ¶
EnableDisableOutputs enables or disables the specified outputs.
Note: Order matters! Enabling or disabling an output that was already executed as no effect at all!
func EnableDisableProcessors ¶
EnableDisableProcessors enables or disables the specified processors.
Note: Order matters! Enabling or disabling a processor that was already executed as no effect at all!
func ForceBasedOnLevel ¶
ForceBasedOnLevel force messages to be printed based on the specified levels.
func MuteBasedOnLevel ¶
MuteBasedOnLevel mute messages based on the specified levels.
func NewProcessor ¶
func NewProcessor(name string, processorFunc ProcessorFunc) *Processor
NewProcessor creates a new `Processor`.
Notes: - The created `Processor` is enabled by default. - This method is chainable.
func PrefixBasedOnMask ¶
PrefixBasedOnMask prefixes messages with the predefined mask.
Example: 2021-06-22 12:51:46.089 [80819] [CLI] [Info].
func PrefixBasedOnMaskExceptForLevels ¶
PrefixBasedOnMaskExceptForLevels is a specialized version of the `PrefixBasedOnMask`. It prefixes all messages, except for the specified levels.
type ProcessorFunc ¶
type ProcessorFunc func(message *Message)
ProcessorFunc is the processor's `do` specification.
type Sypl ¶
type Sypl struct {
// contains filtered or unexported fields
}
sypl definition. It's able to print messages according to the definition of each `output`.
func New ¶
New creates a new custom logger.
Notes: Outputs can be added here, or later using the `AddOutput` method.
Example (Chained) ¶
Chained is the chained example of creating, and setting up a `sypl` logger. It writes to both `stdout` and `stderr`.
// Creates logger, and name it. New("Testing Logger"). // Creates two `Output`s. "Console" and "Error". "Console" will print to // `Fatal`, `Error`, and `Info`. "Error" will only print `Fatal`, and // `Error` levels. AddOutput(NewOutput("Console", level.Info, os.Stdout)). // Creates a `Processor`. It will prefix all messages. It will only // prefix messages for this specific `Output`, and @ `Error` level. AddOutput(NewOutput("Error", level.Error, os.Stderr). AddProcessor(func(prefix string) *Processor { return NewProcessor("Prefixer", func(message *Message) { if message.GetLevel() == level.Error { message.SetProcessedContent(prefix + message.GetProcessedContent()) } }) }(defaultPrefixValue))). // Prints: // Test info message Println(level.Info, "Test info message"). // Prints: // Test error message // My Prefix - Test error message Println(level.Error, "Test error message")
Output:
Example (ChainedUsingBuiltin) ¶
ChainedUsingBuiltin is the chained example of creating, and setting up a `sypl` logger using built-in `Output`, and `Processor`. It writes to `stdout`, and `stderr`.
// Creates logger, and name it. New("Testing Logger"). // Adds an `Output`. In this case, called "Console" that will print to // `stdout` and max print level @ `Info`. // // Adds a `Processor`. It will prefix all messages. AddOutput(Console(level.Info).AddProcessor(Prefixer(defaultPrefixValue))). // Prints: My Prefix - Test info message Infoln("Test info message")
Output: My Prefix - Test info message
Example (InlineUsingBuiltin) ¶
inlineUsingBuiltin same as `ChainedUsingBuiltin` but using inline form.
New("Testing Logger", Console(level.Info).AddProcessor(Prefixer(defaultPrefixValue))).Infoln("Test info message")
Output: My Prefix - Test info message
Example (NotChained) ¶
NonChained is a non-chained example of creating, and setting up a `sypl` logger. It writes to a custom buffer.
var buf bytes.Buffer bufWriter := bufio.NewWriter(&buf) // Creates logger, and name it. testingLogger := New("Testing Logger") // Creates an `Output`. In this case, called "Console" that will print to // `stdout` and max print level @ `Info`. ConsoleToStdOut := NewOutput("Console", level.Info, bufWriter) // Creates a `Processor`. It will prefix all messages. Prefixer := func(prefix string) *Processor { return NewProcessor("Prefixer", func(message *Message) { message.SetProcessedContent(prefix + message.GetProcessedContent()) }) } // Adds `Processor` to `Output`. ConsoleToStdOut.AddProcessor(Prefixer(defaultPrefixValue)) // Adds `Output` to logger. testingLogger.AddOutput(ConsoleToStdOut) // Writes: "My Prefix - Test message" testingLogger.Print(level.Info, "Test info message") bufWriter.Flush() fmt.Println(buf.String())
Output: My Prefix - Test info message
Example (PrintPretty) ¶
PrintPretty example.
New("Testing Logger", Console(level.Info)).PrintPretty(NewDefaultOptions())
Output: { "Flag": 0, "OutputsNames": [], "ProcessorsNames": [], "Tags": [] }
Example (PrintWithOptions) ¶
printWithOptions demonstrates `sypl` flexibility. `Options` enhances the usual `PrintX` methods allowing to specify flags, and tags.
// Creates logger, and name it. testingLogger := New("Testing Logger") // Creates 3 `Output`s, all called "Console" that will print to `stdout`, and // max print level @ `Info`. Console1ToStdOut := NewOutput("Console 1", level.Info, os.Stdout) Console2ToStdOut := NewOutput("Console 2", level.Info, os.Stdout) Console3ToStdOut := NewOutput("Console 3", level.Info, os.Stdout) // Creates a `Processor`. It will `prefix` all messages with the Output, and // Processor names. Prefixer := func() *Processor { return NewProcessor("Prefixer", func(message *Message) { prefix := fmt.Sprintf("Output: %s Processor: %s Content: ", message.GetOutput().GetName(), message.GetProcessor().GetName(), ) message.SetProcessedContent(prefix + message.GetProcessedContent()) }) } // Creates a `Processor`. It will `suffix` all messages with the specified // `tag`. SuffixBasedOnTag := func(tag string) *Processor { return NewProcessor("SuffixBasedOnTag", func(message *Message) { if message.ContainTag(tag) { message.SetProcessedContent(message.GetProcessedContent() + " - My Suffix") } }) } // Adds `Processor`s to `Output`s. Console1ToStdOut. AddProcessor(Prefixer()). AddProcessor(SuffixBasedOnTag("SuffixIt")) Console2ToStdOut. AddProcessor(Prefixer()). AddProcessor(SuffixBasedOnTag("SuffixIt")) Console3ToStdOut. AddProcessor(Prefixer()). AddProcessor(SuffixBasedOnTag("SuffixIt")) // Adds all `Output`s to logger. testingLogger. AddOutput(Console1ToStdOut). AddOutput(Console2ToStdOut). AddOutput(Console3ToStdOut) // Prints with prefix, without suffix. testingLogger.Println(level.Info, defaultContentOutput) // Prints with prefix, and suffix. testingLogger.PrintWithOptions(&Options{ OutputsNames: []string{"Console 1"}, ProcessorsNames: []string{"Prefixer", "SuffixBasedOnTag"}, Tags: []string{"SuffixIt"}, }, level.Info, defaultContentOutput) // output: // Output: Console 1 Processor: Prefixer Content: contentTest // Output: Console 2 Processor: Prefixer Content: contentTest // Output: Console 3 Processor: Prefixer Content: contentTest
Output: Output: Console 1 Processor: Prefixer Content: contentTest Output: Console 2 Processor: Prefixer Content: contentTest Output: Console 3 Processor: Prefixer Content: contentTest Output: Console 1 Processor: Prefixer Content: contentTest - My Suffix
func (*Sypl) Debugf ¶
Debugf prints the message (if has content) according with the specified format @ the Debug level.
func (*Sypl) Debugln ¶
Debugln prints the message (if has content), also adding a new line to the end @ the Debug level.
func (*Sypl) Errorf ¶
Errorf prints the message (if has content) according with the specified format @ the Error level.
func (*Sypl) Errorln ¶
Errorln prints the message (if has content), also adding a new line to the end @ the Error level.
func (*Sypl) Fatal ¶
func (sypl *Sypl) Fatal(args ...interface{})
Fatal prints the message (if has content), and exit with os.Exit(1).
func (*Sypl) Fatalf ¶
Fatalf prints the message (if has content) according with the specified format, and exit with os.Exit(1).
func (*Sypl) Fatalln ¶
func (sypl *Sypl) Fatalln(args ...interface{})
Fatalln prints the message (if has content), also adding a new line and the end, and exit with os.Exit(1).
func (*Sypl) GetOutputs ¶ added in v1.1.0
GetOutputs returns registered outputs.
func (*Sypl) Infof ¶
Infof prints the message (if has content) according with the specified format @ the Info level.
func (*Sypl) Infoln ¶
Infoln prints the message (if has content), also adding a new line to the end @ the Info level.
func (*Sypl) PrintPretty ¶ added in v1.1.1
func (sypl *Sypl) PrintPretty(data interface{})
PrintPretty prints data structures as JSON text.
func (*Sypl) PrintWithOptions ¶ added in v1.1.0
PrintWithOptions prints the message (if has content), with `Options` to the specified outputs.
func (*Sypl) Printf ¶
Printf prints the message (if has content) according with the specified format.
func (*Sypl) PrintfWithOptions ¶ added in v1.1.0
func (sypl *Sypl) PrintfWithOptions(options *Options, level level.Level, format string, args ...interface{}) *Sypl
PrintfWithOptions prints the message (if has content), with `Options` according with the specified format, and to the specified outputs.
func (*Sypl) Println ¶
Println prints the message (if has content), also adding a new line to the end.
func (*Sypl) PrintlnPretty ¶ added in v1.1.1
func (sypl *Sypl) PrintlnPretty(data interface{})
PrintlnPretty prints data structures as JSON text, also adding a new line to the end. Its content will be printed regardless of the level, and it isn't processed.
func (*Sypl) PrintlnWithOptions ¶ added in v1.1.0
func (sypl *Sypl) PrintlnWithOptions(options *Options, level level.Level, args ...interface{}) *Sypl
PrintlnWithOptions prints the message (if has content), with `Options`, also adding a new line to the end, and to the specified outputs.
func (*Sypl) Tracef ¶
Tracef prints the message (if has content) according with the specified format @ the Trace level.
func (*Sypl) Traceln ¶
Traceln prints the message (if has content), also adding a new line to the end @ the Trace level.