jlog-rails 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,19 +3,21 @@ module Jlog
3
3
  class AjaxController < ActionController::Metal
4
4
  include ActionController::Rendering
5
5
 
6
+ Levels = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
7
+ LevelsPattern = Regexp.new("^(#{Levels.join("|")})", Regexp::IGNORECASE)
8
+
6
9
  def append
7
- messages = params[:message]
8
- messages = [messages] unless messages.is_a? Array
10
+ messages = Array(params[:message])
9
11
  messages.each do |message|
10
- level_pattern = /^(DEBUG|INFO|WARN|ERROR|FATAL)/
11
- level = message.match(level_pattern)[1]
12
- message = "#{Time.now} Client " << message
12
+ path = request.original_fullpath
13
+ output = JLog.formatter(path).call(:path => path, :message => message)
13
14
 
14
- if ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'].include? level
15
- Rails.logger.send(level.downcase.to_sym, message)
15
+ if message =~ LevelsPattern
16
+ level = $1
17
+ JLog.logger(path).send(level.downcase.to_sym, output)
16
18
  else
17
- Rails.logger.warn('*** Attempt to log with a nonexistent level ***')
18
- Rails.logger.warn(message)
19
+ JLog.logger.warn('*** Attempt to log with a nonexistent level ***')
20
+ JLog.logger.warn(output)
19
21
  end
20
22
  end
21
23
 
data/config/routes.rb CHANGED
@@ -2,4 +2,4 @@ Jlog::Engine.routes.draw do
2
2
 
3
3
  post 'append' => 'ajax#append'
4
4
 
5
- end
5
+ end
@@ -10,6 +10,17 @@ module Jlog
10
10
  end
11
11
  end
12
12
 
13
+ def create_initializer
14
+ puts "Create Jlog initializer config/initializers/jlog.rb"
15
+ create_file("config/initializers/jlog.rb") do
16
+ %Q{require 'jlog-rails'\n
17
+ JLog.configure("/jlog/append") do |config|
18
+ config.logger = Logger.new(File.join(Rails.root, "log", "jlog_\#\{Rails.env\}.log"))
19
+ end
20
+ }
21
+ end
22
+ end
23
+
13
24
  def include_js_file
14
25
  append_to_file 'app/assets/javascripts/application.js' do
15
26
  %Q{\n//\n//= require jlog}
@@ -0,0 +1,10 @@
1
+ module JLog
2
+ class Configuration
3
+ attr_accessor :logger, :formatter
4
+
5
+ def initialize
6
+ @logger = Rails.logger
7
+ @formatter = Formatter::Default
8
+ end
9
+ end
10
+ end
data/lib/jlog/dsl.rb ADDED
@@ -0,0 +1,27 @@
1
+ module JLog
2
+ module DSL
3
+ def configure(path = nil, &block)
4
+ @configurations ||= {}
5
+ config = Configuration.new
6
+ @configurations[path] = config
7
+ block.call(config) if block
8
+ end
9
+
10
+ def get_config(path)
11
+ return nil unless @configurations or @configurations[path]
12
+ return @configurations[path]
13
+ end
14
+
15
+ def logger(path)
16
+ config = get_config(path)
17
+ return config.logger if config
18
+ Rails.logger
19
+ end
20
+
21
+ def formatter(path)
22
+ config = get_config(path)
23
+ return config.formatter if config
24
+ Formatter::Default
25
+ end
26
+ end
27
+ end
data/lib/jlog/engine.rb CHANGED
@@ -2,4 +2,4 @@ module Jlog
2
2
  class Engine < Rails::Engine
3
3
  isolate_namespace Jlog
4
4
  end
5
- end
5
+ end
@@ -0,0 +1,11 @@
1
+ module JLog
2
+ module Formatter
3
+ Default = proc do |params|
4
+ "#{Time.now} #{params[:path]}: #{params[:message]}"
5
+ end
6
+
7
+ SplunkGenericSingleLine = proc do |params|
8
+ ([Time.now.strftime("%Y-%m-%dT%H:%M:%S.%3N")] + params.map { |k,v| "#{k}=#{ERB::Util.url_encode(v)}" }).join " "
9
+ end
10
+ end
11
+ end
data/lib/jlog/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jlog
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/jlog-rails.rb CHANGED
@@ -1 +1,11 @@
1
- require "jlog/engine"
1
+ require "erb"
2
+
3
+ require "jlog/formatters"
4
+ require "jlog/configuration"
5
+ require "jlog/dsl"
6
+ require "jlog/engine"
7
+ require "jlog/version"
8
+
9
+ module JLog
10
+ extend DSL
11
+ end
@@ -32,7 +32,7 @@
32
32
 
33
33
  function JLog(name) {
34
34
  var _currentLevel = JLog.ALL,
35
- _appenders = [new JLog.ConsoleAppender],
35
+ _appenders = [new JLog.ConsoleAppender()],
36
36
  _name = null,
37
37
  _enabled = true;
38
38
 
@@ -40,6 +40,10 @@ function JLog(name) {
40
40
  _name = name || null;
41
41
  };
42
42
 
43
+ if (name) {
44
+ this.setName(name);
45
+ }
46
+
43
47
  this.addAppender = function(appender) {
44
48
  if (appender) {
45
49
  _appenders.push(appender);
@@ -80,10 +84,9 @@ function JLog(name) {
80
84
  case 'all': _currentLevel = JLog.ALL; break;
81
85
  case 'debug': _currentLevel = JLog.DEBUG; break;
82
86
  case 'info': _currentLevel = JLog.INFO; break;
87
+ case 'warn': _currentLevel = JLog.WARN; break;
83
88
  case 'error': _currentLevel = JLog.ERROR; break;
84
89
  case 'fatal': _currentLevel = JLog.FATAL; break;
85
- case 'warn': _currentLevel = JLog.WARN; break;
86
- case 'none': // fall through to default
87
90
  default: _currentLevel = JLog.NONE;
88
91
  }
89
92
  } else {
@@ -111,11 +114,7 @@ function JLog(name) {
111
114
  this.getLevel = function() {
112
115
  return _currentLevel;
113
116
  };
114
-
115
- if (name) {
116
- this.setName(name);
117
- }
118
- };
117
+ }
119
118
 
120
119
  JLog.ALL = 0;
121
120
  JLog.DEBUG = 1;
@@ -180,18 +179,19 @@ JLog.prototype._log = function() {
180
179
  JLog.ConsoleAppender = function() {
181
180
  this.name = 'ConsoleAppender';
182
181
 
183
- if(window.console && window.console.log)
184
- this.log = function(msg) { window.console.log(msg); }
185
- else this.log = function(msg) {};
182
+ if (window.console && window.console.log)
183
+ this.log = function(msg) { window.console.log(msg); };
184
+ else
185
+ this.log = function(msg) {};
186
186
  };
187
187
 
188
188
  JLog.AjaxAppender = function(url) {
189
189
  // Do we make more then 1 call at a time?
190
- var waitForResponse = true;
190
+ var waitForResponse = true;
191
191
  // Current buffer of messages
192
- var queuedLoggingEvents = [];
192
+ var queuedLoggingEvents = [];
193
193
  // Messages which should be sent
194
- var queuedRequests = [];
194
+ var queuedRequests = [];
195
195
  // Maximum count of messages sent at a one time
196
196
  var batchSize = 10;
197
197
  // Are we currently sending something
@@ -206,23 +206,25 @@ JLog.AjaxAppender = function(url) {
206
206
  function sendRequest(postData, callback) {
207
207
  $.post(url, postData, "json")
208
208
  .complete(function() {
209
- if(waitForResponse) sending = false;
209
+ if (waitForResponse) sending = false;
210
210
  if (callback) callback(true);
211
211
  });
212
212
  }
213
213
 
214
214
  function sendAllRemaining() {
215
- if(queuedLoggingEvents.length == 0) return;
215
+ if (queuedLoggingEvents.length === 0) return;
216
216
  var eventCopy = queuedLoggingEvents;
217
217
  queuedLoggingEvents = [];
218
218
  queuedRequests.push(eventCopy);
219
219
  sendAll();
220
220
  }
221
221
 
222
- function preparePostData(data) { return { message:data }; }
222
+ function preparePostData(data) {
223
+ return { message:data };
224
+ }
223
225
 
224
226
  function sendAll() {
225
- if(waitForResponse && sending) return;
227
+ if (waitForResponse && sending) return;
226
228
  sending = true;
227
229
  var currentRequestBatch;
228
230
  if (waitForResponse) {
@@ -251,7 +253,7 @@ JLog.AjaxAppender = function(url) {
251
253
  log: function(msg) {
252
254
  queuedLoggingEvents.push(msg);
253
255
  if (queuedLoggingEvents.length >= batchSize) sendAllRemaining();
254
- else if(queuedLoggingEvents.length == 1) scheduleSending();
256
+ else if (queuedLoggingEvents.length == 1) scheduleSending();
255
257
  }
256
- }
258
+ };
257
259
  };
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jlog-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,7 +43,10 @@ files:
43
43
  - jlog-rails.gemspec
44
44
  - lib/generators/jlog/install_generator.rb
45
45
  - lib/jlog-rails.rb
46
+ - lib/jlog/configuration.rb
47
+ - lib/jlog/dsl.rb
46
48
  - lib/jlog/engine.rb
49
+ - lib/jlog/formatters.rb
47
50
  - lib/jlog/version.rb
48
51
  - script/rails
49
52
  - vendor/assets/javascripts/jlog.js
@@ -67,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
70
  version: '0'
68
71
  requirements: []
69
72
  rubyforge_project: jlog-rails
70
- rubygems_version: 1.8.23
73
+ rubygems_version: 1.8.24
71
74
  signing_key:
72
75
  specification_version: 3
73
76
  summary: Jlog for Rails