aioli_log_formatter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thomas Baustert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Aioli Logging Formatter
2
+
3
+ Aioli logger (All In One LIne Logger) is a logging formatter for Rails 3.
4
+
5
+ Aioli make our meals delicious, right?
6
+
7
+ By default it logs lines of format:
8
+
9
+ <date> <host> rails[<processId>] <sessionId> u<userId>>: <message>
10
+
11
+ Examples:
12
+
13
+ Nov 23 14:27:56 host rails[19572] f79a2d8633 u18: Completed in 1218ms (View: 772, DB: 209) | 200 OK [http://localhost/claims]
14
+ Nov 23 14:27:56 host rails[19572] 0 u0: Completed in 1218ms (View: 772, DB: 209) | 200 OK [http://localhost/claims]
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'aioli_log_formatter'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install aioli_log_formatter
29
+
30
+ ## Usage
31
+
32
+ Enable the formatter:
33
+
34
+ # config/application.rb
35
+ ...
36
+ class Application < Rails::Application
37
+ ...
38
+ config.aioli_log_formatter.enabled = true
39
+
40
+
41
+ Enable before filters to retrieve session ID and user ID from default sources.
42
+
43
+ class ApplicationController < ActionController::Base
44
+
45
+ aioli_log_formatter
46
+ ...
47
+ end
48
+
49
+ This is the same as:
50
+
51
+ class ApplicationController < ActionController::Base
52
+
53
+ aioli_log_formatter user_id: lambda { |controller| controller.session[:user_id] },
54
+ session_id: lambda { |controller| controller.request.session_options[:id] }
55
+ ...
56
+ end
57
+
58
+ To get the values from different sources simply change the procs, e.g.:
59
+
60
+ class ApplicationController < ActionController::Base
61
+
62
+ aioli_log_formatter user_id: lambda { |controller| controller.session[:account_id] }
63
+ ...
64
+ end
65
+
66
+ When using [Devise](https://github.com/plataformatec/devise) gem you can get the user ID as follows:
67
+
68
+ class ApplicationController < ActionController::Base
69
+
70
+ aioli_log_formatter user_id: lambda { |controller|
71
+ user = controller.env['warden'].user
72
+ user ? user.id : nil
73
+ }
74
+ ...
75
+ end
76
+
77
+ ## Define your own Formatter
78
+
79
+ Define your formatter by inheriting from `AioliLogFormatter::Formatter` for example:
80
+
81
+ class MyAwesomeFormatter < AioliLogFormatter::Formatter
82
+
83
+ # I like different formatting
84
+ def call(severity, timestamp, progname, message)
85
+ "#{timestamp} #{hostname} pid=#{process_id} sid=#{shorten_session_id} uid=#{user_id} msg=#{sanitize(message)}\n"
86
+ end
87
+
88
+ end
89
+
90
+ Have a look into source code of `AioliLogFormatter::Formatter` for more methods to overwrite.
91
+
92
+ Enable the formatter:
93
+
94
+ # config/application.rb
95
+ require_relative "../path/to/my_awesome_formatter"
96
+
97
+ ...
98
+ class Application < Rails::Application
99
+ ...
100
+ config.aioli_log_formatter.enabled = true
101
+ config.aioli_log_formatter.formatter = MyAwesomeFormatter.new
102
+
103
+
104
+ See new formatting:
105
+
106
+ 2013-11-12 16:16:50 +0100 host pid=69402 sid=e36db0f022 uid=u42 msg=Started GET "/...
107
+
108
+ ## Tests
109
+
110
+ Examples are located under `spec` and `test/dummy/spec` and are executed via:
111
+
112
+ $ cd path/to/aioli_log_formatter
113
+ $ bundle exec rake spec
114
+
115
+
116
+ ## Credits
117
+
118
+ Inspired by [logjam_logger](https://github.com/alpinegizmo/logjam_logger)
119
+
120
+ ## License
121
+
122
+ Copyright © 2013 [Thomas Baustert](http://thomasbaustert.de), released under the MIT license
@@ -0,0 +1,22 @@
1
+ #
2
+ # ActiveSupport::BufferedLogger is the central logger class for all Rails loggers
3
+ # (e.g. ActiveRecord::Base.logger, ActionMailer::Base.logger, ActionController::Base.logger).
4
+ # Unfortunately it does not support setting a formatter so we have to monkey patch it.
5
+ #
6
+ module ActiveSupport
7
+ class BufferedLogger
8
+
9
+ private
10
+
11
+ def open_logfile(log)
12
+ logger = Logger.new log
13
+
14
+ if AioliLogFormatter::Engine.config.aioli_log_formatter.enabled
15
+ logger.formatter = AioliLogFormatter::Engine.config.aioli_log_formatter.formatter
16
+ end
17
+
18
+ logger
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ module AioliLogFormatter
2
+ module Controller
3
+
4
+ module Extension
5
+ def self.included(base)
6
+ base.class_eval do
7
+ include InstanceMethods
8
+ extend ClassMethods
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ end
14
+
15
+ module ClassMethods
16
+ SESSION_ID_PROC = lambda { |controller| controller.request.session_options[:id] }
17
+ USER_ID_PROC = lambda { |controller| controller.session[:user_id] }
18
+
19
+ def aioli_log_formatter(options = {})
20
+ if session_id = options.fetch(:session_id, SESSION_ID_PROC)
21
+ before_filter { |controller| ($session_id ||= {})[Thread.current] = session_id.call(controller) || 0 }
22
+ end
23
+
24
+ if user_id = options.fetch(:user_id, USER_ID_PROC)
25
+ before_filter { |controller| ($user_id ||= {})[Thread.current] = user_id.call(controller) || 0 }
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module AioliLogFormatter
2
+ class Engine < ::Rails::Engine
3
+ config.aioli_log_formatter = ActiveSupport::OrderedOptions.new
4
+ config.aioli_log_formatter.enabled = false
5
+ config.aioli_log_formatter.formatter = Formatter.new
6
+
7
+ initializer :aioli_log_formatter do |app|
8
+ AioliLogFormatter.setup(app) if app.config.aioli_log_formatter.enabled
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,50 @@
1
+ require 'socket'
2
+
3
+ module AioliLogFormatter
4
+
5
+ class Formatter
6
+
7
+ # Rails 2
8
+ def format(message)
9
+ "#{timestamp} #{hostname} rails[#{process_id}] #{shorten_session_id} #{user_id}: #{sanitize(message)}\n"
10
+ end
11
+
12
+ # Rails 3
13
+ def call(severity, timestamp, progname, message)
14
+ format(message)
15
+ end
16
+
17
+ private
18
+
19
+ def timestamp
20
+ Time.now.strftime("%b %d %H:%M:%S")
21
+ end
22
+
23
+ def hostname
24
+ @parsed_hostname ||= Socket.gethostname.split('.').first
25
+ end
26
+
27
+ def process_id
28
+ $$
29
+ end
30
+
31
+ def shorten_session_id
32
+ session_id[0,10]
33
+ end
34
+
35
+ # before_filter { |controller| ($session_id ||= {})[Thread.current] = controller.session_id || 0 }
36
+ def session_id
37
+ defined?($session_id) ? "#{$session_id[Thread.current] || 0}" : '0'
38
+ end
39
+
40
+ # before_filter { |controller| ($user_id ||= {})[Thread.current] = controller.session[:user_id] || 0 }
41
+ def user_id
42
+ defined?($user_id) ? "u#{$user_id[Thread.current] || 0}" : 'u0'
43
+ end
44
+
45
+ def sanitize(message)
46
+ message.to_s.gsub(/\n/, ' ').lstrip
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module AioliLogFormatter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'aioli_log_formatter/version'
2
+ require 'aioli_log_formatter/formatter'
3
+ require 'aioli_log_formatter/engine'
4
+ require 'aioli_log_formatter/active_support/buffered_logger'
5
+ require 'aioli_log_formatter/controller/extension'
6
+
7
+ module AioliLogFormatter
8
+
9
+ def self.setup(app)
10
+ ActionController::Base.send(:include, AioliLogFormatter::Controller::Extension)
11
+ end
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aioli_log_formatter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Baustert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ type: :development
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ none: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ none: false
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ none: false
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: timecop
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ none: false
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ type: :development
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ none: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ none: false
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ type: :development
81
+ requirement: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ none: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ none: false
93
+ prerelease: false
94
+ description: Logging formatter for Rails
95
+ email:
96
+ - business@thomasbaustert.de
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - lib/aioli_log_formatter/active_support/buffered_logger.rb
102
+ - lib/aioli_log_formatter/controller/extension.rb
103
+ - lib/aioli_log_formatter/engine.rb
104
+ - lib/aioli_log_formatter/formatter.rb
105
+ - lib/aioli_log_formatter/version.rb
106
+ - lib/aioli_log_formatter.rb
107
+ - MIT-LICENSE
108
+ - README.md
109
+ homepage: https://github.com/thomasbaustert/aioli_log_formatter
110
+ licenses: []
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ hash: 607169139388963087
122
+ version: '0'
123
+ none: false
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ segments:
129
+ - 0
130
+ hash: 607169139388963087
131
+ version: '0'
132
+ none: false
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.25
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Aioli logger (All In One LIne Logger) is a logging formatter for Rails
139
+ test_files: []