Skip to content

Simplified composite literal for creating structured loggers, leveraging the slog package.

Notifications You must be signed in to change notification settings

radias-holdings/logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logger Package

The logger package provides a simplified composite literal for creating structured loggers, leveraging the slog package.

NewLogger

This is the primary function in this package. The NewLogger function takes an io.Writer (optional), and the LOG_LEVEL environment varible (optional). It returns a new logger instance from the slog package.

func NewLogger(writer io.Writer, level ...string) (logger *slog.Logger)

Parameters

  • writer: An io.Writer where the log output will be written. This is optional, and if not provided, the logger will default to writing to os.Stdout.
  • level: A string (os.Getenv("LOG_LEVEL")) which sets the logging level for the logger. This is optional. If not provided, it defaults to the error logging level - slog.LevelError.

Logging Levels

The logging levels used are:

  • DEBUG = -4
  • INFO = 0
  • WARN = 4
  • ERROR = 8
  • FATAL = 12 (custom log level)

If a message is logged at a level of ERROR+4, it is output as "FATAL" in the log.

Usage

Use of the logging package should be used at the top level function such as within main. Functions which return errors should remain

To create a logger with default parameters (writing to os.Stdout at ERROR level and above):

log := logger.NewLogger(nil, os.Getenv("LOG_LEVEL"))
...

// Debugging
var1, var2, var3, err := exampleFunction()
log.Debug("exampleFunction has run", "var1", var1, "var2", var2, "var3", var3)

// Information
_, err := exampleFunction()
log.Info("exampleFunction has run")

// Warning
diskSpace, err := diskSpaceChecker()
if diskSpace >= 80 {
	log.Warn("disk space is over 80%")
}

// Error (default level)
_, err := exampleFunction()
	if err != nil {
		log.Error("describe action here", "rsp", err)
	}

// Fatal
ctx := context.Background()
...
_, err := exampleFunction2()
	if err != nil {
		log.Log(ctx, logger.Fatal, "describe action here", "rsp", err)
		os.Exit(1)
	}

Outputs:

time=2023-07-20T16:54:40.558+10:00 level=DEBUG msg="exampleFunction has run" var1="something" var2="something" etc.
time=2023-07-20T16:54:40.558+10:00 level=INFO msg="exampleFunction has run"
time=2023-07-20T16:54:40.558+10:00 level=WARN msg="disk space is over 80%"
time=2023-07-20T16:54:40.558+10:00 level=ERROR msg="describe action here" rsp="error: something in exampleFunction went wrong"
time=2023-07-20T16:54:40.558+10:00 level=FATAL msg="describe action here" rsp="error: something in exampleFunction2 went very wrong"
exit status 1

To create a logger writing to a provided io.Writer at the ERROR level (this is mainly for testing the output stream):

buf := new(bytes.Buffer)
logger := NewLogger(buf)

License

MIT License

Copyright (c) 2024 Radias Holdings

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Simplified composite literal for creating structured loggers, leveraging the slog package.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages