smart_logger 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.
@@ -0,0 +1,13 @@
1
+ module ActiveSupport
2
+ class BufferedLogger
3
+ def add(severity, message = nil, progname = nil, &block)
4
+ return if @level > severity
5
+ message ||= (block && block.call)
6
+ severity = ActiveSupport::BufferedLogger::Severity.constants[severity].to_s
7
+ message = SmartLogger::format_message(severity, message, progname)
8
+ buffer << message
9
+ auto_flush
10
+ message
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ class Logger
2
+ class SimpleFormatter
3
+ def call(severity, time, progname, msg)
4
+ SmartLogger::format_message(severity, msg2str(msg), progname)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # Mark beginning of all rack tasks
2
+ module SmartLogger
3
+ class RackLoggerMarker
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ SmartLogger.start "Rack"
10
+ @app.call(env)
11
+ SmartLogger.end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,23 @@
1
+ # Included in Resque jobs to setup log.
2
+ #
3
+ # Usage:
4
+ # class Something
5
+ # extend SmartLogger::Job
6
+ # end
7
+ module SmartLogger
8
+ module Job
9
+ def around_perform_log_job(*args)
10
+ SmartLogger.start "Job:#{ self.to_s }"
11
+ begin
12
+ yield
13
+ rescue
14
+ raise
15
+ ensure
16
+ SmartLogger.end
17
+ ::Rails.logger.flush
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+
@@ -0,0 +1,95 @@
1
+ require 'securerandom'
2
+
3
+ require 'smart_logger/active_support'
4
+ require 'smart_logger/logger'
5
+ require 'smart_logger/rack'
6
+ require 'smart_logger/resque'
7
+
8
+ module SmartLogger
9
+
10
+ # Mark the start of a new action, creating a new water-mark
11
+ #
12
+ # Example:
13
+ # SmartLogger.start('Worker 3')
14
+ #
15
+ # @action [String] A textual tag to append to log
16
+ #
17
+ def self.start(action)
18
+ create_watermark
19
+ save_param(:action, action)
20
+ end
21
+
22
+ # Mark the end of an action
23
+ #
24
+ def self.end
25
+ create_watermark
26
+ reset_params
27
+ end
28
+
29
+ # Adds extra tags to the log file
30
+ #
31
+ # Example:
32
+ # SmartLogger.add(:remote_ip, '#{IP}')
33
+ #
34
+ # @name [String] Name of parameter
35
+ # @value [String] Value of parameter
36
+ #
37
+ def self.add(name, value)
38
+ create_watermark if self.water_mark.nil?
39
+ save_param(name, value)
40
+ end
41
+
42
+ # Returns a list of parameters for this action
43
+ #
44
+ # @return A string containing name:value pairs
45
+ #
46
+ def self.params
47
+ Thread.current[:logger_params].inject('') do |str, val|
48
+ str << " #{val[0].to_s}:#{val[1]}"
49
+ end rescue ''
50
+ end
51
+
52
+ # Returns the current water mark
53
+ #
54
+ # @return String representing water mark
55
+ #
56
+ def self.water_mark
57
+ Thread.current[:logger_mark]
58
+ end
59
+
60
+ # Initialize smart logger
61
+ #
62
+ # Example:
63
+ # SmartLogger.initialize('InitProcess')
64
+ #
65
+ # @name [String] Default action name before first "start" call
66
+ #
67
+ def self.initialize(name = 'InitProcess')
68
+ Rails.configuration.middleware.insert_before Rails::Rack::Logger, SmartLogger::RackLoggerMarker
69
+ SmartLogger.start(name)
70
+ end
71
+
72
+ def self.format_message(severity, message = nil, progname = nil)
73
+ "[%s%s] [%s: %s] %s\n" % [
74
+ SmartLogger::water_mark,
75
+ SmartLogger::params,
76
+ severity,
77
+ Time.zone.now.strftime("%H:%M:%S"),
78
+ (message || progname).to_s.lstrip]
79
+ end
80
+
81
+ private
82
+ def self.create_watermark
83
+ Thread.current[:logger_mark] = "WM##{SecureRandom.hex(5)}"
84
+ end
85
+
86
+ def self.reset_params
87
+ Thread.current[:logger_params] = { }
88
+ end
89
+
90
+ def self.save_param(name, value)
91
+ Thread.current[:logger_params] ||= {}
92
+ Thread.current[:logger_params][name] = value
93
+ end
94
+ end
95
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_logger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Boris Dinkevich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-04-15 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Smart grouping of log entries
17
+ email: do@itlater.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/smart_logger.rb
26
+ - lib/smart_logger/active_support.rb
27
+ - lib/smart_logger/logger.rb
28
+ - lib/smart_logger/rack.rb
29
+ - lib/smart_logger/resque.rb
30
+ homepage: https://github.com/borisd/smart_logger
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.11
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Smart tagged log
57
+ test_files: []
58
+
59
+ has_rdoc: