Sending Your First Logs

Learn how to send your first logs to LogFlux in under 5 minutes

This guide will walk you through sending your first logs to LogFlux using our Go SDK. You’ll have logs flowing into your dashboard in under 5 minutes!

Prerequisites

Step 1: Install the Go SDK

1
2
go mod init logflux-example
go get github.com/logflux-io/logflux-go-sdk

Step 2: Create Your First Logger

Create a new file main.go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
    "context"
    "log"
    "time"
    
    "github.com/logflux-io/logflux-go-sdk"
)

func main() {
    // Initialize LogFlux with your API key
    client, err := logflux.NewClient(logflux.Config{
        APIKey: "your-api-key-here",
        Environment: "development", // or "production"
    })
    if err != nil {
        log.Fatal("Failed to create LogFlux client:", err)
    }
    defer client.Close()

    // Send a simple log entry
    err = client.Log(context.Background(), logflux.Entry{
        Level:     "info",
        Message:   "Hello from LogFlux!",
        Timestamp: time.Now(),
        Labels: map[string]string{
            "service": "example-app",
            "version": "1.0.0",
        },
        Fields: map[string]interface{}{
            "user_id": 12345,
            "action":  "user_login",
        },
    })
    if err != nil {
        log.Fatal("Failed to send log:", err)
    }

    log.Println("Log sent successfully!")
}

Step 3: Run Your Example

Replace your-api-key-here with your actual API key and run:

1
go run main.go

You should see:

Log sent successfully!

Step 4: View Your Logs

  1. Open your LogFlux dashboard
  2. Navigate to the Logs section
  3. You should see your log entry with the message “Hello from LogFlux!”

Step 5: Send Structured Logs

Let’s send some more interesting structured logs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main

import (
    "context"
    "fmt"
    "log"
    "math/rand"
    "time"
    
    "github.com/logflux-io/logflux-go-sdk"
)

func main() {
    client, err := logflux.NewClient(logflux.Config{
        APIKey: "your-api-key-here",
        Environment: "development",
    })
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    // Simulate various log levels and events
    events := []struct {
        level   string
        message string
        fields  map[string]interface{}
    }{
        {
            level:   "info",
            message: "User authenticated successfully",
            fields: map[string]interface{}{
                "user_id":    rand.Intn(1000),
                "ip_address": "192.168.1.100",
                "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
            },
        },
        {
            level:   "warning",
            message: "Rate limit approaching",
            fields: map[string]interface{}{
                "current_requests": 950,
                "limit":           1000,
                "window":          "1h",
            },
        },
        {
            level:   "error",
            message: "Database connection failed",
            fields: map[string]interface{}{
                "error":           "connection timeout",
                "database":        "postgresql",
                "retry_attempt":   3,
                "connection_pool": "primary",
            },
        },
    }

    for i, event := range events {
        err := client.Log(context.Background(), logflux.Entry{
            Level:     event.level,
            Message:   event.message,
            Timestamp: time.Now(),
            Labels: map[string]string{
                "service":     "auth-service",
                "environment": "development",
                "version":     "2.1.0",
            },
            Fields: event.fields,
        })
        if err != nil {
            log.Printf("Failed to send log %d: %v", i+1, err)
            continue
        }
        
        fmt.Printf("Sent %s log: %s\n", event.level, event.message)
        time.Sleep(100 * time.Millisecond)
    }
}

Understanding Log Structure

LogFlux logs have the following structure:

  • Level: Log severity (debug, info, warning, error, critical)
  • Message: Human-readable log message
  • Timestamp: When the event occurred
  • Labels: Key-value pairs for filtering and grouping (indexed)
  • Fields: Additional structured data (searchable)

Best Practices

1. Use Consistent Labels

Labels are indexed and used for filtering. Use consistent naming:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Good
Labels: map[string]string{
    "service":     "user-api",
    "environment": "production",
    "version":     "1.2.3",
}

// Avoid
Labels: map[string]string{
    "svc":  "user-api",
    "env":  "prod", 
    "ver":  "1.2.3",
}

2. Structure Your Fields

Use structured data in fields for better searchability:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Good
Fields: map[string]interface{}{
    "http": map[string]interface{}{
        "method":      "POST",
        "status_code": 201,
        "path":        "/api/users",
        "duration_ms": 45,
    },
    "user": map[string]interface{}{
        "id":    12345,
        "email": "user@example.com",
    },
}

3. Choose Appropriate Log Levels

  • debug: Detailed diagnostic information
  • info: General operational messages
  • warning: Something unexpected but not critical
  • error: Error conditions that need attention
  • critical: System is unusable

Next Steps