Logs
Provides a helper to tail the Kubernetes deployment logs. Usage looks like:
tailer, done := logs.Logger{
Deployment: "mydeployment",
Namespace: "mynamespace",
}.Start()
defer func() {
err := done() // closes the tailer
if err != nil { ... }
}()
for line := range tailer.NextLine() {
// Do something
}
Features
- Tail logs from Kubernetes deployments
- Supports in-cluster and out-of-cluster configurations
- Automatic pod and container discovery
- Configurable timeout
- Support for tailing specific number of lines
Usage
Basic Usage
tailer, done := logs.Logger{
Deployment: "mydeployment",
Namespace: "mynamespace",
}.Start()
defer func() {
err := done()
if err != nil {
// handle error
}
}()
for line := range tailer.NextLine() {
fmt.Println(line)
}
Advanced Usage
// Tail from a specific pod and container
tailer, done := logs.Logger{
Deployment: "mydeployment",
Namespace: "mynamespace",
Pod: "mydeployment-7b5b5c8c9d-xyz12",
Container: "mycontainer",
TailLines: int64Ptr(100), // Tail last 100 lines
Timeout: 5 * time.Minute,
}.Start()
defer func() {
err := done()
if err != nil {
// handle error
}
}()
for line := range tailer.NextLine() {
fmt.Println(line)
}
Helper Functions
// Helper to create a pointer to int64
func int64Ptr(i int64) *int64 {
return &i
}
Configuration
The Logger struct accepts the following fields:
Deployment(required): Name of the Kubernetes deploymentNamespace(required): Kubernetes namespacePod(optional): Specific pod name to tail logs fromContainer(optional): Specific container name to tail logs fromTailLines(optional): Number of lines to tail from the end of logsTimeout(optional): Time to wait before giving up on tailing
Requirements
- Kubernetes cluster access (in-cluster or via kubeconfig)
- Proper RBAC permissions to read pod logs