-
Notifications
You must be signed in to change notification settings - Fork 0
Implement comprehensive logging system #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dansimau
merged 4 commits into
main
from
cursor/TIK-5-implement-comprehensive-logging-system-6e2f
Sep 28, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d180c4f
feat: Add logging service and log model
cursoragent 049a69a
Ignore SQLite database files and related WAL/SHM files
cursoragent a0c2903
feat: Add logs command to display HAL log entries
cursoragent 2dd36b4
Refactor: Use internal logging service and simplify logs command
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| # Binaries | ||
| hal | ||
| /hal | ||
|
|
||
| # Test coverage | ||
| cover.out | ||
|
|
||
| # Databases | ||
| sqlite.db* | ||
| test-results.json | ||
|
|
||
| # SQLite database files and related WAL/SHM files | ||
| *.db | ||
| *.db-wal | ||
| *.db-shm | ||
| **/*.db | ||
| **/*.db-wal | ||
| **/*.db-shm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/dansimau/hal/store" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // NewLogsCmd creates the logs command | ||
| func NewLogsCmd() *cobra.Command { | ||
| var fromTime string | ||
| var toTime string | ||
| var lastDuration string | ||
| var entityID string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "logs", | ||
| Aliases: []string{"log"}, | ||
| Short: "Display HAL log entries", | ||
| Long: `Display log entries from the HAL automation system. | ||
| Shows logs in chronological order with optional filtering by time range or entity.`, | ||
| Example: ` hal logs # Show all logs in chronological order | ||
| hal logs --from "2024-01-01" # Show logs from a specific date | ||
| hal logs --to "2024-01-31" # Show logs up to a specific date | ||
| hal logs --from "2024-01-01" --to "2024-01-31" # Show logs in date range | ||
| hal logs --last 5m # Show logs from last 5 minutes | ||
| hal logs --last 1h # Show logs from last 1 hour | ||
| hal logs --last 1d # Show logs from last 1 day | ||
| hal logs --entity-id "light.kitchen" # Show logs for specific entity`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return runLogsCommand(fromTime, toTime, lastDuration, entityID) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&fromTime, "from", "", "Start time for filtering logs (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS)") | ||
| cmd.Flags().StringVar(&toTime, "to", "", "End time for filtering logs (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS)") | ||
| cmd.Flags().StringVar(&lastDuration, "last", "", "Show logs from last duration (e.g., 5m, 1h, 2d)") | ||
| cmd.Flags().StringVar(&entityID, "entity-id", "", "Filter logs by entity ID") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runLogsCommand(fromTime, toTime, lastDuration, entityID string) error { | ||
| // Open database connection using default path | ||
| db, err := store.Open("sqlite.db") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open database: %w", err) | ||
| } | ||
|
|
||
| // Build query with filters | ||
| query := db.Model(&store.Log{}) | ||
|
|
||
| // Apply time filters | ||
| if lastDuration != "" { | ||
| duration, err := parseDuration(lastDuration) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid duration format: %w", err) | ||
| } | ||
| since := time.Now().Add(-duration) | ||
| query = query.Where("timestamp > ?", since) | ||
| } else { | ||
| if fromTime != "" { | ||
| from, err := parseTime(fromTime) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid from time format: %w", err) | ||
| } | ||
| query = query.Where("timestamp >= ?", from) | ||
| } | ||
| if toTime != "" { | ||
| to, err := parseTime(toTime) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid to time format: %w", err) | ||
| } | ||
| query = query.Where("timestamp <= ?", to) | ||
| } | ||
| } | ||
|
|
||
| // Apply entity filter | ||
| if entityID != "" { | ||
| query = query.Where("entity_id = ?", entityID) | ||
| } | ||
|
|
||
| // Execute query and get results | ||
| var logs []store.Log | ||
| if err := query.Order("timestamp ASC").Find(&logs).Error; err != nil { | ||
| return fmt.Errorf("failed to query logs: %w", err) | ||
| } | ||
|
|
||
| // Print results | ||
| return printLogs(logs) | ||
| } | ||
|
|
||
| func parseDuration(durationStr string) (time.Duration, error) { | ||
| // Handle common duration formats like 5m, 1h, 2d | ||
| if strings.HasSuffix(durationStr, "d") { | ||
| days, err := strconv.Atoi(strings.TrimSuffix(durationStr, "d")) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| return time.Duration(days) * 24 * time.Hour, nil | ||
| } | ||
|
|
||
| // For other formats (m, h, s), use standard time.ParseDuration | ||
| return time.ParseDuration(durationStr) | ||
| } | ||
|
|
||
| func parseTime(timeStr string) (time.Time, error) { | ||
| // Try different time formats | ||
| formats := []string{ | ||
| "2006-01-02 15:04:05", | ||
| "2006-01-02 15:04", | ||
| "2006-01-02", | ||
| } | ||
|
|
||
| for _, format := range formats { | ||
| if t, err := time.Parse(format, timeStr); err == nil { | ||
| return t, nil | ||
| } | ||
| } | ||
|
|
||
| return time.Time{}, fmt.Errorf("unable to parse time: %s (expected formats: YYYY-MM-DD, YYYY-MM-DD HH:MM, or YYYY-MM-DD HH:MM:SS)", timeStr) | ||
| } | ||
|
|
||
| func printLogs(logs []store.Log) error { | ||
| if len(logs) == 0 { | ||
| fmt.Println("No logs found") | ||
| return nil | ||
| } | ||
|
|
||
| // Print logs without header to look like a log file | ||
| for _, log := range logs { | ||
| entityIDStr := "" | ||
| if log.EntityID != nil { | ||
| entityIDStr = fmt.Sprintf(" [%s]", *log.EntityID) | ||
| } | ||
|
|
||
| fmt.Printf("%s%s %s\n", | ||
| log.Timestamp.Format("2006-01-02 15:04:05"), | ||
| entityIDStr, | ||
| log.LogText, | ||
| ) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| package hal | ||
|
|
||
| import ( | ||
| "log/slog" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
@@ -39,7 +38,8 @@ func (b *Button) Action(_ EntityInterface) { | |
| b.pressedTimes = 1 | ||
| } | ||
|
|
||
| slog.Info("Button pressed", "entity", b.GetID(), "times", b.pressedTimes) | ||
| entityID := b.GetID() | ||
| b.connection.loggingService.Info("Button pressed", &entityID, "entity", entityID, "times", b.pressedTimes) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Button Action Method Fails Without Nil CheckThe |
||
|
|
||
| b.lastPressed = time.Now() | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Logs Command Ignores Configurable Database Path
The
logscommand hardcodes the database path tosqlite.db. This means it won't use the configurable database path from the main application, potentially causing it to read from the wrong database or fail if a different path is configured.