Skip to content

logdna/ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ruby gem for logging to LogDNA


All Contributors

Installation

Add this line to your application's Gemfile:

gem 'logdna'

And then execute:

$ bundle

Or install it yourself as:

$ gem install logdna

Quick Setup

After installation, call

logger = Logdna::Ruby.new(your_api_key, options)
#<Logdna::Ruby:0x00000000000000>

to set up the logger.

Options are optional variables that may contain hostname, app name, mac address, ip address, log level specified.

options = {
    :hostname => myHostName,
    :ip =>  myIpAddress,
    :mac => myMacAddress,
    :app => myAppName,
    :level => "INFO",    # LOG_LEVELS = ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'] or your customized log level (custom levels for Rails have to be sent with a log message)
    :env => "PRODUCTION",
    :meta => {:once => {:first => "nested1", :another => "nested2"}},
    :endpoint => "https://fqdn/logs/ingest"
}

To send logs, use "log" method. Default log level is "INFO"

logger.log('This is my first log')
=> "Saved"  # Saved to buffer. Ready to be flushed automatically

Optionally you can use a block to do so

logger.log { 'This is my second log' }
=> "Saved"

Log a message with particular metadata, level, appname, environment (one-time)

logger.log('This is warn message', {:meta => {:meta => "data"}, :level => "WARN", :app => "awesome", :env => "DEVELOPMENT"})

Log a message with lasting metadata, level, appname, environment (lasting)

logger.meta = {:once => {:first => "nested1", :another => "nested2"}}
logger.level = 'FATAL'  or  logger.level = Logger::FATAL
logger.app = 'NEW APP NAME'
logger.env = 'PRODUCTION'
logger.log('This messages and messages afterwards all have the above values')

Clear current metadata, level, appname, environment

logger.clear

Check current log level: logger.info? => true logger.warn? => false

Log a message with a particular level easily

logger.warn('This is a warning message')
logger.fatal('This is a fatal message')
logger.debug { 'This is a debug message' }

Hostname and app name cannot be more than 80 characters.

Rails Setup

In your config/environments/environment.rb:

Rails.application.configure do
  config.logger = Logdna::Ruby.new(your_api_key, options)
end

Important Notes

  1. This logger assumes that you pass in json formatted data
  2. This logger is a singleton (do not create mutiple instances of the logger) even though the singleton structure is not strongly enforced.

API

Logdna::Ruby.new(ingestion_key, options = {})

Instantiates a new instance of the class it is called on. ingestion_key is required.

Options Default
{ :hostname => Host name } Device's default hostname
{ :mac => MAC address } Nil
{ :ip => IP address } Nil
{ :app => App name } 'default'
{ :level => Log level } 'INFO'
{ :env => STAGING, PRODUCTION .. etc} Nil
{ :meta => metadata} Nil
{ :endpoint => LogDNA Ingestion URI 'https://logs.logdna.com/logs/ingest'
{ :flush_interval => Limit to trigger a flush in seconds } 0.25 seconds
{ :flush_size => Limit to trigger a flush in bytes } 2097152 bytes = 2 MiB
{ :request_size => Upper limit of request in bytes } 2097152 bytes = 2 MiB
{ :retry_timeout => Base timeout for retries in seconds } 0.25 seconds
{ :retry_max_attempts => Maximum number of retries per request } 3 attempts
{ :retry_max_jitter => Maximum amount of jitter to add to each retry request in seconds } 0.25 seconds

Different log level displays log messages in different colors as well.

  • TRACE DEBUG INFO Colors "Trace" "Debug" "Info"
  • WARN Color "Warn"
  • ERROR Fatal Colors "Error" "Fatal"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/logdna/ruby.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Amadeus Folego

πŸ›

Brad

πŸ’» πŸ“–

DChai

πŸ‘€ πŸ’»

Dee Evans

πŸ›

Greg Swift

πŸ’»

Gun Woo choi

πŸ’» πŸ“–

Jacob Hull

πŸ’»

Jon Moses

⚠️

Kenneth-KT

πŸ’»

Mansoor

πŸ›

Mike Machado

πŸ›

Muaz Siddiqui

πŸ’» 🚧 πŸ‘€

Oleg Kiviljov

πŸ›

Peter JΓΆnsson

πŸš‡

Phil Ciampini

πŸ’» ⚠️

Samir Musali

πŸ’» πŸ“– 🚧

vilyapilya

πŸ’» πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

License

The gem is available as open source under the terms of the MIT License.