Castor-Gemini/log_formatter.go

59 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"time"
)
// ConversationLog represents the top-level structure of the JSON log file.
type ConversationLog struct {
Entries []LogEntry `json:"conversation"`
}
// LogEntry represents a single turn in the conversation.
type LogEntry struct {
Timestamp string `json:"timestamp"`
Author string `json:"author"`
Content string `json:"content"`
}
func main() {
// Check for the correct number of arguments.
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: go run log_formatter.go <path_to_json_log_file>\n")
os.Exit(1)
}
filePath := os.Args[1]
// Read the JSON file.
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file %s: %v\n", filePath, err)
os.Exit(1)
}
// Unmarshal the JSON data into our struct.
var log ConversationLog
if err := json.Unmarshal(data, &log); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing JSON from %s: %v\n", filePath, err)
os.Exit(1)
}
// Iterate through the entries and print them in a readable format.
for _, entry := range log.Entries {
// Parse the timestamp to format it nicely.
ts, err := time.Parse(time.RFC3339, entry.Timestamp)
var formattedTime string
if err != nil {
formattedTime = entry.Timestamp // Fallback to the raw timestamp
} else {
formattedTime = ts.Format("15:04:05") // HH:MM:SS format
}
fmt.Printf("---\n")
fmt.Printf("[%s]:\n%s\n\n", entry.Author, entry.Content)
}
}