Documentation
¶
Overview ¶
Package log provides structured logging helpers built on top of log/slog.
The package adds source metadata to every record, stack traces to debug records, and OpenTelemetry integration through the otel subpackage.
Installation ¶
Run command in terminal:
go get github.com/jgolang/log
Quick Start ¶
This is a simple example of how the package is implemented with a basic function.
package main
import "github.com/jgolang/log"
func main(){
log.Info("My info....")
}
Output:
{"time":"2026-05-02T10:00:00Z","level":"INFO","msg":"My info....","source":{"func":"main","file":"main.go","line":6}}
Configuration ¶
You can configure the current level and handler programmatically:
log.SetLevel(slog.LevelWarn) log.NewTextHandler() log.SetSource(true) log.SetDebugStackTrace(false)
The package does not depend on environment variables, and debug stack traces are opt-in.
The otel subpackage disables baggage logging by default. If baggage is enabled, prefer allow-lists or explicit filters so sensitive context values are not written to logs accidentally.
You can also build isolated logger instances:
logger := log.New(
log.WithLevel(slog.LevelInfo),
log.WithTextHandler(os.Stdout),
log.WithSource(true),
log.WithDebugStackTrace(false),
)
logger.Info("service started")
Index ¶
- func Debug(args ...any)
- func DebugC(ctx context.Context, args ...any)
- func Error(args ...any)
- func ErrorC(ctx context.Context, args ...interface{})
- func Fatal(args ...any)
- func FatalC(ctx context.Context, args ...any)
- func Info(args ...interface{})
- func InfoC(ctx context.Context, args ...interface{})
- func Level() slog.Level
- func NewJSONHandler()
- func NewTextHandler()
- func Panic(args ...any)
- func PanicC(ctx context.Context, args ...any)
- func Print(args ...interface{})
- func PrintC(ctx context.Context, args ...interface{})
- func SetCalldepth(calldepth int)
- func SetDebugStackTrace(enabled bool)
- func SetLevel(newLevel slog.Level) (oldLevel slog.Level)
- func SetSource(enabled bool)
- func StackTrace() slog.Attr
- func Warn(args ...any)
- func WarnC(ctx context.Context, args ...interface{})
- type Logger
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(args ...any)
Debug logs a debug-level message using the global logger. msg: The message to log. args: Additional arguments to format the message.
func DebugC ¶ added in v1.3.0
DebugC logs a debug-level message with context using the global logger. ctx: The context for the log entry. msg: The message to log. args: Additional arguments to format the message.
func ErrorC ¶ added in v1.3.0
ErrorC logs an error-level message with context using the global logger. ctx: The context for the log entry..
func Fatal ¶
func Fatal(args ...any)
Fatal logs a fatal-level message using the global logger and then calls os.Exit(1).
func FatalC ¶ added in v1.3.0
FatalC logs a fatal-level message with context using the global logger and then calls os.Exit(1). ctx: The context for the log entry.
func Info ¶
func Info(args ...interface{})
Example (Info) ¶
package main
import (
"github.com/jgolang/log"
)
func main() {
// Use this function to see the trace of execution of the sentence.
// This function is useful for tracking where errors are generated.
log.Info("Hello world!")
}
Output:
func NewJSONHandler ¶ added in v1.3.0
func NewJSONHandler()
func NewTextHandler ¶ added in v1.3.0
func NewTextHandler()
func Panic ¶
func Panic(args ...any)
Panic logs a panic-level message using the global logger and then panics.
func PanicC ¶ added in v1.3.0
PanicC logs a panic-level message with context using the global logger and then panics. ctx: The context for the log entry.
func SetCalldepth ¶ added in v1.1.2
func SetCalldepth(calldepth int)
SetCalldepth configure the number of stack frames to ascend, with 0 identifying the caller of Caller for default loggin
func SetDebugStackTrace ¶ added in v1.4.0
func SetDebugStackTrace(enabled bool)
SetDebugStackTrace controls whether package-level debug logs include stack traces.
func SetLevel ¶ added in v1.3.0
SetLevel sets the logging level for the Logger instance. This method updates the log level to the specified level and returns the previous log level.
Parameters:
level (slog.Level) - The new log level to be set. This determines the severity of the logs
that will be captured. Common log levels include DEBUG, INFO, WARN, and ERROR.
Returns:
oldLevel (slog.Level) - The previous log level before it was updated. This can be used to restore
the previous log level if needed.
Example usage:
logger := &Logger{}
oldLevel := logger.SetLevel(slog.INFO)
// The log level is now set to INFO
// You can restore the old level if needed
logger.SetLevel(oldLevel)
func SetSource ¶ added in v1.4.0
func SetSource(enabled bool)
SetSource controls whether package-level logs include source metadata.
func StackTrace ¶
StackTrace allows you to view the exact place where the error or incident originated within the code. Shows a trace of up to 10 layers from where the error or incident was generated.