Skip to content

Commit

Permalink
make new rails apps log to STDOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
hone committed Mar 15, 2013
1 parent b601399 commit b7d9d6e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
8 changes: 8 additions & 0 deletions activesupport/lib/active_support/tagged_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def tags_text
end
end

def self.create(f, formatter, level)
logger = ActiveSupport::Logger.new f
logger.formatter = formatter
logger = new(logger)
logger.level = ActiveSupport::Logger.const_get(level.to_s.upcase)
logger
end

def self.new(logger)
# Ensure we set a default formatter so we aren't extending nil!
logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
Expand Down
11 changes: 11 additions & 0 deletions activesupport/test/tagged_logging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def flush(*)
assert logger.formatter.respond_to?(:tagged)
end

test 'creates a tagged logger with the appropriate level and formatter' do
stringio = StringIO.new
logger = ActiveSupport::TaggedLogging.create(stringio, ActiveSupport::Logger::SimpleFormatter.new, :debug)
logger.debug("foo")

assert_not_nil logger.formatter
assert logger.formatter.respond_to?(:tagged)
assert_equal 0, logger.level
assert stringio.string.include?("foo")
end

test "tagged once" do
@logger.tagged("BCX") { @logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
Expand Down
4 changes: 4 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##

* New rails apps log to STDOUT by default

*Terence Lee*

* Add support for generate scaffold password:digest

* adds password_digest attribute to the migration
Expand Down
6 changes: 1 addition & 5 deletions railties/lib/rails/application/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ module Bootstrap
f.binmode
f.sync = config.autoflush_log # if true make sure every write flushes

logger = ActiveSupport::Logger.new f
logger.formatter = config.log_formatter
logger = ActiveSupport::TaggedLogging.new(logger)
logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase)
logger
logger = ActiveSupport::TaggedLogging.create(f, config.log_formatter, config.log_level)
rescue StandardError
logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDERR))
logger.level = ActiveSupport::Logger::WARN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ class Application < Rails::Application
# Disable the asset pipeline.
config.assets.enabled = false
<% end -%>

config.logger = ActiveSupport::TaggedLogging.create(STDOUT, config.log_formatter, config.log_level)
end
end
21 changes: 21 additions & 0 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,5 +670,26 @@ def index
end
end
end

test "set logger to STDOUT by default" do
stdout = capture(:stdout) do
app = build_app

controller :omg, <<-RUBY
class OmgController < ApplicationController
def index
Rails.logger.info "HI MOM"
render text: "omg"
end
end
RUBY

require "#{app_path}/config/environment"

get "/omg/index"
end

assert stdout.include?("HI MOM"), "STDOUT does not include 'HI MOM', #{stdout}"
end
end
end

0 comments on commit b7d9d6e

Please sign in to comment.