logging

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Portions of this file are derived from the slog-leveler project (https://github.com/shashankram/slog-leveler) which is licensed under the MIT License.

MIT License

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.

Portions of this file are derived from the slog-leveler project (https://github.com/shashankram/slog-leveler) which is licensed under the MIT License.

MIT License

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.

Portions of this file are derived from the slog-leveler project (https://github.com/shashankram/slog-leveler) which is licensed under the MIT License.

MIT License

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.

Index

Constants

View Source
const (
	DefaultComponent = "default"
)
View Source
const (
	LevelTrace = slog.Level(-5) // 1 lower than slog.LevelDebug
)

Extra slog log levels

Variables

This section is empty.

Functions

func DeleteLeveler

func DeleteLeveler(component string) error

DeleteLeveler deletes the leveler instance for the given component

func GetComponentLevels

func GetComponentLevels() map[string]slog.Level

GetComponentLevels returns a map of component names to their respective slog.Level

func GetLevel

func GetLevel(component string) (slog.Level, error)

GetLevel returns the current log level for the component

func HTTPLevelHandler

func HTTPLevelHandler(w http.ResponseWriter, r *http.Request)

HTTPLevelHandler handles HTTP requests to the log level of the default or component specific loggers.

GET returns the current log levels of all components.

POST/PUT with query parameters updates log levels:

  • level=<level>: updates log level across all component loggers
  • <component>=<level>&<component>=<level2>...: updates log level for specific components

POST/PUT without query parameters returns 400.

func LevelToString

func LevelToString(level slog.Level) string

LevelToString returns the string representation of slog.Level

func LocalhostOnly

func LocalhostOnly(next http.HandlerFunc) http.HandlerFunc

LocalhostOnly wraps an http.HandlerFunc to only allow requests from localhost

func MustGetLevel

func MustGetLevel(component string) slog.Level

MustGetLevel returns the current log level for the component or panics if the component is not found

func MustSetLevel

func MustSetLevel(component string, level slog.Level)

MustSetLevel sets the log level for the component or panics if the component is not found

func New

func New(component string) *slog.Logger

New returns a new slog.Logger instance for the given component with default Options. If the component is empty, it returns the default logger. NOTE: the logger's leveler is tracked in memory, so this should be used to create long lived loggers whose log levels are required to be configurable at runtime. Short lived loggers should be instantiated using logger.With() or call DeleteLeveler if instantiated with New, to avoid leaking memory.

func NewWithOptions

func NewWithOptions(component string, opts Options) *slog.Logger

NewWithOptions returns a new slog.Logger instance for the given component with the provided Options If the component is empty, it returns the default logger. NOTE: the logger's leveler is tracked in memory, so this should be used to create long lived loggers whose log levels are required to be configurable at runtime. Short lived loggers should be instantiated using logger.With() or call DeleteLeveler if instantiated with NewWithOptions, to avoid leaking memory.

func ParseLevel

func ParseLevel(level string) (slog.Level, error)

ParseLevel parses the given level string to slog.Level, and returns an error if the level is unknown

func Reset

func Reset(level slog.Level)

Reset resets the log level for all components to the given level

func SetLevel

func SetLevel(component string, level slog.Level) error

SetLevel sets the log level for the component

func SetupDefault

func SetupDefault()

SetupDefault creates and sets the default slog logger for the DefaultComponent. This must be called early in application startup (e.g. in main or server init).

Types

type LogFormat

type LogFormat string

LogFormat represents the format of the log output

const (
	// TextFormat represents plain text format
	TextFormat LogFormat = "text"

	// JSONFormat represents JSON format
	JSONFormat LogFormat = "json"
)

type Options

type Options struct {
	// Logger level
	Level *slog.Level

	// Log format: text or json
	Format LogFormat

	// Writer to write logs to
	Writer io.Writer

	// AddSource adds the source code position of the log statement to the output
	AddSource bool
}

Options to configure the logger

func (*Options) Default

func (o *Options) Default()

Default sets default values on Options

Jump to

Keyboard shortcuts

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