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 ¶
Copyright (c) 2025 Shashank Ram ¶
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 ¶
Copyright (c) 2025 Shashank Ram ¶
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 ¶
Copyright (c) 2025 Shashank Ram ¶
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
- func DeleteLeveler(component string) error
- func GetComponentLevels() map[string]slog.Level
- func GetLevel(component string) (slog.Level, error)
- func HTTPLevelHandler(w http.ResponseWriter, r *http.Request)
- func LevelToString(level slog.Level) string
- func LocalhostOnly(next http.HandlerFunc) http.HandlerFunc
- func MustGetLevel(component string) slog.Level
- func MustSetLevel(component string, level slog.Level)
- func New(component string) *slog.Logger
- func NewWithOptions(component string, opts Options) *slog.Logger
- func ParseLevel(level string) (slog.Level, error)
- func Reset(level slog.Level)
- func SetLevel(component string, level slog.Level) error
- func SetupDefault()
- type LogFormat
- type Options
Constants ¶
const (
DefaultComponent = "default"
)
const (
LevelTrace = slog.Level(-5) // 1 lower than slog.LevelDebug
)
Extra slog log levels
Variables ¶
This section is empty.
Functions ¶
func DeleteLeveler ¶
DeleteLeveler deletes the leveler instance for the given component
func GetComponentLevels ¶
GetComponentLevels returns a map of component names to their respective slog.Level
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 ¶
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 ¶
MustGetLevel returns the current log level for the component or panics if the component is not found
func MustSetLevel ¶
MustSetLevel sets the log level for the component or panics if the component is not found
func New ¶
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 ¶
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 ¶
ParseLevel parses the given level string to slog.Level, and returns an error if the level is unknown
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).