rails_log_watcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ rails_log_watcher
2
+ =================
3
+
4
+ == Watch and analyze rails log, mark long time requests and sqls.
5
+
6
+ == Require Log4R and tested in rails 2 only.
7
+
8
+ ## Install
9
+
10
+ $ sudo gem install log4r
11
+ $ sudo gem install rails_log_watcher
12
+
13
+ ## Usage
14
+
15
+ #enviroment.rb
16
+ gem "log4r"
17
+ gem "rails_log_watcher"
18
+
19
+ #config/development.rb or production.rb
20
+ config.logger = RailsLogWatcher.new(config.log_path)
21
+ config.logger.warn_request_secs = 5
22
+ config.logger.warn_secs = 2
23
+ config.log_level = :debug
24
+
25
+ #copy log4r.yml to config/log4r.yml
26
+
27
+ Copyright (c) 2012 MIT-LICENSE
28
+
29
+ Author : Wayne Deng
30
+
31
+ Web : http://blog.waynedeng.com
32
+
33
+ Email : wayne.deng.cn(AT).com
@@ -0,0 +1,56 @@
1
+ require 'logger'
2
+ require 'log4r'
3
+ require 'log4r/yamlconfigurator'
4
+ require 'log4r/outputter/datefileoutputter'
5
+
6
+ class RailsLogWatcher < Logger
7
+ attr_accessor :warn_request_secs, :warn_secs
8
+
9
+ alias ext_old_format_message format_message
10
+
11
+ def format_message(severity, timestamp, progname, msg)
12
+ analyze_log_message timestamp, msg
13
+ ext_old_format_message severity, timestamp, progname, msg
14
+ end
15
+
16
+ protected
17
+
18
+ def watcher_logger
19
+ unless @watcher_log
20
+ cfg = Log4r::YamlConfigurator
21
+ cfg.load_yaml_file(RAILS_ROOT + '/config/log4r.yml')
22
+ @watcher_log = Log4r::Logger['watch_log']
23
+
24
+ self.warn_request_secs ||= 10
25
+ self.warn_secs ||= 5
26
+ end
27
+ @watcher_log
28
+ end
29
+
30
+ def clean_str(str)
31
+ str.gsub(/\n/, '').lstrip
32
+ end
33
+
34
+ def analyze_log_message(timestamp, msg)
35
+ return if msg.blank?
36
+
37
+ if msg =~ /Processing (\w+)\#(\w+)/
38
+ watcher_logger.debug clean_str(msg)
39
+ end
40
+
41
+ if msg =~ /Completed in ([\d|.]+)ms/
42
+ complete_time = $1.to_f
43
+ if complete_time > self.warn_request_secs.to_f * 1000.0
44
+ watcher_logger.warn clean_str(msg)
45
+ else
46
+ watcher_logger.debug clean_str(msg)
47
+ end
48
+ elsif msg =~ /\(([\d|.]+)ms\)/
49
+ complete_time = $1.to_f
50
+ if complete_time > self.warn_secs.to_f * 1000.0
51
+ watcher_logger.warn clean_str(msg)
52
+ end
53
+ end
54
+ end
55
+
56
+ end
data/log4r.yml ADDED
@@ -0,0 +1,18 @@
1
+ log4r_config:
2
+ # define all loggers ...
3
+ loggers:
4
+ - name : watch_log
5
+ level : DEBUG
6
+ trace : 'true'
7
+ outputters :
8
+ - watch_log
9
+
10
+ outputters:
11
+ - type: DateFileOutputter
12
+ name: watch_log
13
+ dirname: "log"
14
+ filename: "watch_log.log"
15
+ formatter:
16
+ date_pattern: '%Y-%m-%d %H:%M:%S'
17
+ pattern : '%d %l: %m '
18
+ type : PatternFormatter
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{rails_log_watcher}
3
+ s.version = "0.0.1"
4
+
5
+ s.authors = ["Wayne Deng"]
6
+ s.date = %q{2012-09-28}
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "Watch and analyze rails log, mark long time requests and sqls."
9
+ s.description = "Watch and analyze rails log, mark long time requests and sqls."
10
+ s.email = %q{wayne.deng.cn@gmail.com}
11
+ s.homepage = %q{http://blog.waynedeng.com}
12
+ s.files = ["README.md", "lib/rails_log_watcher.rb", "log4r.yml", "rails_log_watcher.gemspec"]
13
+ s.require_paths = ["lib"]
14
+ s.requirements << 'log4r'
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "SqlLogic", "--main", "README"]
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_log_watcher
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Wayne Deng
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-28 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Watch and analyze rails log, mark long time requests and sqls.
23
+ email: wayne.deng.cn@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.md
32
+ - lib/rails_log_watcher.rb
33
+ - log4r.yml
34
+ - rails_log_watcher.gemspec
35
+ has_rdoc: true
36
+ homepage: http://blog.waynedeng.com
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --line-numbers
42
+ - --inline-source
43
+ - --title
44
+ - SqlLogic
45
+ - --main
46
+ - README
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements:
68
+ - log4r
69
+ rubyforge_project:
70
+ rubygems_version: 1.5.2
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Watch and analyze rails log, mark long time requests and sqls.
74
+ test_files: []
75
+