1. Rails Environments and Configuration - 1.6 Logging

1.6 Logging

Most programming contexts in Rails (models, controllers, view templates) have a logger attribute, which holds a reference to a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class.
For other cases, the Rails.logger method references a logger that you can use anywhere.

The standard logger’s severities are :

  • debug: Use the debug level to capture data and application state useful for debugging problems later on. This level is not usually captured in production logs.
  • info: Use info level to capture informational messages. I like to use this log level for time-stamping non-ohrdinary events tat are still within the bounds of good application behavior.
  • warn: Use the warn level to capture things that are out of the ordinary and might be worth investigating. Sometimes I’ll throw in a logged warning when guard clauses in my code keep a client from doing something they weren’t supposed to do.
  • error: Use the error log level to capture information about error conditions that don’t require a server restart.
  • fatal: The worst-case imaginable has happened—your application is now dead and manual intervention is
    necessary to restart it.

1.6.1 Rails Log Files

The log folder of your Rails application holds three log files corresponding to each of the standardenvironments. Log files can grow very large over time. A rake task is provided for easily clearing the log files:

$ rake log:clear # Truncates all *.log files in log/ to zero bytes

The contents of log/development.log are very useful while you’re working. Many Rails coders leave a
terminal window open with a continuous tail of the development log open while they’re coding:

$ tail -f log/development.log

  Article Load (0.2ms)  SELECT "articles".* FROM "articles" WHERE
    "articles"."id" = $1 LIMIT 1  [["id", "1"]]

This is a list of all the data items contained in that chunk of log output:

  • The controller and action that were invoked
  • The remote IP address of the computer making the request
  • A timestamp indicating when the request happened
  • The session ID associated with the request
  • The hash of parameters associated with the request
  • Database request information including the time and the SQL statement executed
  • Query cache hit info including time and the SQL statement triggering results from the cache instead of
    a roundtrip to the database
  • Rendering information for each template involved in rendering the view output and time consumed by
    each
  • Total time used in completing the request with corresponding request-per-second figures
  • Analysis of the time spent in database operations versus rendering
  • The HTTP status code and URL of the response sent back to the client

1.6.2 Tagged Logging

Log files can contain an extensive amounts of information, making tracking down issues or particular requestsdifficult. To alleviate this issue, Rails 3.2 introduced the ability to tag your log messages.

The easiest way to include extra “tagged” information in your logs, is to include methods which respond to the request object to the config.log_tags configuration setting.
For example, setting config.log_tags to include :subdomain:

config.log_tags = [:subdomain]
# result in subdomain of a request being prefixed with square brackets to each log message
> [admin] Started GET "/articles" for 127.0.0.1 at 2013-02-01 11:49:09 -0500

1.6.3 Log File Analysis

  • Performance: One of the more obvious analyses would be a study of the performance of your application. The faster your requests execute, the more requests you can serve with a given Rails process. That’s why performance figures are often expressed in terms of requests per second. Find the queries and rendering sections that are taking a long time and figure out why.
    It’s important to realize that the times reported by the logger are not super-accurate. In fact, they’re wrong more often than not, if simply for the reason that it’s very difficult to measure the timing of something from within itself. Add up the percentage of rendering and database times for any given request and it will not always be close to 100%.
    However, despite not being accurate in a purely objective sense, the reported times are perfect for making subjective comparisons within the same application. They give you a way of gauging whether an action is taking longer than it used to, or whether it is relatively faster or slower than another action, and so on.

  • SQL queries: Active Record not behaving as expected? The fact that SQL generated by Active Record is logged can often help you debug problems caused by complicated queries.

  • Identification of N+1 select problems: Whenever you are displaying a record along with an associated collection of records, there’s a chance that you will have a so-called N+1 select problem. You’ll recognize the problem by a series of many SELECT statements, with the only difference being the value of the primary key.

1.6.3.1 Rails::Subscriber.colorize_logging

Tells Rails whether to use ANSI codes to colorize the logging statements. The colors make it much easier to read the logs (except on Windows) and may complicate matters if you use software like syslog. Defaults to true. Change to false if you view your logs with software that doesn’t understand the ANSI color codes.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末虹茶,一起剝皮案震驚了整個(gè)濱河市夷恍,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌彩掐,老刑警劉巖医咨,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件枫匾,死亡現(xiàn)場離奇詭異,居然都是意外死亡拟淮,警方通過查閱死者的電腦和手機(jī)干茉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來很泊,“玉大人角虫,你說我怎么就攤上這事∥欤” “怎么了戳鹅?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長昏兆。 經(jīng)常有香客問我枫虏,道長,這世上最難降的妖魔是什么爬虱? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任隶债,我火速辦了婚禮,結(jié)果婚禮上跑筝,老公的妹妹穿的比我還像新娘死讹。我一直安慰自己,他們只是感情好曲梗,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布赞警。 她就那樣靜靜地躺著妓忍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪愧旦。 梳的紋絲不亂的頭發(fā)上世剖,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天,我揣著相機(jī)與錄音忘瓦,去河邊找鬼搁廓。 笑死,一個(gè)胖子當(dāng)著我的面吹牛耕皮,可吹牛的內(nèi)容都是我干的境蜕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼凌停,長吁一口氣:“原來是場噩夢啊……” “哼粱年!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起罚拟,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤台诗,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后赐俗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體拉队,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年阻逮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了粱快。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡叔扼,死狀恐怖事哭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瓜富,我是刑警寧澤鳍咱,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站与柑,受9級特大地震影響谤辜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜价捧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一每辟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧干旧,春花似錦、人聲如沸妹蔽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至编整,卻和暖如春舔稀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背掌测。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工内贮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人汞斧。 一個(gè)月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓夜郁,卻偏偏與公主長得像,于是被迫代替她去往敵國和親粘勒。 傳聞我的和親對象是個(gè)殘疾皇子竞端,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容