Skip to content

Commit

Permalink
added log_level/log_file to sunspot_rails
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Krause committed Sep 16, 2009
1 parent 52981cd commit a5f6dbf
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 33 deletions.
3 changes: 3 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
== 0.11
* Added Sunspot::Rails::Server class to start/stop/run/bootstrap the solr server
* Added log_level key to sunspot.yml, see java docs for valid values (http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/Level.html)
* Added log_file key to sunspot.yml. This defaults to RAILS_ROOT/log/solr_<environment>.log.

== 0.10.6
* Added script/generate sunspot support to generate the required sunspot.yml
file [Brandon Keepers]

== 0.10.5
* Added a auto_commit_after_request option to sunspot.yml. Sunspot will not
automatically commit any changes in solr if you set this value to false.
Expand Down
29 changes: 29 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,35 @@ remove those documents from the index, use +clean_index_orphans+. Note that
neither of these operations should be needed if Sunspot and Sunspot::Rails are
used as intended.


== Testing Solr integration using RSpec

To disable the sunspot-solr integration for your active record models, add the
following line to your spec_helper.rb

require 'sunspot/spec/extension'

This will disable all automatic after_save/after_destroy solr-requests generated
via the #searchable method. This will not disable/mock explicit calls in your code.

If you want to test the sunspot-solr integration with active record, you can
reenable the after_save/after_destroy hooks by adding 'integrate_sunspot' in your
examples.

describe Searches do
integrate_sunspot

before(:each) do
@movie = Factory.create :movie
end

it "should find a movie" do
Movie.search { keywords @movie.title }.first.should == @movie
end
end



== Further Reading

Reading the {Sunspot documentation}[http://outoftime.github.com/sunspot/docs] is
Expand Down
17 changes: 8 additions & 9 deletions generators/sunspot/templates/sunspot.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
common: &common
production:
solr:
hostname: localhost
port: 8983

production:
<<: *common
solr:
path: /solr/myindex
log_level: WARNING

development:
<<: *common
solr:
hostname: localhost
port: 8982
log_level: INFO

test:
<<: *common
solr:
port: 8981
hostname: localhost
port: 8981
log_level: WARNING

23 changes: 23 additions & 0 deletions lib/sunspot/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ def log_level
@log_level ||= (user_configuration_from_key('solr', 'log_level') || 'INFO')
end

#
# The log directory for solr logfiles
#
# ==== Returns
#
# String:: log_dir
#
def log_file
@log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
end

#
# The solr home directory. Sunspot::Rails expects this directory
# to contain a config, data and pids directory. See
Expand Down Expand Up @@ -104,6 +115,18 @@ def auto_commit_after_request?

private

#
# Logging in rails_root/log as solr_<environment>.log as a
# default.
#
# ===== Returns
#
# String:: default_log_file_location
#
def default_log_file_location
File.join(::Rails.root, 'log', "solr_" + ::Rails.env + ".log")
end

#
# return a specific key from the user configuration in config/sunspot.yml
#
Expand Down
29 changes: 14 additions & 15 deletions lib/sunspot/rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Rails #:nodoc:
class Server

class << self
delegate :port, :solr_home, :to => :configuration
delegate :log_file, :log_level, :port, :solr_home, :to => :configuration

# Name of the sunspot executable (shell script)
SUNSPOT_EXECUTABLE = (RUBY_PLATFORM =~ /w(in)?32$/ ? 'sunspot-solr.bat' : 'sunspot-solr')
Expand Down Expand Up @@ -116,7 +116,7 @@ def bootstrap_neccessary?
# Array:: sunspot_start_command
#
def start_command
[ SUNSPOT_EXECUTABLE, 'start', '--', '-p', port.to_s, '-d', data_path, '-s', solr_home ]
[ SUNSPOT_EXECUTABLE, 'start', '--', '-p', port.to_s, '-d', data_path, '-s', solr_home, '-l', log_level, '-lf', log_file ]
end

#
Expand All @@ -141,6 +141,18 @@ def run_command
[ SUNSPOT_EXECUTABLE, 'run' ]
end

#
# access to the Sunspot::Rails::Configuration, defined in
# sunspot.yml. Use Sunspot::Rails.configuration if you want
# to access the configuration directly.
#
# ==== returns
#
# Sunspot::Rails::Configuration:: configuration
#
def configuration
Sunspot::Rails.configuration
end

private

Expand Down Expand Up @@ -186,19 +198,6 @@ def create_solr_configuration_files
FileUtils.cp( config_file, config_path )
end
end

#
# access to the Sunspot::Rails::Configuration, defined in
# sunspot.yml. Use Sunspot::Rails.configuration if you want
# to access the configuration directly.
#
# ==== returns
#
# Sunspot::Rails::Configuration:: configuration
#
def configuration
Sunspot::Rails.configuration
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
@config.port.should == 8983
end

it "should handle the 'log_level' propery when not set" do
it "should handle the 'log_level' property when not set" do
@config.log_level.should == 'INFO'
end

it "should handle the 'solr_home' propery when not set" do
it "should handle the 'log_file' property" do
@config.log_file.should =~ /log\/solr_test.log/
end

it "should handle the 'solr_home' property when not set" do
Rails.should_receive(:root).at_least(1).and_return('/some/path')
@config.solr_home.should == '/some/path/solr'
end
Expand Down
26 changes: 20 additions & 6 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,37 @@
Sunspot::Rails::Server.should_receive(:configuration).and_return(@sunspot_configuration)
end

it "should delegate the port command to the configuration" do
it "should delegate the port method to the configuration" do
@sunspot_configuration.should_respond_to_and_receive(:port).and_return(1234)
Sunspot::Rails::Server.port.should == 1234
end

it "should delegate the solr_home command to the configuration" do
it "should delegate the solr_home method to the configuration" do
@sunspot_configuration.should_respond_to_and_receive(:solr_home).and_return('/some/path')
Sunspot::Rails::Server.solr_home.should == '/some/path'
end

it "should delegate the log_level method to the configuration" do
@sunspot_configuration.should_respond_to_and_receive(:log_level).and_return('LOG_LEVEL')
Sunspot::Rails::Server.log_level.should == 'LOG_LEVEL'
end

it "should delegate the log_dir method to the configuration" do
@sunspot_configuration.should_respond_to_and_receive(:log_file).and_return('log_file')
Sunspot::Rails::Server.log_file.should =~ /log_file/
end

end

describe "protected methods" do
it "should generate the start command" do
Sunspot::Rails::Server.should_receive(:port).and_return('1')
Sunspot::Rails::Server.should_receive(:solr_home).and_return('home')
Sunspot::Rails::Server.should_receive(:data_path).and_return('data')
Sunspot::Rails::Server.send(:start_command).should == [ 'sunspot-solr', 'start', '--', '-p', '1', '-d', 'data', '-s', 'home' ]
Sunspot::Rails::Server.should_respond_to_and_receive(:port).and_return('1')
Sunspot::Rails::Server.should_respond_to_and_receive(:solr_home).and_return('home')
Sunspot::Rails::Server.should_respond_to_and_receive(:data_path).and_return('data')
Sunspot::Rails::Server.should_respond_to_and_receive(:log_level).and_return('LOG')
Sunspot::Rails::Server.should_respond_to_and_receive(:log_file).and_return('log_file')
Sunspot::Rails::Server.send(:start_command).should == \
[ 'sunspot-solr', 'start', '--', '-p', '1', '-d', 'data', '-s', 'home', '-l', 'LOG', '-lf', 'log_file' ]
end

it "should generate the stop command" do
Expand Down
2 changes: 1 addition & 1 deletion sunspot_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|
"README.rdoc"
]
s.files = [
"History.txt",
"History.txt",
"LICENSE",
"MIT-LICENSE",
"MIT-LICENSE",
Expand Down

0 comments on commit a5f6dbf

Please sign in to comment.