### Systemd and Syslog Style Logging Source: https://github.com/theforeman/journald-logger/blob/master/README.md Provides examples of using native systemd-journal style methods and syslog-prefixed methods for logging. ```ruby logger.send_message(message: 'hi!', priority: Journald::LOG_NOTICE, any_field: 'any_value') logger.print Journald::LOG_NOTICE, 'hi!' logger.log_err 'Error' logger.log_debug 'Debug' ``` -------------------------------- ### Configure Log Levels and Priorities in Ruby Source: https://context7.com/theforeman/journald-logger/llms.txt Demonstrates how to set and modify logging thresholds using both Ruby Logger severity levels and syslog priority constants. Includes examples of temporary silencing for specific code blocks. ```ruby require 'journald/logger' # Set minimum priority at construction logger = Journald::Logger.new('my-app', Journald::LOG_WARNING) # Runtime change using syslog priority logger.min_priority = Journald::LOG_NOTICE # Runtime change using Logger severity logger.level = Logger::WARN logger.sev_threshold = Logger::INFO # Alias for level= # Check current level puts logger.level # Returns Logger constant puts logger.min_priority # Returns Journald constant # Debug messages are now filtered out logger.debug "This won't be logged" # Skipped logger.warn "This will be logged" # Logged # Temporarily silence logger (useful during noisy operations) logger.silence(Logger::ERROR) do |silenced_logger| silenced_logger.info "Not logged" silenced_logger.error "This is logged" end # Original level restored # Alternative silence syntax with priority logger.silence(priority: Journald::LOG_CRIT) do |l| l.error "Silenced" l.fatal "Logged as CRIT or above" end ``` -------------------------------- ### Use Syslog-Style Logging Methods Source: https://context7.com/theforeman/journald-logger/llms.txt Provides examples of using syslog-prefixed methods for granular control over log priority levels, including support for lazy evaluation via blocks. ```ruby require 'journald/logger' logger = Journald::Logger.new('my-app') # Syslog priority methods (lowest to highest priority) logger.log_debug "Debug level message" logger.log_info "Informational message" logger.log_notice "Normal but significant" logger.log_warning "Warning condition" logger.log_err "Error condition" logger.log_crit "Critical condition" logger.log_alert "Action must be taken" logger.log_emerg "System is unusable" # Block form for lazy evaluation logger.log_notice { "Expensive computation: #{calculate_stats}" } ``` -------------------------------- ### Initialize Journald Logger Source: https://github.com/theforeman/journald-logger/blob/master/README.md Demonstrates how to instantiate the standard Journald logger or the TraceLogger, which includes file and line number metadata. ```ruby require 'journald/logger' logger = Journald::Logger.new('gandalf') logger = Journald::TraceLogger.new('gandalf') ``` -------------------------------- ### Ruby Journald Logger Compatibility Methods Source: https://context7.com/theforeman/journald-logger/llms.txt Demonstrates the usage of compatibility methods in the Journald::Logger class for Ruby. It shows how progname, formatter, datetime_format, close, and silence methods work, highlighting that formatter and datetime_format are no-ops and that it integrates with Rails. ```ruby require 'journald/logger' logger = Journald::Logger.new('my-app') # progname getter/setter (maps to SYSLOG_IDENTIFIER) logger.progname = 'new-app-name' puts logger.progname # => 'new-app-name' # Formatter methods (no-ops, journald handles formatting) logger.formatter = SomeFormatter.new # Ignored logger.formatter # => nil logger.datetime_format = '%Y-%m-%d' # Ignored logger.datetime_format # => nil # Close method (no-op, nothing to close) logger.close # Works with Rails and other frameworks expecting Logger Rails.logger = Journald::Logger.new('rails-app') # ActiveRecord session store compatibility logger.silence_logger do |l| # Alias for silence # Quiet logging during noisy operations end ``` -------------------------------- ### Apply and Manage Log Tags Source: https://github.com/theforeman/journald-logger/blob/master/README.md Demonstrates adding systemd-journal fields to log entries globally or within specific blocks, and removing tags. ```ruby logger = Journald::Logger.new('gandalf', world: 'arda') logger.tag location: 'shire', weapon: 'staff' logger.tag(location: 'moria', object: 'balrog') do logger.warn 'you shall not pass!' end logger.untag :location, :weapon ``` -------------------------------- ### Log Ruby Exceptions with Context Source: https://context7.com/theforeman/journald-logger/llms.txt Shows how to use the exception method to capture full stack traces, cause chains, and custom metadata for structured logging of errors. ```ruby require 'journald/logger' logger = Journald::Logger.new('my-app') begin begin raise ArgumentError, "Invalid input" rescue => inner raise RuntimeError, "Processing failed" end rescue => e # Log exception at default LOG_ERR level logger.exception(e) # Log with custom severity (Logger constant) logger.exception(e, severity: Logger::WARN) # Log with custom priority (Journald constant) logger.exception(e, priority: Journald::LOG_ALERT) end ``` -------------------------------- ### Manage Reporting Levels Source: https://github.com/theforeman/journald-logger/blob/master/README.md Configures the minimum reporting priority for the logger, allowing for both initialization-time and runtime adjustments. ```ruby logger = Journald::Logger.new('gandalf', Journald::LOG_NOTICE) logger.min_priority = Journald::LOG_NOTICE logger.level = Logger::WARN logger.sev_threshold = Logger::INFO ``` -------------------------------- ### Standard Logger Methods (Ruby) Source: https://context7.com/theforeman/journald-logger/llms.txt Demonstrates using standard Ruby Logger methods (debug, info, warn, error, fatal, unknown) with `Journald::Logger`. Messages are mapped to syslog priorities. Supports block form for expensive messages, progname override, and checking log level enablement. Requires the 'journald/logger' gem. ```ruby require 'journald/logger' logger = Journald::Logger.new('my-app') # Basic logging at different severity levels logger.debug "Debug message for troubleshooting" logger.info "Informational message" logger.warn "Warning: something unexpected happened" logger.error "Error occurred during processing" logger.fatal "Fatal error, application shutting down" logger.unknown "Unknown severity message" # Block form for expensive message construction (only called if level enabled) logger.info("request-handler") { "Processing request #{request.id}" } # Progname override for individual messages logger.warn("database") { "Connection pool exhausted" } # Check if level is enabled if logger.debug? logger.debug "Detailed debug info: #{expensive_operation}" end # Stream-style logging (logs at DEBUG level) logger << "Appended message" # Query journal: journalctl -t my-app -p warning # Shows WARN and above ``` -------------------------------- ### Logger Compatibility and Severity Mapping Source: https://github.com/theforeman/journald-logger/blob/master/README.md Shows the drop-in replacement capabilities and the internal mapping of Ruby Logger severity levels to Syslog priorities. ```ruby logger.warn "you shall not pass!" logger.info("gollum") { "my preciousss" } LEVEL_MAP = { ::Logger::UNKNOWN => LOG_ALERT, ::Logger::FATAL => LOG_CRIT, ::Logger::ERROR => LOG_ERR, ::Logger::WARN => LOG_WARNING, ::Logger::INFO => LOG_INFO, ::Logger::DEBUG => LOG_DEBUG, } ``` -------------------------------- ### Tagging Log Messages (Ruby) Source: https://context7.com/theforeman/journald-logger/llms.txt Shows how to add custom systemd-journal fields (tags) to log messages using `Journald::Logger`. Tags can be persistent, additive, or block-scoped. Demonstrates adding, removing, and retrieving tag values. Requires the 'journald/logger' gem. ```ruby require 'journald/logger' logger = Journald::Logger.new('my-app') # Add persistent tags logger.tag(user_id: 12345, request_id: 'abc-123') # All subsequent logs include these fields logger.info "User action performed" # Journal: MESSAGE=User action performed, USER_ID=12345, REQUEST_ID=abc-123 # Add more tags (additive) logger.tag(component: 'checkout') logger.info "Processing checkout" # Journal includes: USER_ID, REQUEST_ID, and COMPONENT # Block-scoped tags (automatically restored after block) logger.tag(transaction_id: 'txn-456') do logger.info "Transaction started" # Includes TRANSACTION_ID logger.tag(step: 'payment') do logger.info "Processing payment" # Includes TRANSACTION_ID and STEP end # STEP is removed, TRANSACTION_ID still present end # TRANSACTION_ID is removed # Get current tag value current_user = logger.tag_value(:user_id) # => 12345 # Remove specific tags logger.untag(:user_id, :request_id) # Query tagged entries: journalctl -t my-app USER_ID=12345 ``` -------------------------------- ### Create Journald::Logger Instance (Ruby) Source: https://context7.com/theforeman/journald-logger/llms.txt Instantiates a `Journald::Logger` for sending logs to the systemd journal. Supports setting a program name, minimum priority level, and initial custom tags for all log entries. Requires the 'journald/logger' gem. ```ruby require 'journald/logger' # Simple logger with program name logger = Journald::Logger.new('my-app') # Logger with minimum priority level (only log messages at or above NOTICE) logger = Journald::Logger.new('my-app', Journald::LOG_NOTICE) # Logger with initial tags that will be added to all log entries logger = Journald::Logger.new('my-app', Journald::LOG_DEBUG, environment: 'production', version: '1.2.3' ) # Verify logged entries with journalctl: # journalctl -t my-app --since "1 minute ago" ``` -------------------------------- ### Execute Low-Level Journal Operations Source: https://context7.com/theforeman/journald-logger/llms.txt Demonstrates direct interaction with the journal using send_message for custom fields and print for simple priority-based logging. ```ruby require 'journald/logger' logger = Journald::Logger.new('my-app') logger.tag(environment: 'production') # send_message: full control over journal fields logger.send_message( message: 'User authentication successful', priority: Journald::LOG_NOTICE, user_id: 12345, auth_method: 'oauth2', ip_address: '192.168.1.100', session_id: 'sess_abc123' ) # print: simple priority + message (tags still applied) logger.print(Journald::LOG_INFO, 'Application started') # Custom metrics logging logger.send_message( message: 'Request completed', priority: Journald::LOG_INFO, response_time_ms: 42, status_code: 200, endpoint: '/api/users', method: 'GET' ) ``` -------------------------------- ### Create Journald::TraceLogger Instance (Ruby) Source: https://context7.com/theforeman/journald-logger/llms.txt Instantiates a `Journald::TraceLogger`, which extends `Journald::Logger` to automatically include caller location information (file, line, function) in log entries. Accepts program name, minimum priority, and initial tags. Requires the 'journald/logger' gem. ```ruby require 'journald/logger' # TraceLogger with program name logger = Journald::TraceLogger.new('my-app') # TraceLogger with priority and tags logger = Journald::TraceLogger.new('my-app', Journald::LOG_DEBUG, component: 'authentication' ) # All log calls now include location info logger.info "User logged in" # Journal entry includes: # MESSAGE=User logged in # CODE_FILE=/path/to/app.rb # CODE_LINE=15 # CODE_FUNC=login # View with journalctl: # journalctl -t my-app -o json-pretty ``` -------------------------------- ### Log Exceptions with Metadata Source: https://github.com/theforeman/journald-logger/blob/master/README.md Handles exception logging, which automatically captures backtraces, exception classes, and messages into structured journal fields. ```ruby begin raise "Aw, snap!" rescue => e logger.exception e logger.exception e, severity: Logger::WARN logger.exception e, priority: Journald::LOG_ALERT end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.