How to configure prosopite gem in rails for custom logger file.
I need to separate file for specs and API scan details.
for example:
prosopite_api.log file for API scan details.
prosopite_specs.log file for Specs scan details.
In the readme file they have provided a way, by passing a custom logger class to prosopite.custom_logger, but I do not want to create this custom class and also don't have a need for the separate custom logger class, What I need is just a separate logger file for specs.
We can just pass a new instance of the existing logger class with the file name we want.
for example:
Prosopite.custom_logger = Logger.new('log/custom_file_name.log')
This way I didn't have to create a new custom logger class and configured a separate file for specs(By adding this in test.rb).
development.rb:
config.after_initialize do
Prosopite.prosopite_logger = true
Prosopite.custom_logger = Rails.logger
end
test.rb
config.after_initialize do
Prosopite.custom_logger = Logger.new('log/prosopite_specs.log')
end
Related
I'm currently working on the GCP and deployed my rails app on the GCP instance.
I'm currently using https://github.com/brendeschuijmert/google-cloud-ruby/tree/master/google-cloud-logging for the compute engine.
When I use this on the rails application.
Rails.logger.info "Hello World" works well.
But logger.warn "Hola Mundo" is not working.
I want someone shade some light on this problem.
Thanks
If you're trying to call logger from outside of a controller/model or some other file that is a part of Rails' structure - you will have to explicitly create a logger for yourself with:
logger = Logger.new(STDOUT) # Or wherever you want to log to
However if Rails.logger is working, then you likely just need an alias like logger = Rails.logger to allow you to use logger.warn without the Rails namespace prefix. You're probably in a file that doesn't already have a helper that is aliasing that for you.
Some more digging in the API reveals that the logger stems from ActiveSupport::LogSubscriber - several classes like ActionController include child LogSubscribers that inherit from that module and hence have the logger method available to them. You can manually alias it to Rails.logger to have it work for you wherever you are trying to invoke it.
Source: https://api.rubyonrails.org/classes/ActiveSupport/LogSubscriber.html#method-c-logger
In addition to the default Rails logger, I'd like to create my own one. I will be using LogstashLogger, configured with a custom URL.
I created a file lib/custom_logstash_logger.rb with:
class CustomLogstashLogger
include ActiveSupport::Configurable
config_accessor :logstash_url
def self.log(message)
# ?
end
end
And I'm configuring it via config/initializers/custom_logstash_logger.rb:
CustomLogstashLogger.config.logstash_url = ENV.fetch("LOGSTASH_URL", nil)
Now, I could go ahead and define self.log such that I always create a new logger whenever the method is called:
def self.log(message)
logger = LogStashLogger.new(uri: self.config.logstash_url)
logger.info(message)
end
But that is hardly efficient—the logger is instantiated every time the log method is called.
What would be the ideal way to instantiate the LogStashLogger already when the class is configured, and not when the logger is used? I don't want to modify application.rb or set a global variable that holds the logger—it should be a class method.
As mentioned by Aleksei Matuishkin, one can use a lazily instantiated variable:
#logger ||= LogStashLogger.new(uri: self.config.logstash_url)
This allows creating the logger when first calling log.
I have an initializer named _settings.rb that looks like this
class Settings < Settingslogic
source "#{Rails.root}/config/application.yml"
namespace Rails.env
end
My application.yml defines a value for a custom setting I call environhost.
I call it using:
Settings.environhost
This works fine, EXCEPT for when I try to call the value in my /app/config/application.rb
config.action_controller.asset_host = Settings.environhost
For this, I get an uninitialized constant.
Is there anyway I can put a pointer in my application.rb to load _settings.rb before
config.action_controller.asset_host = Settings.environhost
is loaded? What's the best way to do this?
http://guides.rubyonrails.org/initialization.html
Rails own configuration will be always loaded before any custom things, that's for sure. Otherwise can you imagine what a mess :)
The solution is not to try to load before Rails configuration. Instead, hook into initializer to add your own logic to override Rails default.
Railtie is the place you can do that without sweat. Here you can access config method shared in all initializers including Rails.
module MySettings
def self.environhost
"foobar"
end
class MySettingsRailtie < Rails::Railtie
config.action_controller.asset_host = MySettings.environhost
end
end
Side note: In most cases you should be fine to set assets host as mu_is_too_short commented. If you need anything other than that, you can use custom intializer by Railtie.
I have a custom class located in lib and I want to create one instance of it that I can call anywhere in my Rails app--from controllers to rake tasks.
I can seem to do this with simple variables in an initializers file like this:
#foo = "bar"
and I can see #foo anywhere. How do I create an instance of my class so I can call it and its methods from anywhere?
In the lib directory: lib/some_thing.rb
Also see the comments in config/application.rb :
...
# Custom directories with classes and modules you want to be autoloadable.
...
It sounds to me that you're trying to create a singleton instance of your class in lib.
Have a look at Ruby's singleton module in the ruby standard library http://www.ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html
Then you can just always access it by
SomeClass.instance
What is the best/easiest way to configure logging for code kept in the lib directory?
There's two ways to go about it:
Assuming your library is self-contained and has a module, you can add a logger attribute to your module and use that everywhere in your library code.
module MyLibrary
mattr_accessor :logger
end
You then either use an initializer in config/initializers/, or an config.after_initialize block in config/environment.rb to initialize your logger, like so:
require 'mylibrary'
MyLibrary.logger = Rails.logger
This would still allow you to use your self-contained library from scripts outside of Rails. Which is nice, on occasion.
If using your library without Rails really doesn't make sense at all, then you can also just use Rails.logger directly.
In either case, you're dealing with a standard Ruby Logger. Also keep in mind that, in theory, the logger may be nil.
We can use Rails logger directly into the lib, see the following snippet.
require 'logger'
Rails.logger.info "Hay..!!! Im in lib"
Rails.logger.debug "Debugging this object from Lib #{object.inspect}"
Rails.logger.error "This is an error..."