scribble

吕小荣

Blog Friends RSS About

出了问题不靠猜

Published on 2013-10-28

Vincent 听说我不会看日志,大惊失色的问「你以前如何debug?」

我只会对着一堆日志发呆,因为除了日志中的message,其他的都看不懂。只能逐个排除,直到程序能正常运行。

于是 Vincent 给了我讲了两个看日志的方式,醍醐灌顶的感觉。

自下往上

程序的执行无非就是堆栈,自下往上逐个依赖。找问题时也应该从最下面的入手,逐渐往上排插。

概率

哪些方面最容易引起bug呢?

  1. 自己的代码有问题(99.99%的概率)

  2. Gem 包的兼容性问题(有可能)

  3. Rails 框架中的问题(概率很低)

  4. Ruby 的问题(概率奇低无比)

所以应该先看自己代码 => 再看gem => 再看Rails源码。

范例一枚

感谢 Vincent 教我,代码出了问题再也不用靠猜了,直接命中。

以下这一坨日志是某个测试未通时提示的,问题可能就出在这一行 test/models/order_test.rb:17


ERROR (0:00:01.695) test_0003_build order_no

验证失败: Device 已经被使用 

/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/validations.rb:56:in `save!'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/transactions.rb:264:in `block in save!'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/transactions.rb:208:in `transaction'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.14/lib/active_record/transactions.rb:264:in `save!'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluation.rb:15:in `[]'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluation.rb:15:in `create'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:9:in `tap'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:9:in `result'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory.rb:42:in `run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/notifications.rb:125:in `instrument'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:22:in `run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'test/factories/users.rb:3:in `block (3 levels) in <top (required)>'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute/dynamic.rb:14:in `instance_exec'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute/dynamic.rb:14:in `block in to_proc'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb:71:in `instance_exec'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb:71:in `block in define_attribute'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:56:in `get'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluation.rb:12:in `object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:9:in `result'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory.rb:42:in `run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/notifications.rb:125:in `instrument'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:22:in `run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy/build.rb:5:in `association'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb:31:in `association'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute/association.rb:19:in `block in to_proc'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb:71:in `instance_exec'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb:71:in `block in define_attribute'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:56:in `get'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/evaluation.rb:12:in `object'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy/build.rb:9:in `result'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory.rb:42:in `run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/notifications.rb:125:in `instrument'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:22:in `run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'test/models/order_test.rb:17:in `block in '
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/testing/setup_and_teardown.rb:36:in `block in run'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/callbacks.rb:414:in `_run__1982525448475124232__setup__3280942427524290793__callbacks'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/xiaoronglv/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.14/lib/active_support/testing/setup_and_teardown.rb:35:in `run' 

</code></pre>

吕小荣 at 00:00
Edit this page