What is the best Rails Logging Gem - ruby-on-rails

what is the best way of configuring Logging features on a rails project? I'm looking for something like Log4J which is available to Rails. I have found log4r and it's conflicting built in Logger class and also tried 'Logging' gem and It has some issues configuring as a audit logger. Please let me know your suggestions on this topic since I'm a beginner on the subject.
I have used below code block in logging.rb and included in environment.rb
But I'm receiving a error on 'returning' keyword as it's deprecated on rails 2.8
config/environment.rb
# Logging
require File.join(File.dirname(__FILE__), 'logging')
Rails::Initializer.run do |config|
config/logging.rb
require 'logging'
# Logging.init is required to avoid
# unknown level was given 'info' (ArgumentError)
# or
# uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal
layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"
# Default logfile, history kept for 10 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
:filename => 'log/default.log', :age => 'daily', :keep => 10, :safe => true, :layout => layout
# Audit logfile, history kept forever
audit_appender = Logging::Appenders::RollingFile.new 'audit', \
:filename => 'log/audit.log', :age => 'daily', :safe => true, :layout => layout
# Production logfile, history kept forever
prod_appender = Logging::Appenders::RollingFile.new 'prod', \
:filename => 'log/production.log', :age => 'daily', :safe => true, :layout => layout
DEFAULT_LOGGER = returning Logging::Logger['server'] do |l|
l.add_appenders default_appender
end

Have a look at the following threads:
Rails Logging API
logging in rails app
What's a good logging replacement for rails?

It should be like this:
config/logging.rb
require 'logging'
# Logging.init is required to avoid
# unknown level was given 'info' (ArgumentError)
# or
# uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal
layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"
# Default logfile, history kept for 30 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
:filename => "log/#{Rails.env}.log", :age => 'daily', :keep => 30, :safe => true, :layout => layout
log = Logging::Logger[:root]
log.add_appenders default_appender
log.level = :info
Rails.logger = log

Related

Hide server logs for send_file method

Rails version: 4.2.0
Ruby version: 2.3.8
Is there any way in a Rails application to hide the absolute path displayed for the send_file method?
controller code:
def download_file(file_path)
send_file file_path, :x_sendfile => true, :type => 'application/pdf'
end
the path displayed in server logs:
Sent file /home/new_user/Project/new_project/pdfs/plan_7.pdf
Check the config/production.rb and change log level.
Try something like that:
config.log_level = :fatal
The level can be: :debug, :info, :warn, :error, :fatal, and :unknown. Choise the best option for your case.

“Uninitialized constant” error when trying to create Client

I am working on dashing dashboard and trying to add a Jira widget.In the widget code it is trying to create and initialize a Client object. As shown in code below
require 'Jira'
SCHEDULER.every '5m', :first_in => 0 do |job|
client = Jira::Client.new({
:username => ENV['Talal'],
:password => ENV['Talal123'],
:site => "http://192.168.99.100:32768",
:auth_type => :basic,
:context_path => "/jira"
})
But as I gets to this line client = Jira::Client.new. An exception occurs which states that uninitialized constant Jira::Client.
I believe the gem you should be using is jira-ruby. Once you install it, Bundler should automatically require it for you, meaning if you're in a Rails environment, you shouldn't need to do require 'Jira'.
If you don't want it required application-wide, BTW, you should add this to your Gemfile:
gem 'jira-ruby', require: false
# then in your scheduler, you have to explicitly require it as before:
require 'jira-ruby'
SCHEDULER.every '5m', :first_in => 0 do |job|
client = Jira::Client.new({
:username => ENV['Talal'],
:password => ENV['Talal123'],
:site => "http://192.168.99.100:32768",
:auth_type => :basic,
:context_path => "/jira"
})
Additional information here

How do I tell a Rails 4 app where my SSL certs are?

I'm trying to tell a Rails 4 app where to find the SSL certificates.
Ww got it working by modifying the bin/rails, adding this
if ENV['SSL'] == "true"
module Rails
class Server < ::Rack::Server
def default_options
super.merge({
:Port => 443,
:environment => (ENV['RAILS_ENV'] || "development").dup,
:daemonize => false,
:debugger => false,
:pid => File.expand_path("tmp/pids/server.pid"),
:config => File.expand_path("config.ru"),
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(
File.open("/path/to/my/private/.key").read),
:SSLCertificate => OpenSSL::X509::Certificate.new(
File.open("/path/to/my.cert").read),
:SSLCertName => [["CN", WEBrick::Utils::getservername]],
})
end
end
end
end
all good but I want these setting in my config/environments/staging.rb file instead, and I want it to be non-specific to Webbrick.
Is this the right approach, and
if so, how do I move these settings into config/environments/staging.rb?
The consensus from the comments is:
No, and
see 1.
Thanks #tpbowden and #josh-deeden for your quick responses.

How to "crawl" only the root URL with Anemone?

In the example below I would like anemone to only execute on the root URL (example.com). I am unsure if I should apply the on_page_like method and if so what pattern I would need.
require 'anemone'
Anemone.crawl("http://www.example.com/") do |anemone|
anemone.on_pages_like(???) do |page|
# some code to execute
end
end
require 'anemone'
Anemone.crawl("http://www.example.com/", :depth_limit => 1) do |anemone|
# some code to execute
end
You can also specify the following in the options hash, below are the defaults:
# run 4 Tentacle threads to fetch pages
:threads => 4,
# disable verbose output
:verbose => false,
# don't throw away the page response body after scanning it for links
:discard_page_bodies => false,
# identify self as Anemone/VERSION
:user_agent => "Anemone/#{Anemone::VERSION}",
# no delay between requests
:delay => 0,
# don't obey the robots exclusion protocol
:obey_robots_txt => false,
# by default, don't limit the depth of the crawl
:depth_limit => false,
# number of times HTTP redirects will be followed
:redirect_limit => 5,
# storage engine defaults to Hash in +process_options+ if none specified
:storage => nil,
# Hash of cookie name => value to send with HTTP requests
:cookies => nil,
# accept cookies from the server and send them back?
:accept_cookies => false,
# skip any link with a query string? e.g. http://foo.com/?u=user
:skip_query_strings => false,
# proxy server hostname
:proxy_host => nil,
# proxy server port number
:proxy_port => false,
# HTTP read timeout in seconds
:read_timeout => nil
My personal experience is that Anemone was not very fast and had a lot of corner cases. The docs are lacking (as you have experienced) and the author doesn't seem to be maintaining the project. YMMV. I tried Nutch shortly but didn't play aroud as much but it seemed faster. No benchmarks, sorry.

Ruby daemon script only runs once

I've written a ruby NFC reader script and daemonised it with the daemons gem. It all works great except the script only runs once...
Daemon.rb
require 'rubygems'
require 'daemons'
pwd = File.dirname(File.expand_path(__FILE__))
file = pwd + '/touchatag.rb'
Daemons.run_proc(
'touchatag_project_daemon', # name of daemon
:dir_mode => :normal,
:dir => File.join(pwd, 'tmp/pids'), # directory where pid file will be stored
:backtrace => true,
:monitor => true,
:log_output => true
) do
exec "ruby #{file}"
end
touchatag.rb
quire 'rubygems'
require 'nfc'
require 'httparty'
class TagAssociator
include HTTParty
base_uri 'localhost:3000'
end
NFC.instance.find do |tag|
puts "Processing tag..."
TagAssociator.post('/answers/answer', :query => {:uid => tag.uid})
end
This works great and i'm receiving the tag.uid in my app. But when i scan another RFID tag the script wont run again...
Does anyone know how to adjust the script that it runs "forever" and stops when the daemon is stopped?
Thanks
UPDATE
i updated my daemon.rb script:
require 'rubygems'
require 'daemons'
options = {
:app_name => "touchatag_project_daemon",
:ARGV => ['start', '-f', '--', 'param_for_myscript'],
:dir_mode => :script,
:dir => 'tmp/pids',
:multiple => true,
:ontop => true,
# :mode => :exec,
:backtrace => true,
:monitor => true
}
Daemons.run(File.join(File.dirname(__FILE__), '/touchatag.rb'), options)
But it just runs once... i don't get it any other suggestions?
You almost certainly want to be using Daemon.run. run_proc would be useful if you wanted to move the code from touchtag.rb into Daemon.rb.
http://daemons.rubyforge.org/classes/Daemons.html#M000004

Resources