### Install Kibana with Homebrew Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/centralized_logging.md Use homebrew to install Kibana on Mac OSX. Follow on-screen instructions for auto-starting. ```bash brew install kibana ``` -------------------------------- ### Example Minitest Test File Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/testing.md A complete example of a Minitest test file demonstrating how to capture and assert log events. ```ruby require_relative "test_helper" class UserTest < ActiveSupport::TestCase describe User do it "logs message" do messages = semantic_logger_events do # Captures all log events during this block User.new.enable! end # Confirm the number of expected messages assert_equal 2, messages.count, messages # Confirm that the first log message includes the following elements assert_semantic_logger_event( messages[0], level: :info, message: "User enabled" ) # Confirm that the second log message includes the following elements assert_semantic_logger_event( messages[1], level: :debug, message: "Completed" ) end end end ``` -------------------------------- ### Install Semantic Logger Gem Source: https://github.com/reidmorrison/semantic_logger/blob/master/README.md Install the Semantic Logger gem using the standard RubyGems command. ```bash gem install semantic_logger ``` -------------------------------- ### Install Elasticsearch with Homebrew Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/centralized_logging.md Use homebrew to install Elasticsearch on Mac OSX. Follow on-screen instructions for auto-starting. ```bash brew install elasticsearch ``` -------------------------------- ### Enable Rack Started Messages in Production Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Show Rack 'Started' messages in production logs. By default, these are logged at the debug level and do not appear in production. ```ruby config.rails_semantic_logger.started = true ``` -------------------------------- ### Re-enable Started, Processing, and Rendered messages Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Enable the display of 'Started', 'Processing', and 'Rendered' messages in the logs, which are often suppressed by default for brevity. ```ruby config.rails_semantic_logger.started = true config.rails_semantic_logger.processing = true config.rails_semantic_logger.rendered = true ``` -------------------------------- ### Sentry Ruby Appender Context Example Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Demonstrates how to use tagged logging with the Sentry Ruby appender to enrich error events with transaction names, user information, fingerprints, and tags. ```ruby SemanticLogger.tagged(transaction_name: "foo", user_id: 42, baz: "quz") do logger.error("some message", username: "joe", fingerprint: ["bar"]) end ``` -------------------------------- ### RSpec Example for Logging Errors Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/testing.md An RSpec example demonstrating how to capture log events using `SemanticLogger::Test::CaptureLogEvents` and assert their properties, such as message content and level. ```ruby context 'when it blows up' do let(:capture_logger) { SemanticLogger::Test::CaptureLogEvents.new } it 'should should log the error' do allow_any_instance_of(MyThing).to receive(:logger).and_return(capture_logger) MyThing.new('asdf').do_something! expect(capture_logger.events.last.message).to include('Here is a message') expect(capture_logger.events.last.level).to eq(:error) end end ``` -------------------------------- ### Kibana Example Search: Host Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/centralized_logging.md Filter logs in Kibana to display messages originating from a specific host. ```kibana_query host: mymachine ``` -------------------------------- ### Configure Multiple Appenders Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Allows messages to be logged to multiple appenders simultaneously. This example shows logging to a local file and a remote Syslog server. ```ruby require "semantic_logger" SemanticLogger.default_level = :trace SemanticLogger.add_appender(file_name: "development.log", formatter: :color) SemanticLogger.add_appender(appender: :syslog, url: "tcp://myloghost:514") ``` -------------------------------- ### MongoDB Log Document Example Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md An example of a log message document as written to a MongoDB collection by the SemanticLogger::Appender::MongoDB. ```javascript > db.semantic_logger.findOne() { "_id" : ObjectId("53a8d5b99b9eb4f282000001"), "time" : ISODate("2014-06-24T01:34:49.489Z"), "host_name" : "appserver1", "pid" : null, "thread_name" : "2160245740", "name" : "Example", "level" : "info", "level_index" : 2, "application" : "my_application", "message" : "This message is written to mongo as a document" } ``` -------------------------------- ### Add Graylog UDP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure Semantic Logger to send logs to Graylog using the UDP protocol. Ensure the 'gelf' gem is installed. ```ruby SemanticLogger.add_appender( appender: :graylog, url: "udp://localhost:12201" ) ``` -------------------------------- ### Add Graylog TCP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure Semantic Logger to send logs to Graylog using the TCP protocol. Ensure the 'gelf' gem is installed. ```ruby SemanticLogger.add_appender( appender: :graylog, url: "tcp://localhost:12201" ) ``` -------------------------------- ### Custom Appender: Basic Structure Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/customize.md This example outlines the basic structure for creating a custom log appender by inheriting from SemanticLogger::Subscriber. It includes implementing the initializer, log, flush, and close methods. ```ruby require "semantic_logger" class SimpleAppender < SemanticLogger::Subscriber attr_reader :host # Add additional arguments to the initializer while supporting all existing ones. def initialize(host: host, **args, &block) @host = host super(**args, &block) end # Display the log struct and the text formatted output def log(log) # Display the raw log structure p log # Display the formatted output puts formatter.call(log) end # Optional def flush puts "Flush :)" end # Optional def close puts "Closing :)" end end ``` ```ruby SemanticLogger.default_level = :trace ``` -------------------------------- ### Daily Log Rotation Configuration Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/log_rotation.md Configure daily log rotation for `/var/www/rails/my_app/log/*.log` files using `logrotate`. This setup ensures logs are rotated once per day. ```bash /var/www/rails/my_app/log/*.log { daily missingok copytruncate rotate 14 compress delaycompress notifempty } ``` -------------------------------- ### Custom Formatter: Apply to Active Logger in Rails Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/customize.md This example shows how to apply a custom formatter to an existing file appender for a logger, such as the one used in Rails. It assumes the `rails_semantic_logger` gem is installed. ```ruby # Find file appender: appender = SemanticLogger.appenders.find{ |a| a.is_a?(SemanticLogger::Appender::File) } appender.formatter = MyFormatter.new ``` -------------------------------- ### Log to IO Stream Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to any IO stream, such as standard error or standard output. This example logs messages at the error level and above to standard error. ```ruby # Log errors and above to standard error: SemanticLogger.add_appender(io: $stderr, level: :error) ``` -------------------------------- ### Define Custom Log Formatter Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Create a custom log formatter by inheriting from `SemanticLogger::Formatters::Base` or a specific formatter like `Color`. This example shows how to override the `level` method. ```ruby class MyFormatter < SemanticLogger::Formatters::Color # Return the complete log level name in uppercase def level "#{color}log.level.upcase#{color_map.clear}" end end ``` -------------------------------- ### Configure Appender Logging Levels Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Explicitly sets the logging level for individual appenders, allowing a subset of messages to be sent to specific destinations. This example demonstrates logging warnings and above to `warnings.log` and all trace messages to `trace.log`. ```ruby require "semantic_logger" # Set default log level for new logger instances SemanticLogger.default_level = :info # Log all warning messages and above to warnings.log SemanticLogger.add_appender(file_name: "log/warnings.log", level: :warn) # Log all trace messages and above to trace.log SemanticLogger.add_appender(file_name: "log/trace.log", level: :trace) logger = SemanticLogger["MyClass"] logger.level = :trace logger.trace "This is a trace message" logger.info "This is an info message" logger.warn "This is a warning message" ``` -------------------------------- ### Log to Local Syslog Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to a local Syslog daemon. Ensure the syslog_protocol gem is installed if logging to a remote Syslog server. ```ruby SemanticLogger.add_appender(appender: :syslog) ``` -------------------------------- ### Customize HTTP Appender JSON Output Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Customize the JSON output for the HTTP appender using a formatter Proc. This example changes the timestamp format and renders the log to JSON. ```ruby formatter = Proc.new do |log, logger| h = log.to_h(logger.host, logger.application) # Change time from iso8601 to seconds since epoch h[:timestamp] = log.time.utc.to_f # Render to JSON h.to_json end SemanticLogger.add_appender( appender: :http, url: "https://localhost:8088/path", formatter: formatter ) ``` -------------------------------- ### Kibana Example Search: Tags Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/centralized_logging.md Search for a specific string within the 'tags' field in Kibana. ```kibana_query tags: 17262353 ``` -------------------------------- ### Customize UDP Appender Message Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Customize the message format for the UDP appender using a formatter Proc. This example modifies the timestamp and renders the log to JSON. ```ruby formatter = Proc.new do |log, logger| h = log.to_h(logger.host, logger.application) # Change time from iso8601 to seconds since epoch h[:timestamp] = log.time.utc.to_f # Render to JSON h.to_json end SemanticLogger.add_appender( appender: :udp, server: "localhost:8088", formatter: formatter ) ``` -------------------------------- ### Customize TCP Appender Message Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Customize the message format for the TCP appender using a formatter Proc. This example modifies the timestamp and renders the log to JSON. ```ruby formatter = Proc.new do |log, logger| h = log.to_h(logger.host, logger.application) # Change time from iso8601 to seconds since epoch h[:timestamp] = log.time.utc.to_f # Render to JSON h.to_json end SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088", ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE}, formatter: formatter ) ``` -------------------------------- ### Add JSON Log File Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md After disabling default file logging, add a new appender to direct logs to a JSON file. This example creates a log file named 'json.log' in the 'log' directory. ```ruby config.semantic_logger.add_appender(file_name: "log/json.log", formatter: :json) ``` -------------------------------- ### Add Logentries TCP Appender with Custom Formatter Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure Semantic Logger to send logs to Logentries via TCP using a custom JSON formatter that includes an access token. Ensure the 'logentries' gem is installed if not using Bundler. ```ruby module Logentries class Formatter < SemanticLogger::Formatters::Json attr_accessor :token def initialize(token) @token = token end def call(log, logger) "#{token} #{super(log, logger)}" end end end formatter = Logentries::Formatter.new("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") SemanticLogger.add_appender(appender: :tcp, server: "api.logentries.com:20000", ssl: true, formatter: formatter) ``` -------------------------------- ### Sample Log Rotation Configuration Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md This is a sample log rotation configuration file for Linux using `logrotate`. It sets up daily rotation, keeps 14 rotated files, and uses copy-truncate to avoid issues with log files not being re-opened. ```text /var/www/rails/my_rails_app/shared/log/*.log { daily missingok copytruncate rotate 14 compress delaycompress notifempty } ``` -------------------------------- ### Create a Logger Instance by Name Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Instantiate a logger for a specific class or application by providing its name as a string. ```ruby logger = SemanticLogger["MyClass"] ``` -------------------------------- ### Log a Message with a Logger Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/customize.md Create a logger instance and log an informational message. This demonstrates basic logging functionality after setting up appenders. ```ruby logger = SemanticLogger["Hello"] logger.info "Hello World" ``` -------------------------------- ### Configure Amazing Print Options Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Change the default Amazing Print options for pretty-printing log data. The `:multiline` option defaults to `false` if not supplied. ```ruby config.rails_semantic_logger.ap_options = {multiline: false} ``` -------------------------------- ### Standard Logging Methods Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Utilize standard logging methods for different severity levels, from trace to fatal. ```ruby logger.trace("Low level trace information such as data sent over a socket") logger.debug("Debugging information to aid with problem determination") logger.info("Informational message such as request received") logger.warn("Warn about something in the system") logger.error("An error occurred during processing") logger.fatal("Oh no something really bad happened") ``` -------------------------------- ### Create a Logger Instance by Class Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Instantiate a logger for a specific class by passing the class object directly. ```ruby logger = SemanticLogger[MyClass] ``` -------------------------------- ### Kibana Example Search: Error Level Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/centralized_logging.md Filter logs in Kibana to display only messages with the 'error' level. ```kibana_query level: error ``` -------------------------------- ### Add Logstash TCP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure Semantic Logger to send logs to Logstash using the TCP protocol. Ensure the 'logstash-logger' gem is installed. ```ruby require "logstash-logger" # Use the TCP logger ``` -------------------------------- ### Configure Stand-alone Application Source: https://github.com/reidmorrison/semantic_logger/blob/master/README.md Configure Semantic Logger for a stand-alone application by setting the default log level and adding an appender with a specific formatter. ```ruby require 'semantic_logger' # Set the global default log level SemanticLogger.default_level = :trace # Log to a file, and use the colorized formatter SemanticLogger.add_appender(file_name: 'development.log', formatter: :color) ``` -------------------------------- ### Add File and Simple Appenders Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/customize.md Configure Semantic Logger to write logs to a file named 'dev.log' and also to use a SimpleAppender. Ensure the SimpleAppender class is defined or imported. ```ruby SemanticLogger.add_appender(file_name: "dev.log") SemanticLogger.add_appender(appender: SimpleAppender.new) ``` -------------------------------- ### Log Duration with Keyword Arguments Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/metrics.md Log a duration metric using keyword arguments, specifying the metric name and the duration in milliseconds. ```ruby logger.info(message: "Called supplier", metric: "supplier/add_user", duration: 100.23) ``` -------------------------------- ### Initialize Semantic Logger Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Sets the global default log level to trace and adds a file appender with a colorized formatter. Also creates a logger instance for a specific class. ```ruby require 'semantic_logger' # Set the global default log level SemanticLogger.default_level = :trace # Log to a file, and use the colorized formatter SemanticLogger.add_appender(file_name: 'development.log', formatter: :color) # Create an instance of a logger # Add the application/class name to every log message logger = SemanticLogger['MyClass'] ``` -------------------------------- ### Include source file and line number in logs Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Enable logging of the source file and line number where a message originated. Use with caution in production due to potential performance impacts from backtrace generation. ```ruby config.semantic_logger.backtrace_level = :info ``` -------------------------------- ### Measure Info Block with Parameters Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Measure the execution time of a block of code and log it at the 'info' level. Supports optional parameters for detailed logging control. ```ruby log.measure_info(message, params=nil) do # Measure how long it takes to run this block of code end ``` -------------------------------- ### Configure MongoDB Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Sets up the MongoDB appender to write log messages as documents into a capped collection. Requires the 'mongo' gem. ```ruby client = Mongo::MongoClient.new("127.0.0.1", 27017) database = client["test"] appender = SemanticLogger::Appender::MongoDB.new( db: database, collection_size: 1024**3, # 1.gigabyte application: Rails.application.class.name ) SemanticLogger.add_appender(appender: appender) ``` -------------------------------- ### Configure File Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/README.md When creating the File appender explicitly, pass the file name as the first argument. This is a change from older versions where file_name: was used. ```ruby SemanticLogger::Appender::File.new("file.log") ``` -------------------------------- ### Add Custom Data to Rails Completed Log Message Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Modify the payload logged during controller-action processing by adding a `append_info_to_payload` method to your controller. This example adds a `user_id` to the payload. ```ruby class ThingController private def append_info_to_payload(payload) super payload[:user_id] = 42 end end ``` -------------------------------- ### Modifying Log Messages with a Proc Filter Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/filtering.md Use a Proc filter to modify log messages before they are logged, for example, to remove sensitive information like job payloads from Resque logs. ```ruby Resque.logger.filter = -> log do if (log.name == "Resque") && (match = log.message.to_s.match(/\A(got|done): /)) log.message = match[1] end # After the message has been modified, make sure it is logged: true end ``` -------------------------------- ### Configure Rollbar Integration Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Set up a callback to send error logs to Rollbar. This requires the Rollbar gem and is intended for synchronous logging modes. ```ruby SemanticLogger.on_log do |log| next unless log.try(:level) == :error err = RuntimeError.new(log.try(:message)) err.set_backtrace(log.backtrace) if log.backtrace Rollbar.error(err, :log_extra => log.to_h) end ``` -------------------------------- ### Custom Formatter: Exclude Process ID Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/customize.md Create a custom formatter by subclassing SemanticLogger::Formatters::Default to omit the process ID (PID) from log messages. This is useful in containerized environments or single-process setups. ```ruby class NoPidFormatter < SemanticLogger::Formatters::Default # Leave out the pid def pid end end ``` ```ruby SemanticLogger.add_appender(file_name: "development.log", formatter: NoPidFormatter.new) ``` ```ruby SemanticLogger.appenders.first.formatter = NoPidFormatter.new ``` -------------------------------- ### Add Syslog Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure Semantic Logger to log to a local Syslog instance. ```ruby config.semantic_logger.add_appender(appender: syslog) ``` -------------------------------- ### Enable Action View Rendering Messages in Production Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Show Action View rendering messages in production logs. By default, these are logged at the debug level and do not appear in production. ```ruby config.rails_semantic_logger.rendered = true ``` -------------------------------- ### Set Default Output Format Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Set the default output format for Rails logs. The format defaults to `:color` if `config.colorize_logging` is not `false`. ```ruby config.rails_semantic_logger.format = :default ``` -------------------------------- ### Configure CloudWatch Logs Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Forwards all log messages to CloudWatch Logs. Ensure the `client_kwargs` are set correctly for your AWS region and consider `create_stream: true` if the log stream does not exist. ```ruby SemanticLogger.add_appender( appender: :cloudwatch_logs, client_kwargs: {region: "eu-west-1"}, group: "/my/application", create_stream: true ) ``` -------------------------------- ### Size-Based Log Rotation Configuration Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/log_rotation.md Configure log rotation based on file size (2GB) for `/var/www/rails/my_app/log/*.log` files using `logrotate`. This is suitable for high-volume logging scenarios. ```bash /var/www/rails/my_app/log/*.log { size 2G missingok copytruncate rotate 7 compress nodelaycompress notifempty dateformat .%Y%m%d } ``` -------------------------------- ### Measure Info Block with Advanced Options Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Measure code execution time with advanced options like logging exceptions fully, setting a minimum duration threshold, and specifying a metric. Ideal for detailed performance monitoring and error tracking. ```ruby logger.measure_info "Called external interface", log_exception: :full, min_duration: 100, metric: "Custom/Supplier/process" do # Code to call external service ... end ``` -------------------------------- ### Log to Existing Ruby Logger Instance Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Integrates Semantic Logger with an existing Ruby `Logger` instance. This allows leveraging Semantic Logger's interface while still writing to the destinations configured in the original logger. ```ruby ruby_logger = Logger.new($stdout) # Log to an existing Ruby Logger instance SemanticLogger.add_appender(logger: ruby_logger) ``` -------------------------------- ### Set JSON Output Format Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure Semantic Logger to use the JSON output format for logs. This is typically done in the `application.rb` file. ```ruby config.rails_semantic_logger.format = :json ``` -------------------------------- ### Add Signal Handler for Standalone Application Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/signals.md Enable signal handling for Semantic Logger in a standalone Ruby application by calling this method. ```ruby # Enable signal handling for this process SemanticLogger.add_signal_handler ``` -------------------------------- ### Configure named tags for web requests Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Add custom key-value pairs to every log message for a given web request. This helps in correlating logs with specific request attributes. ```ruby config.log_tags = { request_id: :request_id, ip: :remote_ip, user: -> request { request.cookie_jar["login"] } } ``` -------------------------------- ### Error Logging with Details Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Logs an error message with associated key-value pairs detailing the failure. ```ruby logger.error("Oops external call failed", :result => :failed, :reason_code => -10) ``` -------------------------------- ### Measure with Dynamic Log Level Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Measure code execution time and log it using a dynamically specified log level. Accepts a log level symbol and a message. ```ruby logger.measure(:info, "Informational message such as request received") ``` -------------------------------- ### Configure Rails logging for Heroku (Log to stdout) Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure production environment to log to standard output on Heroku. This is typically enabled by the `RAILS_LOG_TO_STDOUT` environment variable. ```ruby if ENV["RAILS_LOG_TO_STDOUT"].present? $stdout.sync = true config.rails_semantic_logger.add_file_appender = false config.semantic_logger.add_appender(io: $stdout, formatter: config.rails_semantic_logger.format) end ``` -------------------------------- ### Log to Text File with Color Formatter Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to a specified file using a colorized formatter for better readability in the console. ```ruby SemanticLogger.add_appender(file_name: "development.log", formatter: :color) ``` -------------------------------- ### Log Ruby Thread Dump with TTIN Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/signals.md Send a TTIN signal to the process to log a thread dump with back-traces. Recommended to name threads using `Thread.current.name` for better identification. ```shell kill -TTIN 1234 ``` -------------------------------- ### Configure Rails log level from Heroku environment variable Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Enable setting the Rails log level dynamically using the `LOG_LEVEL` environment variable on Heroku. The level is converted to a symbol. ```ruby if ENV["LOG_LEVEL"].present? config.log_level = ENV["LOG_LEVEL"].downcase.strip.to_sym end ``` -------------------------------- ### Include Minitest Helpers Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/testing.md Add this line to your `test_helper.rb` to include Semantic Logger's Minitest helper methods. ```ruby Minitest::Test.include SemanticLogger::Test::Minitest ``` -------------------------------- ### Configure IO Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/README.md When creating the File appender explicitly for an IO stream, use the IO appender and pass the IO object directly. This is a change from older versions where File.new was used with an io: argument. ```ruby SemanticLogger::Appender::IO.new($stderr) ``` -------------------------------- ### Add Elasticsearch Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure Semantic Logger to send logs to an Elasticsearch instance. Requires the Elasticsearch URL to be specified. ```ruby config.semantic_logger.add_appender(appender: :elasticsearch, url: "http://localhost:9200") ``` -------------------------------- ### Add rails_semantic_logger to Gemfile Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Include these gems in your Gemfile to enable semantic logging. `amazing_print` is optional but recommended for colored hash output. ```ruby gem "amazing_print" gem "rails_semantic_logger" ``` -------------------------------- ### Add TCP Appender with SSL Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the TCP appender to send messages over TCP with SSL enabled. Set the 'ssl' option to true. ```ruby SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088", ssl: true ) ``` -------------------------------- ### Enable Controller Processing Messages in Production Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Show Controller 'Processing' messages in production logs. By default, these are logged at the debug level and do not appear in production. ```ruby config.rails_semantic_logger.processing = true ``` -------------------------------- ### Raise and Log Causal Exceptions in Ruby Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Demonstrates how to raise a new exception that wraps an original caught exception. Semantic Logger automatically logs both the new exception and its cause. ```ruby def oh_no f = File.new("filename", "w") # Will raise: IOError: not opened for reading f.read rescue IOError raise RuntimeError.new("Failed to write to file") end ``` ```ruby require "semantic_logger" SemanticLogger.add_appender(io: $stdout, formatter: :color) logger = SemanticLogger["Demo"] begin oh_no rescue StandardError => exception # Semantic Logger will log both the exception and the causing exception logger.error("Failed calling oh_no", exception) end ``` -------------------------------- ### Set Global Default Log Level to Trace Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Override the default log level to include :debug and :trace messages. Ensure appenders are configured after setting the default level. ```ruby require "semantic_logger" # Override the default log level of :info so that :debug and :trace are also logged SemanticLogger.default_level = :trace SemanticLogger.add_appender(file_name: "development.log", formatter: :color) logger = SemanticLogger["MyClass"] logger.info "Hello World" logger.trace "Low level trace information" ``` -------------------------------- ### Add Apache Kafka Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configures and adds the Kafka appender to publish log messages to an Apache Kafka broker. Specify the seed brokers for connection. ```ruby SemanticLogger.add_appender( appender: :kafka, seed_brokers: ["kafka1:9092", "kafka2:9092"], ) ``` -------------------------------- ### Basic Log Message Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Log a simple informational message including a duration. ```ruby logger.info("Queried users table in #{duration} ms, with a result code of #{result}") ``` -------------------------------- ### Include SemanticLogger::Loggable via Initializer Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md If your custom controller base class is in a third-party gem, add this to an initializer to include `SemanticLogger::Loggable` and ensure correct log entry class names. ```ruby CustomControllerBase.include(SemanticLogger::Loggable) ``` -------------------------------- ### Re-open Semantic Logger after Fork in Unicorn Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/forking.md Add this to your Unicorn configuration to re-open appenders after a process fork. ```ruby # config/unicorn.conf.rb after_fork do |server, worker| # Re-open appenders after forking the process SemanticLogger.reopen end ``` -------------------------------- ### Custom Formatter: Return Log Struct Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/customize.md Use a Proc to define a custom formatter that simply returns the log struct as a string. This is useful for debugging or when you need direct access to the log data. ```ruby require "semantic_logger" SemanticLogger.default_level = :trace formatter = Proc.new do |log| # This formatter just returns the log struct as a string log.inspect end SemanticLogger.add_appender(io: $stdout, formatter: formatter) logger = SemanticLogger["Hello"] logger.info "Hello World" ``` -------------------------------- ### Add Syslog Appender over TCP Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure Semantic Logger to log to a remote Syslog instance over TCP, specifying the host and port. ```ruby config.semantic_logger.add_appender(appender: syslog, url: "tcp://myloghost:514") ``` -------------------------------- ### Require Synchronous Logging via Gemfile Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Ensures Semantic Logger operates in synchronous mode by requiring the 'semantic_logger/sync' file. This is an alternative to calling SemanticLogger.sync! directly. ```ruby gem "semantic_logger", require: "semantic_logger/sync" ``` -------------------------------- ### Log Message with Hash Parameters Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Log messages by passing parameters, including the message itself, as a Hash. Supports optional payload, exception, duration, and metrics. ```ruby logger.debug(message: "Calling Supplier") logger.debug(message: "Calling Supplier", payload: {request: "update", user: "Jack"}) # Log a complete exception logger.error(message: "Calling Supplier", exception: exception) # Add a 100ms duration to the log entry logger.error(message: "Calling Supplier", duration: 100) # Add a count metric ( with a value of 1 ) logger.error(message: "Calling Supplier", metric: "Supplier/inquiry") ``` -------------------------------- ### Add Statsd Metric Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/metrics.md Configure Semantic Logger to send metrics to Statsd via UDP by adding a Statsd appender with the specified URL. ```ruby SemanticLogger.add_appender( metric: :statsd, url: 'localhost:8125' ) ``` -------------------------------- ### Measure and Log Execution Time Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Measures the execution time of a block of code and logs it at the info level. This is useful for performance monitoring. ```ruby logger.measure_info "Called external interface" do # Code to call external service ... sleep 0.75 end ``` -------------------------------- ### Add TCP Appender with SSL Verification Disabled Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the TCP appender with SSL, disabling server certificate verification. Useful for self-signed certificates. Refer to Net::TCPClient for more options. ```ruby SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088", ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE} ) ``` -------------------------------- ### Handle Forking for Auto-detected Frameworks (Stand-alone) Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/forking.md Use this code when Semantic Logger is used stand-alone (not with the Rails Semantic Logger gem) and you are using frameworks like Phusion Passenger, Resque, or Spring. It ensures appenders are reopened after a process fork. ```ruby # Passenger provides the :starting_worker_process event for executing # code after it has forked, so we use that and reconnect immediately. if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| SemanticLogger.reopen if forked end end # Re-open appenders after Resque has forked a worker if defined?(Resque) Resque.after_fork { |job| ::SemanticLogger.reopen } end # Re-open appenders after Spring has forked a process if defined?(Spring) Spring.after_fork { |job| ::SemanticLogger.reopen } end ``` -------------------------------- ### Add HTTP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the HTTP appender to send JSON logs to a specified URL. Ensure the target service accepts logs in JSON format. ```ruby SemanticLogger.add_appender( appender: :http, url: "http://localhost:8088/path" ) ``` -------------------------------- ### Use Custom Log Formatter Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Apply a custom log formatter to Semantic Logger in Rails. The custom formatter class must be defined and then instantiated. ```ruby config.rails_semantic_logger.format = MyFormatter.new ``` -------------------------------- ### Measure Info with Manual Duration Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Log a measured duration without executing a code block. Supply the duration manually, useful when the duration is already known. Ensures semantic logging of duration. ```ruby duration = Time.now - start_time logger.measure_info "Called external interface", duration: duration ``` -------------------------------- ### Log to Remote Syslog via UDP Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to a remote Syslog server using UDP. Specify the URL. Requires the syslog_protocol gem. ```ruby SemanticLogger.add_appender( appender: :syslog, url: "udp://myloghost:514" ) ``` -------------------------------- ### Define a Test Logger Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/testing.md Instantiate `SemanticLogger::Test::CaptureLogEvents` to create a logger that captures events in memory. This is useful for stubbing. ```ruby let(:logger) { SemanticLogger::Test::CaptureLogEvents.new } ``` -------------------------------- ### Add Count and Duration Metrics to Logs Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Log messages can include numeric metrics or durations. Use `metric_amount` for counts and `duration` for time elapsed. ```ruby logger.error(message: "Calling Supplier", metric: "Supplier/inquiry", metric_amount: 21) ``` ```ruby logger.error(message: "Calling Supplier", metric: "Supplier/inquiry", duration: 100) ``` -------------------------------- ### Change Log Level with SIGUSR2 Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/signals.md Send a SIGUSR2 signal to the process to cycle through log levels: :fatal, :error, :warn, :info, :debug, :trace. The level wraps around from :trace to :fatal. ```shell kill -SIGUSR2 1234 ``` -------------------------------- ### Configure Semantic Logger for Elasticsearch Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/centralized_logging.md Add this code to a Rails initializer or directly to your application to forward log messages to Elasticsearch. Ensure Elasticsearch is running at the specified URL. ```ruby SemanticLogger.add_appender( appender: :elasticsearch, url: "http://localhost:9200" ) ``` -------------------------------- ### Add UDP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the UDP appender to send JSON or other formatted messages over UDP. Specify the server address and port. ```ruby SemanticLogger.add_appender( appender: :udp, server: "localhost:8088" ) ``` -------------------------------- ### Log File Name and Line Number Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure Semantic Logger to capture the file name and line number for log messages at or above the specified level. By default, backtraces are only captured for `:error` and `:fatal` levels due to performance costs. ```ruby # Log file name and line number for log messages at this level and above config.semantic_logger.backtrace_level = :debug ``` -------------------------------- ### Configure Papertrail Syslog Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configures the syslog appender for Papertrail, which accepts log messages via TLS TCP. Ensure the CA certificate file path is correct. ```ruby config.semantic_logger.add_appender( appender: :syslog, url: "tcp://something.papertrailapp.com:1234", tcp_client: { ssl: { ca_file: File.join(Rails.root, "config", "papertrail-bundle.pem") } } ) ``` -------------------------------- ### Measure Info Block Execution Time Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Measure the duration of a code block and log it with 'info' level, assigning a metric name. Useful for tracking external service calls. ```ruby logger.measure_info "Called external interface", metric: "Supplier/inquiry" do # Code to call external service ... end ``` -------------------------------- ### Re-open Semantic Logger before Worker Boot in Puma Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/forking.md Include this in your Puma worker boot code when preloading your application in Clustered mode to re-open appenders after a fork. ```ruby # config/puma.rb before_worker_boot do # Re-open appenders after forking the process SemanticLogger.reopen end ``` -------------------------------- ### Log to Text File Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to a specified file using the standard formatter. Ensure the file is rotated using a copy-truncate operation to avoid data loss. ```ruby SemanticLogger.add_appender(file_name: "development.log") ``` -------------------------------- ### Informational Logging Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Logs an informational message indicating a call to a supplier. ```ruby logger.info("Calling Supplier") ``` -------------------------------- ### Apply Named Tags to Log Messages Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Apply named tags (key-value pairs) to all log messages within a block. Recommended for clarity and easier alerting in centralized logging systems. ```ruby SemanticLogger.tagged(user: "Jack", zip_code: 12345) do # All log entries in this block will include the above named tags logger.debug("Hello World") end ``` -------------------------------- ### Add RabbitMQ (AMQP) Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configures and adds the RabbitMQ appender to stream log messages through a queue on a RabbitMQ broker. Requires specifying queue name and broker connection details. ```ruby SemanticLogger.add_appender( appender: :rabbitmq, queue_name: "semantic_logger", rabbitmq_host: "localhost", username: "the-username", password: "the-password", ) ``` -------------------------------- ### Debug Logging with Block Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Logs a debug message that is constructed using a block, allowing for dynamic message generation. This is useful for performance as the message is only evaluated if the log level is enabled. ```ruby results = [ 5, 7, 2, 10 ] logger.debug { "A total of #{results.inject(0) {|sum, i| i+sum }} were processed" } ``` -------------------------------- ### Add Bugsnag Appender (Info, Warn, and Error Messages) Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configures the Bugsnag appender to send :info, :warn, and :error messages. Exercise caution with sensitive payload data. ```ruby SemanticLogger.add_appender(appender: :bugsnag, level: :info) ``` -------------------------------- ### Assert Log Event with Exact Payload Match Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/testing.md Verify that the payload of a log event exactly matches the provided hash. This is useful when the payload structure and content are critical. ```ruby assert_semantic_logger_event( messages[0], level: :info, message: "User enabled", payload: { first_name: "Jack", last_name: "Jones" } ) ``` -------------------------------- ### Configure Grafana Loki Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Sends log messages to Grafana Loki using its HTTP push API. Configure the URL, username, and password according to your Loki instance. The `compress` option can be set to `true` to compress log messages. ```ruby SemanticLogger.add_appender( appender: :loki, url: "https://logs-prod-001.grafana.net", username: "grafana_username", password: "grafana_token_here", compress: true ) ``` -------------------------------- ### Add Splunk HTTP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure Semantic Logger to send logs to Splunk via its HTTP Event Collector. A valid token is required. ```ruby SemanticLogger.add_appender( appender: :splunk_http, url: "http://localhost:8088/services/collector/event", token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ) ``` -------------------------------- ### Assert Log Event with Partial Payload Match Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/testing.md Use `payload_includes` to assert that the log event's payload contains specific key-value pairs, ignoring any extra keys. This allows for testing partial payload content. ```ruby assert_semantic_logger_event( messages[0], level: :info, message: "User enabled", payload_includes: { first_name: "Jack" } ) ``` -------------------------------- ### Apply Tags to Log Messages Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Apply specified tags to all log messages within a block of code. Essential for organizing logs in concurrent environments and identifying related entries. ```ruby tracking_number = "15354128" SemanticLogger.tagged(tracking_number) do # All log entries in this block will include the "tracking_number" logging tag logger.debug("Hello World") end ``` -------------------------------- ### Require Synchronous Logging Directly Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Forces Semantic Logger to operate in synchronous mode by directly requiring the 'semantic_logger/sync' file. This is an alternative to calling SemanticLogger.sync! directly. ```ruby require "semantic_logger/sync" ``` -------------------------------- ### Log to Remote Syslog via TCP Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to a remote Syslog server using TCP. Specify the URL and optionally the maximum packet size. Requires the syslog_protocol and net_tcp_client gems. ```ruby SemanticLogger.add_appender( appender: :syslog, url: "tcp://myloghost:514", max_size: 2048 ) ``` -------------------------------- ### Configure Specific Signals and GC Threshold Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/signals.md Override default signal handlers by specifying custom signals for log level changes and thread dumps, and set a custom microsecond threshold for JRuby garbage collection logging. ```ruby # Set the log level change signal to USR1 # Set the thread dump signal to "USR2" # Set the Garbage collection minimum threshold to 100,000 micro-seconds SemanticLogger.add_signal_handler("USR1", "USR2", 100000) ``` -------------------------------- ### Elastic Logging with Minimum Duration Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Log messages only when a minimum duration is exceeded using the `min_duration` option. This is useful for identifying slow operations. ```ruby logger.measure_warn "Called memcache", min_duration: 3 do # Code to call memcache ... end ``` -------------------------------- ### Add JSON Appender for ELK/Splunk Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/rails.md Configure Semantic Logger to also log to a JSON file, suitable for consumption by log aggregation systems like ELK or Splunk. ```ruby config.semantic_logger.add_appender(file_name: "log/#{Rails.env}.json", formatter: :json) ``` -------------------------------- ### Log Message with Semantic Information Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/index.md Log an informational message with additional semantic details like duration, result, table, and action. ```ruby logger.info('Queried table', duration: duration, result: result, table: 'users', action: 'query') ``` -------------------------------- ### Re-open Semantic Logger for Parallel Tests in Rails Minitest Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/forking.md Add this to your `ActiveSupport::TestCase` class when using parallel tests with forking in Rails with Minitest. ```ruby # test/test_helper.rb parallelize_setup do |worker| SemanticLogger.reopen end ``` -------------------------------- ### Log to Text File in JSON Format Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Appends log messages to a specified file in JSON format. The actual JSON output is a single line terminated with a newline, excluding fields with nil values. ```ruby SemanticLogger.add_appender(file_name: "development.log", formatter: :json) ``` -------------------------------- ### Add NewRelic Appender (Including Warnings) Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configures the New Relic appender to also send :warn level messages as error events. Be mindful of sensitive data in payloads. ```ruby SemanticLogger.add_appender(appender: :new_relic, level: :warn) ``` -------------------------------- ### Add HTTPS Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the HTTP appender to send JSON logs over HTTPS. Change the URL to use the 'https://' scheme. ```ruby SemanticLogger.add_appender( appender: :http, url: "https://localhost:8088/path" ) ``` -------------------------------- ### Add TCP Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the TCP appender to send JSON or other formatted messages over TCP. Specify the server address and port. ```ruby SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088" ) ``` -------------------------------- ### Add Honeybadger Insights Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the Honeybadger Insights appender to forward all log messages as events to Honeybadger Insights. This also relies on the Honeybadger gem configuration. ```ruby SemanticLogger.add_appender(appender: :honeybadger_insights) ``` -------------------------------- ### Measure Code Execution with Different Log Levels Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md The Semantic Logger provides various `measure_` methods corresponding to different log levels (trace, debug, info, warn, error, fatal) for timing code blocks. ```ruby logger.measure_trace("Low level trace information such as data sent over a socket") do ... end logger.measure_debug("Debugging information to aid with problem determination") do ... end logger.measure_info("Informational message such as request received") do ... end logger.measure_warn("Warn about something in the system") do ... end logger.measure_error("An error occurred during processing") do ... end logger.measure_fatal("Oh no something really bad happened") do ... end ``` -------------------------------- ### Add Honeybadger Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configure the Honeybadger appender to forward errors to Honeybadger. This requires the Honeybadger gem and uses its standard configuration. ```ruby SemanticLogger.add_appender(appender: :honeybadger) ``` -------------------------------- ### Add New Relic Metric Appender Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/metrics.md Forward metrics to New Relic by adding a New Relic appender. This allows for visualization of custom metrics in New Relic dashboards. ```ruby SemanticLogger.add_appender(metric: :new_relic) ``` -------------------------------- ### Measure Code Execution Time Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Use `measure_info` to log the execution time of a block of code. The message is logged only after the block completes, including its duration. ```ruby logger.measure_info "Called external interface" do # Code to call external service ... end ``` -------------------------------- ### Include SemanticLogger::Loggable Mixin Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/api.md Include the Loggable mixin within a class to automatically provide a logger instance accessible from both class and instance methods. ```ruby include SemanticLogger::Loggable def self.some_class_method logger.debug("logger is accessible from class methods") end def call_supplier logger.debug("logger is accessible from instance methods") end ``` -------------------------------- ### Add Bugsnag Appender (Warn and Error Messages) Source: https://github.com/reidmorrison/semantic_logger/blob/master/docs/appenders.md Configures the Bugsnag appender to send both :warn and :error level messages. Avoid logging sensitive data. ```ruby SemanticLogger.add_appender(appender: :bugsnag, level: :warn) ```