logs

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package out uses the slog and tint packages for the application logs. There are two logging modes, development and production. The production mode saves the logs to file and automatically rotates older files. While the development mode prints all feedback to stdout.

Index

Constants

View Source
const (
	// NameErr is the filename of the Error and Fatal levels log.
	NameErr = pname + "_error.json.log"
	// NameInfo is the filename of the Warning and Info level log.
	NameInfo = pname + "_info.json.log"
	// NameDebug is the filename of the Debug level log.
	NameDebug = pname + "_debug.json.log"
)
View Source
const (
	LevelDebug   = slog.LevelDebug
	LevelInfo    = slog.LevelInfo
	LevelWarning = slog.LevelWarn
	LevelError   = slog.LevelError
	LevelFatal   = slog.Level(12) // A more serious ERROR that aborts the application.
)
View Source
const (
	Ldate      = 1 << iota // the date as year/month/day: 2009/01/23
	Ltime                  // the time using US syntax: 1:23PM
	Lseconds               // the time using 24-hour syntax: 13:23:23. overrides Ltime
	Llongfile              // full file name and line number: /a/b/c/d.go:23
	Lshortfile             // final file name element and line number: d.go:23. overrides Llongfile
	Lcolor                 // use color output by providing ansi escape codes
	Lstdout                // output to the standard output (stdout)
	Lstderr                // output to the standard error (stderr)
	FlagAttr               // an internal flag to toggle a custom output for the environment configurations
)
View Source
const (
	// Quiets flag for the config.Quiet toggle that outputs to stderr but without color or a source file.
	Quiets = Ltime | Lstderr
	// Defaults flag for the Default slog logger that outputs to stderr and includes the time and source filename.
	Defaults = Lcolor | Lseconds | Lshortfile | Lstderr
	// Configurations flag for the Config.Print method to style the log output with a time but no source file.
	Configurations = Lcolor | Lstdout | Ltime | FlagAttr
	// Flags flag for the flags package to style the log output without the date and time.
	Flags = Lcolor | Lstdout | FlagAttr

	ErrorRed    = 9
	FatalRed    = 196 // #ff0000
	DebugPurple = 206 // #ff5fff
)

Variables

This section is empty.

Functions

func Color

func Color(w io.Writer) bool

Color returns true when the w io writer is an os.file type and its file descriptor is a terminal.

func Default

func Default() *slog.Logger

Default is a general logger intended for use before the configurations environment variables have been read and parsed.

It prints all log levels including debugging to the stdout and does not write to any files.

func Discard

func Discard() *slog.Logger

Discard is a logger intended for use with tests and discards all log output.

func Fatal

func Fatal(sl *slog.Logger, msg string, args ...slog.Attr)

Fatal logs any issues and exits to the operating system.

func FatalTx

func FatalTx(ctx context.Context, sl *slog.Logger, msg string, args ...slog.Attr)

FatalTx logs any issues and exits to the operating system.

Types

type Files

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

Files is used to write log files to multiple locations in parallel. The primary use of this is to log the different error types into separate files and to also permit writing to files and the terminal at the same time.

func NoFiles

func NoFiles() Files

NoFiles returns an empty Files struct and is available to show usage and intention.

func OpenFiles

func OpenFiles(root string, ename, iname, dname string) (Files, error)

OpenFiles creates or opens the named log files for use with the Files.New method. Multiple files can be opened together and all files must closed a after use using the Files.Close method.

  • ename will be used to write fatal and error reports.
  • iname will be used to write fatal, error, warnings and info reports.
  • dname will be used to write all reports including debug level reports.

The root should be the named directory to store the logs. If root is left empty the home directory of the user account will be used.

If any errors occur they will be returned as a wrapped error and must be handled appropriately.

func (Files) Close

func (f Files) Close() error

Close the open file descriptors in use by Files. Any errors will be joined and returned.

func (Files) New

func (f Files) New(stdmin slog.Level, flag int) *slog.Logger

New creates a slog logger that can write to multiple writers. The stdmin slog level can be used to limit the minimum log level for the stdout and stderr loggers. The flag int is used to configure the stdout and stderr logging.

Opened files descriptors can be provided using the Files struct and should be closed after use. Multiple open files can be used for different log levels. For example, you can use the Files.errolevel descriptor to save only errors and fatal issues. While using infolevel to also log info, warning, errors and fatal to another file.

File descriptors ignore the provided stdmin slog level.

Jump to

Keyboard shortcuts

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