Overview
Caller information captures the source location of each log statement, showing:- File name (e.g.,
main.go) - Line number (e.g.,
42) - Function name (e.g.,
handleRequest) - Package name (e.g.,
server)
~/workspace/source/caller.go
Quick Start
Enable Caller Info
From~/workspace/source/options.go:144:
Auto-Capture for Errors
From~/workspace/source/options.go:208:
CallerInfo Structure
From~/workspace/source/caller.go:11-17:
Configuration Options
WithCaller
From~/workspace/source/options.go:137-146:
Enables caller info for all log levels:
WithCallerLevel
From~/workspace/source/options.go:193-210:
Enables automatic caller capture for logs at or above a threshold:
ErrorLevel (caller captured for Error and Fatal).
Common values:
go_logs.WarnLevel- Capture for Warn, Error, Fatalgo_logs.ErrorLevel- Capture for Error, Fatal (default)go_logs.FatalLevel- Capture only for Fatalgo_logs.SilentLevel- Disable auto-capture (useWithCaller(true)instead)
WithCallerSkip
From~/workspace/source/options.go:183-190:
Adjusts stack frame skipping for wrapper functions:
2 (skips GetCaller and Log methods).
GetCaller Implementation
From~/workspace/source/caller.go:21-49:
runtime.Caller() to retrieve call stack information.
String Formatting
From~/workspace/source/caller.go:51-65:
String() - Short Format
FullString() - Detailed Format
Accessing Caller Info
From~/workspace/source/entry.go:24-26:
Caller info is stored in the Entry struct:
In Hooks
In Formatters
Formatters can include caller info in output:When Caller is Captured
From~/workspace/source/logger_impl.go:177-179:
WithCaller(true)is set (all levels), OR- Log level ≥
callerLevel(auto-capture threshold)
Examples
Enable for All Levels
Auto-Capture for Errors Only
Custom Skip for Wrapper Functions
Conditional Caller Based on Environment
Performance Impact
From~/workspace/source/options.go:143:
- ~100-200ns per log call when enabled
- Uses
runtime.Caller()which has overhead - Consider using
WithCallerLevelinstead ofWithCaller(true)to reduce cost
Best Practices
Use CallerLevel in Production
- Lower overhead for Info/Debug logs
- Caller info where it matters (errors)
- Better production performance
Enable in Development
- Easier debugging
- Faster issue identification
- Better code navigation
Adjust Skip for Wrappers
If you wrap the logger:Troubleshooting
Wrong File/Line Shown
Problem: Caller shows logger wrapper instead of actual call site. Solution: Increase skip count:Caller is nil
Problem:entry.Caller is nil in hooks/formatters.
Solution: Ensure caller is enabled:
Performance Degradation
Problem: Logging is slower with caller enabled. Solution: UseWithCallerLevel instead of WithCaller(true):
See Also
- Stack Traces - Capture full call stack for errors
- Formatters - Include caller in output format
- Entry - Entry structure with caller info