Files
herolib/lib/core/logger
mariobassem 81f377a532 test: improve logger test and search functionality
- Improve the logger test to include more specific assertions.
- Add timestamp filtering to the logger search function.
- Fix a bug in the logger search function that prevented it from correctly handling continuation lines.
- Update redisclient tests to use the correct return type.

Co-authored-by: mahmmoud.hassanein <mahmmoud.hassanein@gmail.com>
2025-01-13 18:46:13 +02:00
..
2024-12-31 11:00:02 +01:00
2024-12-31 11:00:02 +01:00
2024-12-31 11:00:02 +01:00

Logger Module

A simple logging system that provides structured logging with search capabilities.

Logs are stored in hourly files with a consistent format that makes them both human-readable and machine-parseable.

Features

  • Structured logging with categories and error types
  • Automatic timestamp management
  • Multi-line message support
  • Search functionality with filtering options
  • Human-readable log format

Usage

import freeflowuniverse.herolib.core.logger
import freeflowuniverse.herolib.data.ourtime

// Create a new logger
mut l := logger.new(path: '/var/logs')!

// Log a message
l.log(
    cat: 'system',
    log: 'System started successfully',
    logtype: .stdout
)!

// Log an error
l.log(
    cat: 'system',
    log: 'Failed to connect\nRetrying in 5 seconds...',
    logtype: .error
)!

// Search logs
results := l.search(
    timestamp_from: ourtime.now().warp("-24h"), // Last 24 hours
    cat: 'system',                               // Filter by category
    log: 'failed',                              // Search in message content
    logtype: .error,                            // Only error messages
    maxitems: 100                               // Limit results
)!

Log Format

$time
    $cat       - $msg
	   $cat       - first line of message
				    second line of message
				    third line ... 
E   $cat       - first line of message
E				second line of message
E 				third line ... 
  • time is expressed in '1980-07-11 21:23:42' == time_to_test.format_ss()
  • if cat has '-' inside it will be converted to '_'
  • $cat max 10 chars, and always takes the 10 chars so that the logs are nicely formatted
  • the first char is ' ' or 'E' , E means its the logtype error