### Install Gems with Bundler Source: https://logger.rocketjob.io/rails.html Run this command after updating your Gemfile to install the necessary gems. ```bash bundle install ``` -------------------------------- ### Install gelf gem manually Source: https://logger.rocketjob.io/appenders.html Install the 'gelf' gem directly using the gem command if not using Bundler. ```bash gem install gelf ``` -------------------------------- ### Install Kibana with Homebrew Source: https://logger.rocketjob.io/centralized_logging.html Use this command to install Kibana on macOS using Homebrew. Follow on-screen instructions for auto-starting. ```bash brew install kibana ``` -------------------------------- ### Install logstash-logger gem Source: https://logger.rocketjob.io/appenders.html Install the 'logstash-logger' gem using the gem command if not using Bundler. ```bash gem install logstash-logger ``` -------------------------------- ### Install Elasticsearch with Homebrew Source: https://logger.rocketjob.io/centralized_logging.html Use this command to install Elasticsearch on macOS using Homebrew. Follow on-screen instructions for auto-starting. ```bash brew install elasticsearch ``` -------------------------------- ### Example Log Output for Causal Exceptions Source: https://logger.rocketjob.io/api.html The resulting log output showing both stack traces. ```text 2017-05-03 09:45:38.948029 E [17641:70311685126260 demo.rb:17] Demo -- Failed calling oh_no -- Exception: RuntimeError: Failed to write to file demo.rb:6:in `rescue in oh_no' demo.rb:2:in `oh_no' demo.rb:14:in `
' Cause: IOError: not opened for reading demo.rb:4:in `read' demo.rb:4:in `oh_no' demo.rb:14:in `
' ``` -------------------------------- ### Enable Started Messages in Production Source: https://logger.rocketjob.io/rails.html Overrides the default debug-level logging for Rack started messages to show them in production. ```text Rack -- Started -- { :ip => "127.0.0.1", :method => "GET", :path => "/users" } ``` ```ruby config.rails_semantic_logger.started = true ``` -------------------------------- ### Custom Appender: Sample Usage Source: https://logger.rocketjob.io/customize.html Example of how to configure Semantic Logger to use a custom appender, setting the default log level and adding a file appender. ```ruby SemanticLogger.default_level = :trace # Log to file dev.log SemanticLogger.add_appender(file_name: "dev.log") ``` -------------------------------- ### Test Log Events with Minitest Source: https://logger.rocketjob.io/testing.html Example of capturing and asserting log events within a Minitest test case. ```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 ``` -------------------------------- ### Re-enable Started, Processing, and Rendered Messages Source: https://logger.rocketjob.io/rails.html Set these configuration options in `config/application.rb` or an environment-specific file to re-enable the display of 'Started', 'Processing', and 'Rendered' messages in Rails logs. ```ruby config.rails_semantic_logger.started = true config.rails_semantic_logger.processing = true config.rails_semantic_logger.rendered = true ``` -------------------------------- ### Configure NewRelic Appender Source: https://logger.rocketjob.io/appenders.html Configures the NewRelic appender for error events and includes installation instructions for the gem. ```ruby gem "newrelic_rpm" ``` ```bash bundle install ``` ```bash gem install newrelic_rpm ``` ```ruby SemanticLogger.add_appender(appender: :new_relic) ``` ```ruby SemanticLogger.add_appender(appender: :new_relic, level: :warn) ``` -------------------------------- ### Test Log Events with RSpec Source: https://logger.rocketjob.io/testing.html Example of capturing log events in RSpec using the CaptureLogEvents helper. ```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 ``` -------------------------------- ### Configure Named Tags for Web Requests Source: https://logger.rocketjob.io/rails.html Add custom named tags to log messages on a per-request basis by overriding `config.log_tags`. This example adds `request_id`, `ip`, and a user tag derived from cookies. ```ruby config.log_tags = { request_id: :request_id, ip: :remote_ip, user: -> request { request.cookie_jar["login"] } } ``` -------------------------------- ### Original Rails Messages with Semantic Logger Formatting Source: https://logger.rocketjob.io/rails.html Configure Semantic Logger to mimic original Rails log formatting while retaining semantic capabilities. This includes enabling 'Started', 'Processing', and 'Rendered' messages. ```ruby config.rails_semantic_logger.semantic = false config.rails_semantic_logger.started = true config.rails_semantic_logger.processing = true config.rails_semantic_logger.rendered = true ``` -------------------------------- ### Sample Log Rotation File for Linux Source: https://logger.rocketjob.io/rails.html This is a sample log rotation configuration file for Linux using `logrotate`. It sets up daily rotation, keeps 14 days of logs, and uses copytruncate. ```bash /var/www/rails/my_rails_app/shared/log/*.log { daily missingok copytruncate rotate 14 compress delaycompress notifempty } ``` -------------------------------- ### Configure Daily Log Rotation with logrotate Source: https://logger.rocketjob.io/log_rotation.html Use this configuration to rotate log files on a daily basis while keeping 14 days of history. ```text /var/www/rails/my_app/log/*.log { daily missingok copytruncate rotate 14 compress delaycompress notifempty } ``` -------------------------------- ### Initialize SemanticLogger and Add Appender Source: https://logger.rocketjob.io/index.html Sets the default log level and adds a file appender with colorized formatting. Use this to configure the logger globally. ```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'] ``` -------------------------------- ### Initialize Standard Ruby Logger Source: https://logger.rocketjob.io/appenders.html Creates a standard Ruby Logger instance for use with Semantic Logger. ```ruby ruby_logger = Logger.new($stdout) ``` -------------------------------- ### Configure Size-Based Log Rotation with logrotate Source: https://logger.rocketjob.io/log_rotation.html Use this configuration for high-volume applications to rotate logs once they reach 2GB in size. ```text /var/www/rails/my_app/log/*.log { size 2G missingok copytruncate rotate 7 compress nodelaycompress notifempty dateformat .%Y%m%d } ``` -------------------------------- ### Configure Amazing Print Options Source: https://logger.rocketjob.io/rails.html Sets custom options for the Amazing Print formatter. ```ruby config.rails_semantic_logger.ap_options = {multiline: false} ``` -------------------------------- ### Add JSON File Appender Source: https://logger.rocketjob.io/rails.html After disabling the default file logger, add a new appender to log to a JSON file. This example logs to `log/json.log` with a JSON formatter. ```ruby config.semantic_logger.add_appender(file_name: "log/json.log", formatter: :json) ``` -------------------------------- ### Configure Heroku Logging to Standard Out Source: https://logger.rocketjob.io/rails.html Add this configuration to `config/environments/production.rb` to direct all logs to standard output on Heroku. Ensure `$stdout.sync` is true and disable file appenders. ```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 ``` -------------------------------- ### Add Custom Data to Rails Completed Log Source: https://logger.rocketjob.io/rails.html To add custom data to the Rails Completed log message, define an `append_info_to_payload` method in your controller. This example adds `user_id: 42` to the payload. ```ruby class ThingController private def append_info_to_payload(payload) super payload[:user_id] = 42 end end ``` -------------------------------- ### Configure Unicorn for Semantic Logger Source: https://logger.rocketjob.io/forking.html Add this to the Unicorn configuration file to ensure logging continues after a fork. ```ruby # config/unicorn.conf.rb after_fork do |server, worker| # Re-open appenders after forking the process SemanticLogger.reopen end ``` -------------------------------- ### Create a Logger Instance Source: https://logger.rocketjob.io/api.html Create a stand-alone logger instance by providing the name of the class or the class itself. Each class should have its own logger instance for unique identification. ```ruby logger = SemanticLogger["MyClass"] ``` ```ruby logger = SemanticLogger[MyClass] ``` -------------------------------- ### Configure Stand-alone Frameworks for Semantic Logger Source: https://logger.rocketjob.io/forking.html Use these configurations for Passenger, Resque, or Spring when not using the Rails Semantic Logger gem. ```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 ``` -------------------------------- ### Log to Text File with Standard Formatter Source: https://logger.rocketjob.io/appenders.html Appends log messages to a specified file using the default formatter. Ensure the file path is correct. ```ruby SemanticLogger.add_appender(file_name: "development.log") ``` -------------------------------- ### Measure with Dynamic Log Level Source: https://logger.rocketjob.io/api.html Dynamically supply the log level as the first parameter to the measure method. This allows for flexible logging levels based on context. ```ruby logger.measure(:info, "Informational message such as request received") ``` -------------------------------- ### Configure Synchronous Logging Source: https://logger.rocketjob.io/api.html Methods to enable synchronous logging mode in Semantic Logger. ```ruby SemanticLogger.sync! ``` ```ruby require "semantic_logger/sync" ``` ```ruby gem "semantic_logger", require: "semantic_logger/sync" ``` -------------------------------- ### Integrate Sentry Error Tracking Source: https://logger.rocketjob.io/appenders.html Configures the Sentry appender and demonstrates how to pass context and tags. ```ruby SemanticLogger.add_appender(appender: :sentry_ruby) ``` ```ruby SemanticLogger.tagged(transaction_name: "foo", user_id: 42, baz: "quz") do logger.error("some message", username: "joe", fingerprint: ["bar"]) end ``` -------------------------------- ### Configure Minitest for Semantic Logger Source: https://logger.rocketjob.io/testing.html Include the Minitest helper module in your test_helper.rb to enable testing utilities. ```ruby Minitest::Test.include SemanticLogger::Test::Minitest ``` -------------------------------- ### Log Event Helper Methods Source: https://logger.rocketjob.io/log_struct.html Useful helper methods available on the Log Event object. ```APIDOC ## Log Event Helper Methods Other helper methods on the log event object: ### Methods | Method | Type | Description | |-------------------|-------------------|------------------------------------------------------------------------------------------------------------| | backtrace_to_s | `String` | The exception backtrace as a string, including the entire chain of exceptions. | | cleansed_message | `String` | Strip the standard Rails colorizing from the logged message. | | duration_human | `String` | The duration in human readable form. | | duration_to_s | `String` | The duration as a string in milli-seconds. | | each_exception | Enumerator | Iterate over the chain of exception objects. | | file_name_and_line(backtrace) | `[String,String]` | The file name and line number from the supplied backtrace. | | level_to_s | `String` | Single character upper case log level. | | metric_only? | `true` or `false` | A metric only event has a metric, but no message or exception. For example human readable text logs do not log metric only events, whereas JSON (machine readable) appenders generally would. | | payload_to_s | `String` or `nil` | The payload in text form, or nil if no payload present. | | payload? | `true` or `false` | Whether this log event has a payload. | ``` -------------------------------- ### Configure Heroku Log Level Environment Variable Source: https://logger.rocketjob.io/rails.html Add this to `config/environments/production.rb` to allow the log level to be set via the `LOG_LEVEL` environment variable on Heroku. ```ruby if ENV["LOG_LEVEL"].present? config.log_level = ENV["LOG_LEVEL"].downcase.strip.to_sym end ``` -------------------------------- ### Configure Metric Subscribers Source: https://logger.rocketjob.io/metrics.html Register subscribers to process log messages containing metrics. Subscribers run asynchronously in the Appender Thread. ```ruby subscriber = SemanticLogger::Metric::Statsd.new(url: "udp://localhost:8125") SemanticLogger.on_log(subscriber) ``` ```ruby subscriber = SemanticLogger::Metric::NewRelic.new SemanticLogger.on_log(subscriber) ``` ```ruby SemanticLogger.on_log do |log_struct| puts "Metric: #{log_struct.metric} with duration: #{log_struct.duration}ms" end ``` -------------------------------- ### Measure Duration Metrics Source: https://logger.rocketjob.io/metrics.html Use the measure_info method to automatically track the execution time of a block and report it as a metric. ```ruby logger.measure_info("Called supplier", metric: "supplier/add_user") do # Code to call external service ... end ``` ```ruby logger.info(message: "Called supplier", metric: "supplier/add_user", duration: 100.23) ``` -------------------------------- ### Add amazing_print and rails_semantic_logger to Gemfile Source: https://logger.rocketjob.io/rails.html Include these gems in your Gemfile to integrate Semantic Logger with Rails. `amazing_print` is optional but recommended for colored semantic data output. ```ruby gem "amazing_print" gem "rails_semantic_logger" ``` -------------------------------- ### Standard Logging Methods Source: https://logger.rocketjob.io/api.html Semantic Logger supports standard logging methods like info, debug, warn, error, fatal, and trace. Use the method name followed by the message to log. ```ruby logger.info("Hello World") ``` ```ruby logger.info? ``` ```ruby logger.trace("Low level trace information such as data sent over a socket") ``` ```ruby logger.debug("Debugging information to aid with problem determination") ``` ```ruby logger.info("Informational message such as request received") ``` ```ruby logger.warn("Warn about something in the system") ``` ```ruby logger.error("An error occurred during processing") ``` ```ruby logger.fatal("Oh no something really bad happened") ``` -------------------------------- ### Configure Apache Kafka Appender Source: https://logger.rocketjob.io/appenders.html Configures the Kafka appender to publish log messages to specified brokers. ```ruby SemanticLogger.add_appender( appender: :kafka, seed_brokers: ["kafka1:9092", "kafka2:9092"], ) ``` -------------------------------- ### Configure Graylog UDP Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to send logs to Graylog using the UDP protocol. Ensure Graylog is running and accessible at the specified URL. ```ruby SemanticLogger.add_appender( appender: :graylog, url: "udp://localhost:12201" ) ``` -------------------------------- ### Configure CloudWatch Logs Appender Source: https://logger.rocketjob.io/appenders.html Configures the CloudWatch Logs appender with region and stream settings. ```ruby SemanticLogger.add_appender( appender: :cloudwatch_logs, client_kwargs: {region: "eu-west-1"}, group: "/my/application", create_stream: true ) ``` -------------------------------- ### Configure Graylog TCP Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to send logs to Graylog using the TCP protocol. Ensure Graylog is running and accessible at the specified URL. ```ruby SemanticLogger.add_appender( appender: :graylog, url: "tcp://localhost:12201" ) ``` -------------------------------- ### Configure Puma for Semantic Logger Source: https://logger.rocketjob.io/forking.html Include this in the Puma configuration when running in clustered mode with application preloading. ```ruby # config/puma.rb before_worker_boot do # Re-open appenders after forking the process SemanticLogger.reopen end ``` -------------------------------- ### Measure Info with Block and Parameters Source: https://logger.rocketjob.io/api.html Measure the execution time of a code block using `measure_info` and provide optional parameters like `log_exception`, `min_duration`, and `metric`. The `log_exception` parameter controls how exceptions are logged. ```ruby log.measure_info(message, params=nil) do # Measure how long it takes to run this block of code end ``` ```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 Local Syslog Source: https://logger.rocketjob.io/appenders.html Appends log messages to the local syslog daemon. Ensure syslog is configured and running on the system. ```ruby SemanticLogger.add_appender(appender: :syslog) ``` -------------------------------- ### Configure RabbitMQ Appender Source: https://logger.rocketjob.io/appenders.html Configures the RabbitMQ appender to stream logs through a queue. ```ruby SemanticLogger.add_appender( appender: :rabbitmq, queue_name: "semantic_logger", rabbitmq_host: "localhost", username: "the-username", password: "the-password", ) ``` -------------------------------- ### Configure UDP Appender Source: https://logger.rocketjob.io/appenders.html Configures the UDP appender for sending logs via UDP protocol. ```ruby SemanticLogger.add_appender( appender: :udp, server: "localhost:8088" ) ``` ```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 ) ``` -------------------------------- ### Custom Formatter: No Process ID Source: https://logger.rocketjob.io/customize.html Create a formatter that omits the process ID from log messages, useful in environments like Docker where the PID is consistently 1. ```ruby class NoPidFormatter < SemanticLogger::Formatters::Default # Leave out the pid def pid end end SemanticLogger.add_appender(file_name: "development.log", formatter: NoPidFormatter.new) ``` ```ruby SemanticLogger.appenders.first.formatter = NoPidFormatter.new ``` -------------------------------- ### Available Measure Methods Source: https://logger.rocketjob.io/api.html Semantic Logger provides various methods for measuring code execution time at different log levels, from trace to fatal. ```ruby logger.measure_trace("Low level trace information such as data sent over a socket") do ... end ``` ```ruby logger.measure_debug("Debugging information to aid with problem determination") do ... end ``` ```ruby logger.measure_info("Informational message such as request received") do ... end ``` ```ruby logger.measure_warn("Warn about something in the system") do ... end ``` ```ruby logger.measure_error("An error occurred during processing") do ... end ``` ```ruby logger.measure_fatal("Oh no something really bad happened") do ... end ``` -------------------------------- ### Include File Name and Line Number in Log Messages Source: https://logger.rocketjob.io/rails.html Enable logging of the source file name and line number where a log message originated. Use with caution in production due to potential memory leaks from backtrace object allocation; best for development. ```ruby config.semantic_logger.backtrace_level = :info ``` -------------------------------- ### Configure Papertrail Appender Source: https://logger.rocketjob.io/appenders.html Configures the Papertrail appender using TLS TCP with a specified CA certificate file. ```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 with Metric Source: https://logger.rocketjob.io/api.html Measure the time taken to execute a block of code and assign a metric for dashboard aggregation. Use `measure_info` for informational messages. ```ruby logger.measure_info "Called external interface", metric: "Supplier/inquiry" do # Code to call external service ... end ``` -------------------------------- ### Applying Custom Formatter to Existing Appender in Rails Source: https://logger.rocketjob.io/customize.html Demonstrates how to find and modify the formatter for an existing file appender within a Rails application's initializer. ```ruby # Find file appender: appender = SemanticLogger.appenders.find{ |a| a.is_a?(SemanticLogger::Appender::File) } appender.formatter = MyFormatter.new ``` -------------------------------- ### Add Syslog and Net TCP Client Gems Source: https://logger.rocketjob.io/appenders.html Specifies the 'syslog_protocol' and 'net_tcp_client' gem dependencies in the Gemfile for remote syslog logging via TCP. ```ruby gem "syslog_protocol" gem "net_tcp_client" ``` -------------------------------- ### Configure Elasticsearch Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to forward log messages to Elasticsearch. Set the URL, index name, and enable data stream if required. ```ruby SemanticLogger.add_appender( appender: :elasticsearch, url: "http://localhost:9200", index: "my-index", data_stream: true ) ``` -------------------------------- ### Configure Log Output Format Source: https://logger.rocketjob.io/rails.html Sets the output format for Rails logs, supporting built-in formats, custom classes, or Procs. ```ruby config.rails_semantic_logger.format = :default ``` ```ruby config.rails_semantic_logger.format = :json ``` ```ruby # My Custom colorized formatter class MyFormatter < SemanticLogger::Formatters::Color # Return the complete log level name in uppercase def level "#{color}log.level.upcase#{color_map.clear}" end end ``` ```ruby config.rails_semantic_logger.format = MyFormatter.new ``` -------------------------------- ### JSON Log Format Structure Source: https://logger.rocketjob.io/appenders.html Illustrates the structure of log messages when using the JSON formatter. Fields with nil values are excluded from the output. The actual output is a single line. ```json { "timestamp": "ISO-8601", "application": "Application name", "environment": "Custom Environment name", "host": "Host name", "pid": "Process Id", "thread": "Thread name or id", "file": "filename", "line": "line number", "level": "trace|debug|info|warn|error|fatal", "level_index": "0|1|2|3|4|5", "message": "The message text without any colorization", "name": "Name of the class that generated the log message. Including namespace, if any.", "tags": ["tag_name 1", "tag_name 2"], "duration": "Human readable duration", "duration_ms": "Duration in milliseconds", "metric": "Name of the metric", "metric_amount": "Size of the metric, usually 1", "named_tags": { "tag1": "any named tags will be inside this named_tags tag", "tag2": "any named tags will be inside this named_tags tag" }, "payload": { "field1": "any custom payload fields will be inside this payload tag", "field2": "any custom payload fields will be inside this payload tag" }, "exception": { "name": "Exception class name", "message": "Exception message", "stack_trace": ["line 1", "line 2"], "cause": { "name": "Exception class name", "message": "Exception message", "stack_trace": ["line 1", "line 2"] } } } ``` -------------------------------- ### Add Log Appenders Source: https://logger.rocketjob.io/rails.html Configures additional destinations for log output, such as files, Syslog, Elasticsearch, or BugSnag. ```ruby config.semantic_logger.add_appender(file_name: "log/#{Rails.env}.json", formatter: :json) ``` ```ruby config.semantic_logger.add_appender(appender: syslog) ``` ```ruby config.semantic_logger.add_appender(appender: syslog, url: "tcp://myloghost:514") ``` ```ruby config.semantic_logger.add_appender(appender: :elasticsearch, url: "http://localhost:9200") ``` ```ruby config.semantic_logger.add_appender(appender: :bugsnag) ``` -------------------------------- ### Verify Log Events with Assertions Source: https://logger.rocketjob.io/testing.html Various ways to assert log event attributes, including partial message and payload matching. ```ruby assert_semantic_logger_event( messages[0], level: :info, message: "User enabled", ) ``` ```ruby assert_semantic_logger_event( messages[0], level: :info, message_includes: "enabled" ) ``` ```ruby assert_semantic_logger_event( messages[0], level: :info, message: "User enabled", payload: { first_name: "Jack", last_name: "Jones" } ) ``` ```ruby assert_semantic_logger_event( messages[0], level: :info, message: "User enabled", payload_includes: { first_name: "Jack" } ) ``` -------------------------------- ### Enable View Rendering Messages in Production Source: https://logger.rocketjob.io/rails.html Overrides the default debug-level logging for Action View rendering messages to show them in production. ```text ActionView::Base -- Rendered data/search/_user.html.haml (46.7ms) ``` ```ruby config.rails_semantic_logger.rendered = true ``` -------------------------------- ### Configure Logentries TCP Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to send logs to Logentries via TCP, using a custom formatter that includes the access token. Ensure the server and port are correct. ```ruby SemanticLogger.add_appender(appender: :tcp, server: "api.logentries.com:20000", ssl: true, formatter: formatter) ``` -------------------------------- ### Configure Multiple Appenders Source: https://logger.rocketjob.io/appenders.html Log messages to multiple destinations simultaneously, such as a local file and a remote Syslog server. Set the default logging level for all appenders. ```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") ``` -------------------------------- ### Add gelf gem to Gemfile Source: https://logger.rocketjob.io/appenders.html Add the 'gelf' gem to your application's Gemfile for Graylog integration. ```ruby gem "gelf" ``` -------------------------------- ### Configure Rails Minitest for Semantic Logger Source: https://logger.rocketjob.io/forking.html Add this to the test helper to support parallel testing with forking. ```ruby # test/test_helper.rb parallelize_setup do |worker| SemanticLogger.reopen end ``` -------------------------------- ### Integrate Honeybadger Source: https://logger.rocketjob.io/appenders.html Configures appenders for standard error forwarding or Insights event logging. ```ruby SemanticLogger.add_appender(appender: :honeybadger) ``` ```ruby SemanticLogger.add_appender(appender: :honeybadger_insights) ``` -------------------------------- ### Log Trace Message with Data Source: https://logger.rocketjob.io/index.html Logs low-level trace information, such as raw data received. Use the `:trace` level for detailed debugging. ```ruby raw_response = "jbloggsBloggsJoe" logger.trace "Raw data received from Supplier:", raw_response ``` -------------------------------- ### Log to Text File with Colorized Formatter Source: https://logger.rocketjob.io/appenders.html Appends log messages to a specified file using a colorized formatter for better readability in terminals. Ensure the file path is correct. ```ruby SemanticLogger.add_appender(file_name: "development.log", formatter: :color) ``` -------------------------------- ### Configure Splunk HTTP Collector Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to send logs to Splunk via the HTTP Event Collector. Replace the placeholder token with your actual Splunk token. ```ruby SemanticLogger.add_appender( appender: :splunk_http, url: "http://localhost:8088/services/collector/event", token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ) ``` -------------------------------- ### Measure and Log Execution Time Source: https://logger.rocketjob.io/index.html Measures the execution time of a block of code and logs it at the info level. Useful for performance monitoring. ```ruby logger.measure_info "Called external interface" do # Code to call external service ... sleep 0.75 end ``` -------------------------------- ### Add Count Metric with Specific Amount Source: https://logger.rocketjob.io/api.html Log a count metric with a specified amount. This allows for more granular tracking of event occurrences. ```ruby logger.error(message: "Calling Supplier", metric: "Supplier/inquiry", metric_amount: 21) ``` -------------------------------- ### Configure MongoDB Appender Source: https://logger.rocketjob.io/appenders.html Configures the MongoDB appender to store logs in a capped collection. ```ruby gem "mongo" ``` ```bash bundle install ``` ```bash gem install mongo ``` ```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) ``` ```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" } ``` -------------------------------- ### Measure with Optional Parameters Source: https://logger.rocketjob.io/api.html Measures code execution duration with advanced options like exception logging, minimum duration, payload, and silence level. ```APIDOC ## POST /websites/logger_rocketjob_io ### Description Measures code execution duration with advanced options like exception logging, minimum duration, payload, and silence level. ### Method POST ### Endpoint /websites/logger_rocketjob_io ### Parameters #### Request Body - **message** (string) - Required - The log message. - **params** (object) - Optional - A hash of settings: - **log_exception** (string) - Optional - Control exception logging ('full', 'partial', 'off'). Defaults to 'partial'. - **min_duration** (float) - Optional - Minimum duration in ms to log. Defaults to 0.0. - **payload** (object) - Optional - A hash payload. - **exception** (object) - Optional - A Ruby Exception object. - **duration** (float) - Optional - Pre-supplied duration in ms (if no block is given). - **metric** (string) - Optional - Name of the metric to publish. - **silence** (string) - Optional - Log level to silence within the block. - **on_exception_level** (string) - Optional - Log level to increase to if an exception is raised. - **block** (code block) - Required if `:duration` is not provided - The code to measure. ### Request Example ```json { "message": "Called external interface", "params": { "log_exception": "full", "min_duration": 100, "metric": "Custom/Supplier/process" } } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success. #### Response Example ```json { "status": "success" } ``` ``` -------------------------------- ### Configure Semantic Logger for Elasticsearch Source: https://logger.rocketjob.io/centralized_logging.html Add this code to a Rails initializer or your application to configure Semantic Logger to forward logs to Elasticsearch. Ensure Elasticsearch is running at the specified URL. ```ruby SemanticLogger.add_appender( appender: :elasticsearch, url: "http://localhost:9200" ) ``` -------------------------------- ### Log File Name and Line Number Source: https://logger.rocketjob.io/rails.html Set `config.semantic_logger.backtrace_level = :debug` to log the file name and line number for log messages at the debug level and above. Capturing backtraces for every message can be expensive. ```ruby # Log file name and line number for log messages at this level and above config.semantic_logger.backtrace_level = :debug ``` -------------------------------- ### Measure Info with Block Source: https://logger.rocketjob.io/api.html Measures the duration of a code block and logs it with an informational message and an optional metric name. ```APIDOC ## POST /websites/logger_rocketjob_io ### Description Measures the duration of a code block and logs it with an informational message and an optional metric name. ### Method POST ### Endpoint /websites/logger_rocketjob_io ### Parameters #### Request Body - **message** (string) - Required - The informational message to log. - **metric** (string) - Optional - The name of the metric to associate with this measurement. - **block** (code block) - Required - The code to execute and measure. ### Request Example ```json { "message": "Called external interface", "metric": "Supplier/inquiry" } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success. #### Response Example ```json { "status": "success" } ``` ``` -------------------------------- ### Send SIGTTIN Signal Source: https://logger.rocketjob.io/signals.html Command to trigger a thread dump for a specific process ID. ```bash kill -TTIN 1234 ``` -------------------------------- ### Log Error Message with Details Source: https://logger.rocketjob.io/index.html Logs an error message along with key-value pairs for additional context. Useful for capturing error details. ```ruby logger.error("Oops external call failed", :result => :failed, :reason_code => -10) ``` -------------------------------- ### Configure TCP Appender Source: https://logger.rocketjob.io/appenders.html Configures the TCP appender for sending logs to a remote service. Supports SSL and custom message formatting. ```ruby SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088" ) ``` ```ruby SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088", ssl: true ) ``` ```ruby SemanticLogger.add_appender( appender: :tcp, server: "localhost:8088", ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE} ) ``` ```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 ) ``` -------------------------------- ### Measure Code Execution Time Source: https://logger.rocketjob.io/api.html Measure the execution time of a block of code and log the duration. This helps in identifying performance bottlenecks. ```ruby logger.measure_info "Called external interface" do # Code to call external service ... end ``` -------------------------------- ### Use SemanticLogger::Loggable Mixin Source: https://logger.rocketjob.io/api.html Include the SemanticLogger::Loggable mixin within classes to easily access logger instances from both class and instance methods. This ensures log entries are identified by the correct class. ```ruby include SemanticLogger::Loggable ``` ```ruby class Supplier # Include class and instance logger variables 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 end ``` -------------------------------- ### Custom Formatter: Log Struct Inspection Source: https://logger.rocketjob.io/customize.html Use a custom formatter proc to inspect the entire Log Struct. This is useful for debugging or creating highly specific log outputs. ```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" ``` -------------------------------- ### Configure HTTP Appender Source: https://logger.rocketjob.io/appenders.html Configures the HTTP appender to send JSON log messages, with options for HTTPS and custom formatting. ```ruby SemanticLogger.add_appender( appender: :http, url: "http://localhost:8088/path" ) ``` ```ruby SemanticLogger.add_appender( appender: :http, url: "https://localhost:8088/path" ) ``` ```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 ) ``` -------------------------------- ### IO Stream Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to write logs to any IO Stream instance, such as $stderr or $stdout. ```APIDOC ## IO Streams ### Description Semantic Logger can log data to any IO Stream instance, such as $stderr or $stdout. ### Method `SemanticLogger.add_appender` ### Endpoint N/A (Configuration) ### Parameters #### Query Parameters - **io** (IO) - Required - The IO stream to log to (e.g., $stderr, $stdout). - **level** (symbol or string) - Optional - The minimum log level to write to this appender. Defaults to :trace. ### Request Example ```ruby # Log errors and above to standard error: SemanticLogger.add_appender(io: $stderr, level: :error) ``` ### Response N/A (Configuration) ``` -------------------------------- ### Configure Custom Signal Handlers Source: https://logger.rocketjob.io/signals.html Registers specific signals for log level changes and thread dumps, and sets a custom threshold for JRuby GC 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) ``` -------------------------------- ### Add Duration Metric to Log Entry Source: https://logger.rocketjob.io/api.html Log a duration metric, indicating the time taken for an operation. This is helpful for performance analysis. ```ruby logger.error(message: "Calling Supplier", metric: "Supplier/inquiry", duration: 100) ``` -------------------------------- ### Log Causal Exceptions with Semantic Logger Source: https://logger.rocketjob.io/api.html Shows how Semantic Logger automatically logs both the primary exception and its cause. ```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 ``` -------------------------------- ### Log to Remote Syslog via UDP Source: https://logger.rocketjob.io/appenders.html Appends log messages to a remote syslog server using UDP. Specifies the server URL. Requires the 'syslog_protocol' gem. ```ruby SemanticLogger.add_appender( appender: :syslog, url: "udp://myloghost:514" ) ``` -------------------------------- ### Integrate Rollbar Error Handling Source: https://logger.rocketjob.io/appenders.html Registers a callback to forward error-level logs to Rollbar. ```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 ``` -------------------------------- ### Syslog Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to write logs to a local or remote Syslog daemon. ```APIDOC ## Syslog ### Description Log to a local Syslog daemon or a remote Syslog server. ### Method `SemanticLogger.add_appender` ### Endpoint N/A (Configuration) ### Parameters #### Query Parameters - **appender** (symbol) - Required - Set to `:syslog`. - **url** (string) - Optional - The URL for a remote Syslog server (e.g., "tcp://myloghost:514", "udp://myloghost:514"). - **max_size** (integer) - Optional - Maximum packet size in bytes for remote Syslog (default: 1024). - **filter** (Proc) - Optional - A Proc to filter log messages. ### Request Example ```ruby # Log to a local Syslog daemon: SemanticLogger.add_appender(appender: :syslog) # Log to a remote Syslog server using TCP: SemanticLogger.add_appender( appender: :syslog, url: "tcp://myloghost:514", max_size: 2048 ) # Log to a remote Syslog server using UDP: SemanticLogger.add_appender( appender: :syslog, url: "udp://myloghost:514" ) # Optional: Add filter to exclude specific log entries: SemanticLogger.add_appender( appender: :syslog, url: "udp://myloghost:514", filter: Proc.new { |log| log.message !~ /(health_check|Not logged in)/ } ) ``` ### Dependencies - For remote Syslog via UDP: `gem "syslog_protocol"` - For remote Syslog via TCP: `gem "syslog_protocol"`, `gem "net_tcp_client"` ### Response N/A (Configuration) **Note:** `:trace` level messages are mapped to `:debug` for Syslog. ``` -------------------------------- ### Text File Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to write logs to a text file. Supports standard, colorized, and JSON formats. ```APIDOC ## Text File Appender ### Description Log to file with the standard formatter. ### Method `SemanticLogger.add_appender` ### Endpoint N/A (Configuration) ### Parameters #### Query Parameters - **file_name** (string) - Required - The name of the log file. - **formatter** (symbol or string) - Optional - The formatter to use (:color, :json, etc.). Defaults to standard. ### Request Example ```ruby SemanticLogger.add_appender(file_name: "development.log") SemanticLogger.add_appender(file_name: "development.log", formatter: :color) SemanticLogger.add_appender(file_name: "development.log", formatter: :json) ``` ### Response N/A (Configuration) ### JSON Log Format #### Description Details the structure of log messages when using the JSON formatter. #### Response Example ```json { "timestamp": "ISO-8601", "application": "Application name", "environment": "Custom Environment name", "host": "Host name", "pid": "Process Id", "thread": "Thread name or id", "file": "filename", "line": "line number", "level": "trace|debug|info|warn|error|fatal", "level_index": "0|1|2|3|4|5", "message": "The message text without any colorization", "name": "Name of the class that generated the log message. Including namespace, if any.", "tags": ["tag_name 1", "tag_name 2"], "duration": "Human readable duration", "duration_ms": "Duration in milliseconds", "metric": "Name of the metric", "metric_amount": "Size of the metric, usually 1", "named_tags": { "tag1": "any named tags will be inside this named_tags tag", "tag2": "any named tags will be inside this named_tags tag" }, "payload": { "field1": "any custom payload fields will be inside this payload tag", "field2": "any custom payload fields will be inside this payload tag" }, "exception": { "name": "Exception class name", "message": "Exception message", "stack_trace": ["line 1", "line 2"], "cause": { "name": "Exception class name", "message": "Exception message", "stack_trace": ["line 1", "line 2"] } } } ``` **Note:** The actual JSON output is a single line. Fields with nil values are excluded. ``` -------------------------------- ### Available Logging Levels Source: https://logger.rocketjob.io/api.html Semantic Logger provides the following logging levels: :trace, :debug, :info, :warn, :error, :fatal. Levels are ordered by precedence, with :trace being the most detailed and :fatal the least. ```text :trace, :debug, :info, :warn, :error, :fatal ``` -------------------------------- ### Log Message with Semantic Information Source: https://logger.rocketjob.io/index.html Log an informational message with additional key-value pairs for semantic context, such as duration, result, table, and action. ```ruby logger.info('Queried table', duration: duration, result: result, table: 'users', action: 'query') ``` -------------------------------- ### Log to Text File in JSON Format Source: https://logger.rocketjob.io/appenders.html Appends log messages to a specified file in JSON format. This is useful for structured logging and integration with log analysis tools. Ensure the file path is correct. ```ruby SemanticLogger.add_appender(file_name: "development.log", formatter: :json) ``` -------------------------------- ### Configure Loggly HTTP Appender Source: https://logger.rocketjob.io/appenders.html Configure Semantic Logger to send logs to Loggly via HTTP. Replace the placeholder token and ensure the URL is correct for your Loggly input. ```ruby SemanticLogger.add_appender( appender: :http, url: "http://logs-01.loggly.com/inputs/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/tag/semantic_logger/" ) ``` -------------------------------- ### Measure Info with Manual Duration Source: https://logger.rocketjob.io/api.html Manually supply the duration for a measure log entry when a code block is not used. This is useful when the duration is already known. ```ruby duration = Time.now - start_time logger.measure_info "Called external interface", duration: duration ``` -------------------------------- ### Log a Message with Semantic Logger Source: https://logger.rocketjob.io/customize.html Log an informational message using Semantic Logger. Obtain a logger instance for a specific tag and use its methods like `info`, `warn`, `error`, etc. ```ruby logger = SemanticLogger["Hello"] logger.info "Hello World" ```