開発するうえで調べたこと、知ったことを備忘録としてメモメモ!

Ruby
Log

log4r

インストール

$ gem install log4r
Successfully installed log4r-1.1.10
1 gem installed
Installing ri documentation for log4r-1.1.10...
Installing RDoc documentation for log4r-1.1.10...

$ gem list

*** LOCAL GEMS ***

actionmailer (2.3.14)
actionpack (2.3.14)
activerecord (2.3.14)
activeresource (2.3.14)
activesupport (2.3.14)
cgi_multipart_eof_fix (2.5.0)
gem_plugin (0.2.3)
log4r (1.1.10)
mongrel (1.1.5 x86-mingw32)
mongrel_cluster (1.0.5)
mongrel_service (0.4.0)
mysql (2.8.1 x86-mingw32)
rack (1.1.3)
rails (2.3.14)
rake (0.8.7)
rubygems-update (1.3.7)

設定

config/enviroment.rb
require 'Log4r'
Rails::Initializer.run do |config|
  # 出力先の設定
  config.log_path = "#{RAILS_ROOT}/log/#{Rails.env}.log"
  # フォーマット設定
  formatter = Log4r::PatternFormatter.new(
    :pattern => "[%d][%C][%l][%X{:pid}]: [%X{:env}] -- %M",
    :date_format => "%Y/%m/%d %H:%M:%S"
  )
  # 標準出力(コマンドプロンプト)
  outputter1 = Log4r::StdoutOutputter.new(STDOUT, :formatter => formatter)
  # ファイル出力
  outputter2 = Log4r::FileOutputter.new(
    'file',
    :filename => config.log_path,
    :trunc => false,
    :formatter => formatter
  )

  logger = Log4r::Logger.new("Practice")
  logger.add(outputter1, outputter2)
  config.logger = logger
  config.logger.level = Log4r::INFO  #(DEBUG|INFO|WARN|ERROR|FATAL)
  Log4r::MDC.put :pid, $$            # %X{:pid}
  Log4r::MDC.put :env, Rails.env     # %X{:env}
end
フォーマット後
logger.debug "#{__FILE__}:#{__LINE__}"
logger.info  "#{__FILE__}:#{__LINE__}"
logger.warn  "#{__FILE__}:#{__LINE__}"
logger.error "#{__FILE__}:#{__LINE__}"
logger.fatal "#{__FILE__}:#{__LINE__}"
で以下を出力
[2012-12-24 19:23:00][Practice][INFO][6264]: [development] -- RAILS_ROOT/app/controllers/lists_controller.rb:13
[2012-12-24 19:23:00][Practice][WARN][6264]: [development] -- RAILS_ROOT/app/controllers/lists_controller.rb:14
[2012-12-24 19:23:00][Practice][ERROR][6264]: [development] -- RAILS_ROOT/app/controllers/lists_controller.rb:15
[2012-12-24 19:23:00][Practice][FATAL][6264]: [development] -- RAILS_ROOT/app/controllers/lists_controller.rb:16

このページへのコメント

qCJKzm A big thank you for your article post.Really looking forward to read more. Much obliged.

0
Posted by awesome things! 2014年01月23日(木) 17:28:11 返信

oOxtWf Thank you ever so for you article.Really looking forward to read more. Great.

0
Posted by check it out 2014年01月22日(水) 21:30:59 返信

I'm gone to say to my little brother, that he should also pay a visit this web site on regular basis to take updated from newest news.|
アグ ブーツ 正規品 http://www.mwhittlephoto.com/

0
Posted by アグ ブーツ 正規品 2014年01月04日(土) 20:34:30 返信

Favorite furnished sort of tools is the Replacement windows system, Linux system, Unix, in addition to Apple Ossements.
モンクレール ダウン レディース http://www.wimoz.com

0
Posted by モンクレール ダウン レディース 2013年12月17日(火) 02:42:07 返信

Yes! Finally someone writes about %keyword1%.|
サマンサベガ http://www.quizverse.com/サマンサタバサプチチョイス-ポーチ-c-2_4.html

0
Posted by サマンサベガ 2013年12月03日(火) 17:59:32 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

メンバーのみ編集できます