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.
Related
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')
I have a ruby application (not Rails) using Resque. I'd like to know what's going on in one of the worker I have.
To do that, I use the Logger class as stated in the officiel documentation.
Below is how I log inside a worker:
require 'resque'
require 'logger'
<code>
Resque.logger = Logger.new File.new('logfile.log', 'a')
Resque.logger.info "Whatever"
However, when running my worker, nothing is actually being logged. It's as if the worker is actually ignoring all those log instructions. No error is raised. The other pieces of code actually work - only the logging part is ignored.
I tried to use the logger class itself (ie logger = Logger.new) but the result is the same.
Do you have any idea on how I can actually log something inside my resque worker?
Thanks!
Use the following:
Logger.new(path_to_log_file).info(anything)
You are missing the log level to enabled printing info logs, according to the documentation you linked there, you can use something like this
Resque.logger.level = Logger::DEBUG
then if you look into Logger docs it lists levels as
DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
so if you are using logger.info you either need Logger::DEBUG or Logger::INFO to have the logged string to show up.
Also make sure the logger initialization is run by the process running the workers, and not just the process enqueueing jobs.
my environment: rails 5.0.1, resque: 1.26.0.
I tried config the resque logger in config/initializers/resque.rb and lib/tasks/resuqe.rake, they neither work for me.
but config the resque logger inside every job, that works for me, although it is not perfect.
# update_xxx_job.rb
class UpdateXxxJob
def self.perform
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::DEBUG
Resque.logger.info 'Job starts'
...
end
end
more details in another answer.
I am playing around with writing a Sinatra app. I'd like to log something from the configuration blocks. However, that runs in the application scope, which doesn't include the logger methods if I do enable :logging in my configure block. Is there some normal way to log messages from the configure blocks while respecting whatever log configuration I have in effect?
Currently, I do something like the following:
class Blah < Sinatra::Base
configure do
enable :logging
##log = Logger.new("/dev/stderr")
##log.info "blah"
end
end
I'd rather use whatever logger is configured and default to one setup by the enable :logger line. Something similar to the following would be ideal:
class Blah < Sinatra::Base
configure do
enable :logging
logger.info "blah"
end
end
As of 1.3 (i think) Sinatra ships with a logger like you described, but it writes by default to STDOUT and STDERR, if you want to hit it to a file add this to your config.ru:
logger = Logger.new('log/awesome_app.log')
use Rack::CommonLogger, logger
run AwesomeApp
I have a Sinatra app that, boiled down, looks basically like this:
class MyApp < Sinatra::Base
configure :production do
myConfigVar = read_config_file()
end
configure :development do
myConfigVar = read_config_file()
end
def read_config_file()
# interpret a config file
end
end
Unfortunately, this doesn't work. I get undefined method read_config_file for MyApp:Class (NoMethodError)
The logic in read_config_file is non-trivial, so I don't want to duplicate in both. How can I define a method that can be called from both my configuration blocks? Or am I just approaching this problem in entirely the wrong way?
It seems the configure block is executed as the file is read. You simply need to move the definition of your method before the configure block, and convert it to a class method:
class MyApp < Sinatra::Base
def self.read_config_file()
# interpret a config file
end
configure :production do
myConfigVar = self.read_config_file()
end
configure :development do
myConfigVar = self.read_config_file()
end
end
Your configure blocks are run when the class definition is evaluated. So, the context is the class itself, not an instance. So, you need a class method, not an instance method.
def self.read_config_file
That should work. Haven't tested though. ;)
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"