Skip to content
Session Logging

Session Logging

What is a Session?

A session is a record of a user’s authenticated presence in your application — from login to logout (or expiry). Where events capture individual actions, a session captures the span of a user’s access.

Sessions are useful for answering questions like: “Where did this user log in from? What device were they using? How long were they active?”

Examples:

ScenarioStatus
User signs in from a new deviceACTIVE
User explicitly logs outINACTIVE
Session expires after inactivityINACTIVE
Admin remotely terminates a sessionINACTIVE

Each session is enriched with the client’s IP address and device information, and geolocation is derived automatically from the IP. This makes sessions particularly useful for detecting account takeovers, concurrent logins from different locations, or suspicious access patterns.


Records user login and logout events enriched with device information, IP address, and location. Sessions carry a status (ACTIVE / INACTIVE), making it easy to audit access history and detect anomalous activity.


package main

import (
	"log"
	"net/http"
	"os"
	"time"

	_ "github.com/joho/godotenv/autoload"

	logtrace "github.com/logtracehq/logtrace-go"
)

func main() {
	client, err := logtrace.New(os.Getenv("API_KEY"))
	if err != nil {
		log.Fatalf("Failed to create LogTrace client: %v", err)
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
		lc := logtrace.FromContext(r.Context(), client)
		// Create a session
		_, err = lc.CreateSession(r.Context(), &logtrace.CreateSessionRequest{
			LoginAt:  time.Now(),
			Status:   "active",
			UserID:   "user_23232",
			Token:    "efb2c9a1-1234-5678-90ab-cdef12345678",
			UserName: "jane_doe",
		})
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
		w.Write([]byte("done"))
	})

	log.Fatal(http.ListenAndServe(":5000", logtrace.Logger(client)(mux)))
}