Documentation ¶
Overview ¶
Package spyl 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 IsValidLevelFromInt(level int) bool
- func LevelsToString(levels []Level) string
- func OutputsNames(outputs []*Output) string
- type Casing
- type Color
- type FileRotationOptions
- type Level
- type Message
- type Output
- func Console(maxLevel Level, processors ...*Processor) *Output
- func File(path string, maxLevel Level, processors ...*Processor) *Output
- func FileBased(name string, path string, maxLevel Level, writer io.Writer, ...) *Output
- func FileWithRotation(path string, maxLevel Level, options *FileRotationOptions, ...) *Output
- func NewOutput(name string, maxLevel Level, writer io.Writer, processors ...*Processor) *Output
- type Printer
- type Processor
- func ChangeFirstCharCase(casing Casing) *Processor
- func ColorizeBasedOnLevel(levelColorMap map[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) *Processor
- func MuteBasedOnLevel(levels ...Level) *Processor
- func NewProcessor(name string, processorFunc ProcessorFunc) *Processor
- func PrefixBasedOnMask(timestampFormat string) *Processor
- func PrefixBasedOnMaskExceptForLevels(timestampFormat string, levels ...Level) *Processor
- func Prefixer(prefix string) *Processor
- func Suffixer(suffix string) *Processor
- type ProcessorFunc
- type Sypl
- func (cL *Sypl) AddOutput(output *Output) *Sypl
- func (cL *Sypl) Debug(args ...interface{}) *Sypl
- func (cL *Sypl) Debugf(format string, args ...interface{}) *Sypl
- func (cL *Sypl) Debugln(args ...interface{}) *Sypl
- func (cL *Sypl) Error(args ...interface{}) *Sypl
- func (cL *Sypl) Errorf(format string, args ...interface{}) *Sypl
- func (cL *Sypl) Errorln(args ...interface{}) *Sypl
- func (cL *Sypl) Fatal(args ...interface{})
- func (cL *Sypl) Fatalf(format string, args ...interface{})
- func (cL *Sypl) Fatalln(args ...interface{})
- func (cL *Sypl) Info(args ...interface{}) *Sypl
- func (cL *Sypl) Infof(format string, args ...interface{}) *Sypl
- func (cL *Sypl) Infoln(args ...interface{}) *Sypl
- func (cL *Sypl) Print(level Level, args ...interface{}) *Sypl
- func (cL *Sypl) PrintByOutput(outputsNames []string, level Level, args ...interface{}) *Sypl
- func (cL *Sypl) Printf(level Level, format string, args ...interface{}) *Sypl
- func (cL *Sypl) PrintfByOutput(outputsNames []string, level Level, format string, args ...interface{}) *Sypl
- func (cL *Sypl) Println(level Level, args ...interface{}) *Sypl
- func (cL *Sypl) PrintlnByOutput(outputsNames []string, level Level, args ...interface{}) *Sypl
- func (cL *Sypl) Trace(args ...interface{}) *Sypl
- func (cL *Sypl) Tracef(format string, args ...interface{}) *Sypl
- func (cL *Sypl) Traceln(args ...interface{}) *Sypl
- func (cL *Sypl) Warn(args ...interface{}) *Sypl
- func (cL *Sypl) Warnf(format string, args ...interface{}) *Sypl
- func (cL *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 IsValidLevelFromInt ¶
IsValidLevelFromInt validates if a given level - integer format, is valid.
func LevelsToString ¶
LevelsToString converts a slice of levels to string (concatenated).
func OutputsNames ¶
OutputsNames extract the names of the given outputs.
Types ¶
type FileRotationOptions ¶
type FileRotationOptions lumberjack.Logger
FileRotationOptions for the log file.
type Level ¶
type Level int
Level specification.
func LevelFromInt ¶
LevelFromInt return a `Level` from a given integer.
Notes: - It'll be automatically validated. - It'll exit if it's invalid.
func LevelFromString ¶
LevelFromInt return a `Level` from a given integer.
Note: It'll exit if it's invalid.
func MustBeValidFromInt ¶
MustBeValidFromInt validates the given `level`. It'll exit if it's invalid. If `level` is valid, it returns the respective `Level`.
type Message ¶
type Message struct { ContentOriginal string ContentProcessed string Level Level // 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.
type Output ¶
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, 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, options *FileRotationOptions, processors ...*Processor, ) *Output
FileWithRotation is a specialized `output` that outputs to the specified file, with rotation.
func NewOutput ¶
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.
type Printer ¶
type Printer interface { // Print prints the message. Print(level Level, args ...interface{}) *Sypl // Printf prints the message according with the specified format. Printf(level Level, format string, args ...interface{}) *Sypl // Print prints the message, also adding a new line and the end. Println(level Level, args ...interface{}) *Sypl // Fatal prints the message, and exit with os.Exit(1). Fatal(level Level, args ...interface{}) // Fatalf prints the message according with the specified format, and exit // with os.Exit(1). Fatalf(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, 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 specified 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", 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", ERROR, os.Stderr). AddProcessor(func(prefix string) *Processor { return NewProcessor("Prefixer", func(message *Message) { if message.Level == ERROR { message.ContentProcessed = prefix + message.ContentProcessed } }) }(defaultPrefixValue))). // Prints: // Test info message Println(INFO, "Test info message"). // Prints: // Test error message // My Prefix - Test error message Println(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(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(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", INFO, bufWriter) // Creates a `Processor`. It will prefix all messages. Prefixer := func(prefix string) *Processor { return NewProcessor("Prefixer", func(message *Message) { message.ContentProcessed = prefix + message.ContentProcessed }) } // Adds `Processor` to `Output`. ConsoleToStdOut.AddProcessor(Prefixer(defaultPrefixValue)) // Adds `Output` to logger. testingLogger.AddOutput(ConsoleToStdOut) // Prints: "My Prefix - Test message" testingLogger.Print(INFO, "Test info message") bufWriter.Flush() fmt.Println(buf.String())
Output: My Prefix - Test info message
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 (cL *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 (cL *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) 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) PrintByOutput ¶
PrintByOutput prints the message (if has content) to the specified outputs.
func (*Sypl) Printf ¶
Printf prints the message (if has content) according with the specified format.
func (*Sypl) PrintfByOutput ¶
func (cL *Sypl) PrintfByOutput(outputsNames []string, level Level, format string, args ...interface{}) *Sypl
PrintfByOutput prints the message (if has content) 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) PrintlnByOutput ¶
PrintlnByOutput prints the message (if has content), 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.