slogger

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2022 License: MIT Imports: 4 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

slogger Go Report Card

  .--.--.     ,--,                                                      
 /  /    '. ,--.'|                                                      
|  :  /`. / |  | :     ,---.                                    __  ,-. 
;  |  |--`  :  : '    '   ,'\   ,----._,.  ,----._,.          ,' ,'/ /| 
|  :  ;_    |  ' |   /   /   | /   /  ' / /   /  ' /   ,---.  '  | |' | 
 \  \    `. '  | |  .   ; ,. :|   :     ||   :     |  /     \ |  |   ,' 
  `----.   \|  | :  '   | |: :|   | .\  .|   | .\  . /    /  |'  :  /   
  __ \  \  |'  : |__'   | .; :.   ; ';  |.   ; ';  |.    ' / ||  | '    
 /  /`--'  /|  | '.'|   :    |'   .   . |'   .   . |'   ;   /|;  : |    
'--'.     / ;  :    ;\   \  /  `---`-'| | `---`-'| |'   |  / ||  , ;    
  `--'---'  |  ,   /  `----'   .'__/\_: | .'__/\_: ||   :    | ---'     
             ---`-'            |   :    : |   :    : \   \  /           
                                \   \  /   \   \  /   `----'            
                                 `--`-'     `--`-'                      

The logger library using the experimental Golang slog package

Usage:

  • The package was designed to be a simple interface over the slog package extracing out some if the discoverability features and crafting a simple, easy logger.
  • creating a new logger with NewLogger function. The input is an io.Writer, so stdout or a file work (any any io.Writer implementation)
  • All variadic values passed to the LogEvent/LogError functions will be treated as key value pairs, any additional values, that are not mapped to their preceeding values, are added as "misc" fields to the JSON return.
Example Usage:
  • create a logger with slogger.NewLogger(os.Stdout)
  • or log to a file:
f, _ := os.OpenFile(testFile, os.O_RDWR|os.O_TRUNC, os.ModeAppend)
log := slogger.NewLogger(f)
stdoutLogger.LogEvent("info", "Send data to file") 
  • slogger will accept any number of arbitrary elements and create a json structure from the arguments to the LogEvent functions.
  • i.e.
stdoutLogger.LogEvent("info", "Something", "one", "two", "Another", false, "four", true)

output:

stdoutLogger.LogEvent("info", "Something four", "one", "two", "Another", false, "four", true)
  • You can also pass a map to the LogEvent functions as unput:
stdoutLogger.LogEvent("info", "Something four", map[string]any{"one": false})

output:

{"time":"2022-12-27T19:19:22.711393515-08:00","level":"INFO","msg":"Something four","one":false}
  • You can even pass a combination of map strings:
stdoutLogger.LogEvent("warn", "aaaa", "key", "value", "AnotherKey", false, "four", 123123, map[string]any{"testOne": 42069, "testTwo": false})

output:

stdoutLogger.LogEvent("warn", "aaaa", "key", "value", "AnotherKey", false, "four", 123123, map[string]any{"testOne": 42069, "testTwo": false})
  • Example of Misc fields:
{"time":"2022-12-27T18:56:27.543409183-08:00","level":"INFO","msg":"Something four","one":"two","Another":false,"four":true,"miscFields":"bob"}

Output:

  • Example test output:
{"time":"2022-12-27T18:56:27.543409183-08:00","level":"INFO","msg":"Something four","one":"two","Another":false,"four":true,"miscFields":"bob"}
{"time":"2022-12-27T18:56:27.543564927-08:00","level":"INFO","msg":"Something four","one":"two","Another":false,"four":true}
{"time":"2022-12-27T18:56:27.543572397-08:00","level":"INFO","msg":"Something four","one":false,"Another":false,"four":true}
{"time":"2022-12-27T18:56:27.543577644-08:00","level":"INFO","msg":"A thing","key":"value","AnotherKey":false,"four":123123,"more":"","MORETEST":123123,"TestAgain":false,"miscFields":123123}
{"time":"2022-12-27T18:56:27.543583676-08:00","level":"WARN","msg":"aaaa","AnotherKey":false,"four":123123,"testOne":42069,"testTwo":false,"key":"value"}
{"time":"2022-12-27T18:56:27.543587977-08:00","level":"ERROR","msg":"error","err":"something died"}
{"time":"2022-12-27T18:56:27.54359305-08:00","level":"ERROR","msg":"error","1":2,"3":"masdasd","err":"something died"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arguments

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

func NewArgumentMapper

func NewArgumentMapper(args []any) *Arguments

NewArgumentMapper creates a pointer to Arguments struct which is processed and passed into the logger

func (*Arguments) SetMap

func (a *Arguments) SetMap(value map[string]any) *Arguments

type Logger

type Logger struct {
	Logger *slog.Logger
}

func NewLogger

func NewLogger(f io.Writer) *Logger

Generic logger wrapper to log events as JSON to a file, stdout, or any io.Writer implementation

func (*Logger) LogError

func (p *Logger) LogError(msg string, err error, args ...any) error

Logs error messages

func (*Logger) LogEvent

func (p *Logger) LogEvent(logType string, msg string, args ...any)

LogEvent will log a given event with, or without arguments Will accept a map or variadic array of strings

Jump to

Keyboard shortcuts

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