Ruby gem is overriding standard library - ruby

When trying to create a new ruby logger:
logger = Logger.new('my.log')
I'm getting an error:
NoMethodError: undefined method `new' for HTTParty::Logger:Module
Creating the logger from the Rails Console works fine. When trying to create it from the class is when it's getting the error. It appears that HTTParty is interfering with it. How do I specify that I wish to use the stdlib Logger instead of the gem HTTParty's Logger?

If you want to get the constant in 'root' namespace, you can use :: operator, like this:
logger = ::Logger.new('my.log')

Related

Ruby logger scope

I am trying to create a simple logging solution for my program using ruby's built in logger function. At the moment, what I have is the initialization for the logger like this:
class Setup
def initialize
logger = Logger.new(logfile.log)
logger.level = 'DEBUG'
logger.datetime_format = '%Y-%m-%d %H:%M:%S'
And then when I try to call to the logger within other functions in the same class with:
logger.info('testlog')
I get an error saying:
undefined local variable or method 'logger'
How should I access the logger from outside the local scope of the method it is defined in?
Thanks in advance.
logger is a local variable, which is only visible in the area where it was defined. Rename it to #logger to make it be an instance variable which is stored inside the object and can be used from any of the object's methods.

Undefined Method Formatter for Log4r in RAILS 4.0

I am getting this error after upgrading Rails from 3.1.2 to 4.0. When launching my server with rails s I got stuck with the following error
C:/ruby-2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:78:in `start': undefined method `formatter' for #<Log4r::Logger:0x26dd908> (NoMethodError)
I have been on the Log4r site but haven't got any infor;ation about a bug when upgrading Rails.
Does anyone have any idea where this bug comes from. Thank you!
The method formatter is not defined on Log4r::Logger, but on Log4r::FileOutputter. Therefore I am surprised that is worked before the Rails update. Perhaps that changed between different versions of Log4r.
Please try the following (with adjusted filenames and patters):
require 'log4r'
outputter = Log4r::FileOutputter.new('log4r', filename: 'foobar.log')
outputter.formatter = Log4r::PatternFormatter.new(
date_pattern: "%FT%T.000Z", pattern: "%d [%l] %m"
)
logger = Log4r::Logger.new('log4r')
logger.outputters = [outputter]
Add this code to config/application.rb or to a new file like config/initializers/logger.rb

can't convert Symbol into String

I have the following code in Ruby, take directly from the Getting Started with Rails guide
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
When I run the above Create I get the following error.
can't convert Symbol into string
It seems like you are trying to use strong paramaters. You get this error cannot convert symbol into string because you have not configured the strong_parameters. So by default you cant use require on params with symbols.
Configure strong parameters as follows:
1.) Add gem 'strong_parameters' to your gemfile and bundle it.
2.) Include Restrictions to you model as follows.
include ActiveModel::ForbiddenAttributesProtection to your model.
3.) Disable white listing in application confiuration(config/application.rb)
config.active_record.whitelist_attributes = false
See the documentation for more details on configuring.
Now your code should work.
If anyone is using Mongoid, you can fix this issue by adding the following to an initializer:
Mongoid::Document.send(:include, ActiveModel::ForbiddenAttributesProtection)
Add gem 'strong_parameters' to the gem file and
run >bundle install in command prompt
Refresh the browser.

Sinatra Helper in External File gives LoadError

I'm trying to add a helper to connect to a mongo db to my modular Sinatra application
When I type foreman start in my console I get:
/home/sunny/Programs/landing_pages/app.rb:17:in `block in <class:LandingPages>': undefined local variable or method `connect' for LandingPages:Class (NameError)
My app.rb file looks like this:
require 'sinatra/base'
require 'sinatra/partial'
require 'sinatra/db_helper'
require 'bundler/setup'
require 'mongo'
class LandingPages < Sinatra::Base
helpers Sinatra::DbHelper
configure do
$collection = connect
end
end
My ./lib/sinatra/db_helper.rb looks like this:
require 'sinatra/base'
module Sinatra
module DbHelper
def connect
conn = Mongo::Connection.new("localhost")
db = conn.db("leads")
db.collection("laws")
end
end
helpers DbHelper
end
My config.ru looks like this:
require './app'
run LandingPages
I thought I was following the instructions correctly on:
http://www.sinatrarb.com/extensions.html
but I'm not totally sure. I'm not making a gem but just a sinatra app so maybe my directory hierarchy isn't correct. I don't have a rake file or a gem spec. Do I need them?
Some googling also found this:
sinatra helper in external file
Dave Sag answers my question perfectly but I can't get it work.
This comes about because of the scope of methods created through the helpers is on the sinatra application instance, since it calls ruby's include under the hood. So this would work:
get '/some/route' do
db = connect
# do something else ...
end
But the configure block has a class scope, so it can be used for configuring the application as a whole. So to make this work, you can define the method as:
module Sinatra
module DbHelper
def self.connect
conn = Mongo::Connection.new("localhost")
db = conn.db("leads")
db.collection("laws")
end
end
end
which could then be called via: $collection = Sinatra::DbHelper.connect or perhaps more favoured, you could call register instead of helpers. register calls extend under the hood, so you end up with class level methods (if you extend a class, anyway). You could then make the configure block as so:
configure do |app|
$collection = app.connect
end
You could also do all of this in an registered method on the DbHelpers module. See the example in the documentation for how this might work.

Error "undefined local variable or method `logger'" when using `logger.info` in a Sinatra application

I have the following Sinatra 1.2.1 application code:
# app.rb
require 'sinatra'
get '/' do
logger.info "COUCOU"
'Hello world!'
end
and start the server with ruby -rubygems app.rb. When I go to http://localhost:4567 I get the error:
NameError at /
undefined local variable or method `logger' for #<Sinatra::Application:0x00000100d91f88>
file: app.rb location: block in <main> line: 4
Do I need to add or configure something to enable logging in Sinatra? Reading the Sinatra README and documentation, it looks like logging is enabled by default for Sinatra::Application.
The problem is in the not found write method, just extend the Logger class this way and everything should be ok:
class Logger
# Make Rack::CommonLogger accept a Logger instance
# without raising undefined method `write' for #<Logger:0x007fc12db61778>
# makes a method alias using symbols
alias :write :<<
end
You are probably missing a logger = Logger.new.
http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/
Logger is not defined, to overcome it you can just use
Rails.logger.info "COUCOU"
or define it like this:
logger = Rails.logger.new
logger.info "COUCOU"

Resources