padrino-core 0.10.6.a → 0.10.6.b
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/padrino-core/logger.rb +170 -87
- data/lib/padrino-core/server.rb +1 -1
- data/lib/padrino-core/version.rb +1 -1
- data/test/test_logger.rb +54 -0
- metadata +7 -6
data/lib/padrino-core/logger.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Defines the log level for a Padrino project.
|
2
2
|
PADRINO_LOG_LEVEL = ENV['PADRINO_LOG_LEVEL'] unless defined?(PADRINO_LOG_LEVEL)
|
3
|
+
|
3
4
|
# Defines the logger used for a Padrino project.
|
4
|
-
PADRINO_LOGGER
|
5
|
+
PADRINO_LOGGER = ENV['PADRINO_LOGGER'] unless defined?(PADRINO_LOGGER)
|
5
6
|
|
6
7
|
module Padrino
|
7
|
-
|
8
8
|
##
|
9
9
|
# @return [Padrino::Logger]
|
10
10
|
#
|
@@ -35,21 +35,14 @@ module Padrino
|
|
35
35
|
# Padrino.logger = Buffered.new(STDOUT)
|
36
36
|
#
|
37
37
|
def self.logger=(value)
|
38
|
+
value.extend(Padrino::Logger::Extensions) unless (Padrino::Logger::Extensions === value)
|
38
39
|
Thread.current[:padrino_logger] = value
|
39
40
|
end
|
40
41
|
|
41
42
|
##
|
42
|
-
#
|
43
|
+
# Padrinos internal logger, using all of Padrino log extensions.
|
43
44
|
#
|
44
45
|
class Logger
|
45
|
-
|
46
|
-
attr_accessor :level
|
47
|
-
attr_accessor :auto_flush
|
48
|
-
attr_reader :buffer
|
49
|
-
attr_reader :log
|
50
|
-
attr_reader :init_args
|
51
|
-
attr_accessor :log_static
|
52
|
-
|
53
46
|
##
|
54
47
|
# Ruby (standard) logger levels:
|
55
48
|
#
|
@@ -58,6 +51,7 @@ module Padrino
|
|
58
51
|
# :warn:: A warning
|
59
52
|
# :info:: generic (useful) information about system operation
|
60
53
|
# :debug:: low-level information for developers
|
54
|
+
# :devel:: Development-related information that is unnecessary in debug mode
|
61
55
|
#
|
62
56
|
Levels = {
|
63
57
|
:fatal => 7,
|
@@ -68,6 +62,159 @@ module Padrino
|
|
68
62
|
:devel => -1,
|
69
63
|
} unless const_defined?(:Levels)
|
70
64
|
|
65
|
+
module Extensions
|
66
|
+
##
|
67
|
+
# Generate the logging methods for {Padrino.logger} for each log level.
|
68
|
+
#
|
69
|
+
Padrino::Logger::Levels.each_pair do |name, number|
|
70
|
+
define_method(name) do |*args|
|
71
|
+
return if number < level
|
72
|
+
if args.size > 1
|
73
|
+
bench(*args)
|
74
|
+
else
|
75
|
+
push(args * '', name)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
define_method(:"#{name}?") do
|
80
|
+
number >= level
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
##
|
85
|
+
# Append a to development logger a given action with time
|
86
|
+
#
|
87
|
+
# @param [string] action
|
88
|
+
# The action
|
89
|
+
#
|
90
|
+
# @param [float] time
|
91
|
+
# Time duration for the given action
|
92
|
+
#
|
93
|
+
# @param [message] string
|
94
|
+
# The message that you want to log
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# logger.bench 'GET', started_at, '/blog/categories'
|
98
|
+
# # => DEBUG - GET (0.056ms) - /blog/categories
|
99
|
+
#
|
100
|
+
def bench(action, began_at, message, level=:debug, color=:yellow)
|
101
|
+
@_pad ||= 8
|
102
|
+
@_pad = action.to_s.size if action.to_s.size > @_pad
|
103
|
+
duration = Time.now - began_at
|
104
|
+
color = :red if duration > 1
|
105
|
+
push "%s (" % colorize(action.to_s.upcase.rjust(@_pad), color) + colorize("%0.4fms", :bold, color) % duration + ") %s" % message.to_s, level
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Appends a message to the log. The methods yield to an optional block and
|
110
|
+
# the output of this block will be appended to the message.
|
111
|
+
#
|
112
|
+
# @param [String] message
|
113
|
+
# The message that you want write to your stream
|
114
|
+
#
|
115
|
+
# @param [String] level
|
116
|
+
# The level one of :debug, :warn etc...
|
117
|
+
#
|
118
|
+
#
|
119
|
+
def push(message = nil, level = nil)
|
120
|
+
add(Padrino::Logger::Levels[level], format(message, level))
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# Formats the log message. This method is a noop and should be implemented by other
|
125
|
+
# logger components such as {Padrino::Logger}.
|
126
|
+
#
|
127
|
+
# @param [String] message
|
128
|
+
# The message to format
|
129
|
+
#
|
130
|
+
# @param [String,Symbol] level
|
131
|
+
# The log level, one of :debug, :warn...
|
132
|
+
def format(message, level)
|
133
|
+
message
|
134
|
+
end
|
135
|
+
|
136
|
+
##
|
137
|
+
# The debug level, with some style added. May be reimplemented.
|
138
|
+
#
|
139
|
+
# @example
|
140
|
+
# stylized_level(:debug) => DEBUG
|
141
|
+
#
|
142
|
+
# @param [String,Symbol] level
|
143
|
+
# The log level
|
144
|
+
#
|
145
|
+
def stylized_level(level)
|
146
|
+
level.to_s.upcase.rjust(7)
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# Colorizes a string for colored console output. This is a noop and can be reimplemented
|
151
|
+
# to colorize the string as needed.
|
152
|
+
#
|
153
|
+
# @see
|
154
|
+
# ColorizedLogger
|
155
|
+
#
|
156
|
+
# @param [string]
|
157
|
+
# The string to be colorized.
|
158
|
+
#
|
159
|
+
# @param [Array<Symbol>]
|
160
|
+
# The colors to use. Should be applied in the order given.
|
161
|
+
def colorize(string, *colors)
|
162
|
+
string
|
163
|
+
end
|
164
|
+
|
165
|
+
##
|
166
|
+
# Turns a logger with LoggingExtensions into a logger with colorized output.
|
167
|
+
#
|
168
|
+
# @example
|
169
|
+
# Padrino.logger = Logger.new($stdout)
|
170
|
+
# Padrino.logger.colorize!
|
171
|
+
# Padrino.logger.debug("Fancy Padrino debug string")
|
172
|
+
def colorize!
|
173
|
+
self.extend(Colorize)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
module Colorize
|
178
|
+
# Colors for levels
|
179
|
+
ColoredLevels = {
|
180
|
+
:fatal => [:bold, :red],
|
181
|
+
:error => [:red],
|
182
|
+
:warn => [:yellow],
|
183
|
+
:info => [:green],
|
184
|
+
:debug => [:cyan],
|
185
|
+
:devel => [:magenta]
|
186
|
+
} unless defined?(ColoredLevels)
|
187
|
+
|
188
|
+
##
|
189
|
+
# Colorize our level
|
190
|
+
#
|
191
|
+
# @param [String, Symbol] level
|
192
|
+
#
|
193
|
+
# @see Padrino::Logging::ColorizedLogger::ColoredLevels
|
194
|
+
#
|
195
|
+
def colorize(string, *colors)
|
196
|
+
colors.each do |c|
|
197
|
+
string = string.send(c)
|
198
|
+
end
|
199
|
+
string
|
200
|
+
end
|
201
|
+
|
202
|
+
def stylized_level(level)
|
203
|
+
style = ColoredLevels[level].map { |c| "\e[%dm" % String.colors[c] } * ''
|
204
|
+
[style, super, "\e[0m"] * ''
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
include Extensions
|
209
|
+
include Colorize
|
210
|
+
|
211
|
+
attr_accessor :level
|
212
|
+
attr_accessor :auto_flush
|
213
|
+
attr_reader :buffer
|
214
|
+
attr_reader :log
|
215
|
+
attr_reader :init_args
|
216
|
+
attr_accessor :log_static
|
217
|
+
|
71
218
|
@@mutex = {}
|
72
219
|
|
73
220
|
##
|
@@ -110,16 +257,6 @@ module Padrino
|
|
110
257
|
}
|
111
258
|
Config.merge!(PADRINO_LOGGER) if PADRINO_LOGGER
|
112
259
|
|
113
|
-
# Colors for levels
|
114
|
-
ColoredLevels = {
|
115
|
-
:fatal => [:bold, :red],
|
116
|
-
:error => [:red],
|
117
|
-
:warn => [:yellow],
|
118
|
-
:info => [:green],
|
119
|
-
:debug => [:cyan],
|
120
|
-
:devel => [:magenta]
|
121
|
-
} unless defined?(ColoredLevels)
|
122
|
-
|
123
260
|
##
|
124
261
|
# Setup a new logger
|
125
262
|
#
|
@@ -144,6 +281,7 @@ module Padrino
|
|
144
281
|
when :stderr then $stderr
|
145
282
|
else config[:stream] # return itself, probabilly is a custom stream.
|
146
283
|
end
|
284
|
+
|
147
285
|
Thread.current[:padrino_logger] = Padrino::Logger.new(config.merge(:stream => stream))
|
148
286
|
end
|
149
287
|
|
@@ -174,7 +312,7 @@ module Padrino
|
|
174
312
|
def initialize(options={})
|
175
313
|
@buffer = []
|
176
314
|
@auto_flush = options.has_key?(:auto_flush) ? options[:auto_flush] : true
|
177
|
-
@level = options[:log_level] ? Levels[options[:log_level]] : Levels[:debug]
|
315
|
+
@level = options[:log_level] ? Padrino::Logger::Levels[options[:log_level]] : Padrino::Logger::Levels[:debug]
|
178
316
|
@log = options[:stream] || $stdout
|
179
317
|
@log.sync = true
|
180
318
|
@mutex = @@mutex[@log] ||= Mutex.new
|
@@ -183,18 +321,6 @@ module Padrino
|
|
183
321
|
@log_static = options.has_key?(:log_static) ? options[:log_static] : false
|
184
322
|
end
|
185
323
|
|
186
|
-
##
|
187
|
-
# Colorize our level
|
188
|
-
#
|
189
|
-
# @param [String, Symbol] level
|
190
|
-
#
|
191
|
-
# @see Padrino::Logger::ColoredLevels
|
192
|
-
#
|
193
|
-
def colored_level(level)
|
194
|
-
style = ColoredLevels[level].map { |c| "\e[%dm" % String.colors[c] } * ''
|
195
|
-
[style, level.to_s.upcase.rjust(7), "\e[0m"] * ''
|
196
|
-
end
|
197
|
-
|
198
324
|
##
|
199
325
|
# Flush the entire buffer to the log object.
|
200
326
|
#
|
@@ -217,42 +343,11 @@ module Padrino
|
|
217
343
|
end
|
218
344
|
|
219
345
|
##
|
220
|
-
#
|
221
|
-
# the output of this block will be appended to the message.
|
222
|
-
#
|
223
|
-
# @param [String] message
|
224
|
-
# The message that you want write to your stream
|
225
|
-
#
|
226
|
-
# @param [String] level
|
227
|
-
# The level one of :debug, :warn etc...
|
228
|
-
#
|
229
|
-
#
|
230
|
-
def push(message = nil, level = nil)
|
231
|
-
write @format_message % [colored_level(level), Time.now.strftime(@format_datetime).yellow, message.to_s.strip]
|
232
|
-
end
|
233
|
-
|
234
|
-
##
|
235
|
-
# Append a to development logger a given action with time
|
236
|
-
#
|
237
|
-
# @param [string] action
|
238
|
-
# The action
|
239
|
-
#
|
240
|
-
# @param [float] time
|
241
|
-
# Time duration for the given action
|
242
|
-
#
|
243
|
-
# @param [message] string
|
244
|
-
# The message that you want to log
|
245
|
-
#
|
246
|
-
# @example
|
247
|
-
# logger.bench 'GET', started_at, '/blog/categories'
|
248
|
-
# # => DEBUG - GET (0.056ms) - /blog/categories
|
346
|
+
# Adds a message to the log - for compatibility with other loggers.
|
249
347
|
#
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
duration = Time.now - began_at
|
254
|
-
color = :red if duration > 1
|
255
|
-
push "%s (" % action.to_s.upcase.rjust(@_pad).send(color) + "%0.4fms".bold.send(color) % duration + ") %s" % message.to_s, level
|
348
|
+
# @private
|
349
|
+
def add(level, message = nil)
|
350
|
+
write(message)
|
256
351
|
end
|
257
352
|
|
258
353
|
##
|
@@ -269,24 +364,12 @@ module Padrino
|
|
269
364
|
end
|
270
365
|
alias :write :<<
|
271
366
|
|
272
|
-
|
273
|
-
|
274
|
-
#
|
275
|
-
Levels.each_pair do |name, number|
|
276
|
-
define_method(name) do |*args|
|
277
|
-
return if number < level
|
278
|
-
if args.size > 1
|
279
|
-
bench(*args)
|
280
|
-
else
|
281
|
-
push(args * '', name)
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
define_method(:"#{name}?") do
|
286
|
-
number >= level
|
287
|
-
end
|
367
|
+
def format(message, level)
|
368
|
+
@format_message % [stylized_level(level), colorize(Time.now.strftime(@format_datetime), :yellow), message.to_s.strip]
|
288
369
|
end
|
289
370
|
|
371
|
+
|
372
|
+
|
290
373
|
##
|
291
374
|
# Padrino::Loggger::Rack forwards every request to an +app+ given, and
|
292
375
|
# logs a line in the Apache common log format to the +logger+, or
|
@@ -301,7 +384,6 @@ module Padrino
|
|
301
384
|
|
302
385
|
def call(env) # @private
|
303
386
|
env['rack.logger'] = Padrino.logger
|
304
|
-
env['rack.errors'] = Padrino.logger.log
|
305
387
|
began_at = Time.now
|
306
388
|
status, header, body = @app.call(env)
|
307
389
|
log(env, status, header, began_at)
|
@@ -319,7 +401,7 @@ module Padrino
|
|
319
401
|
env["PATH_INFO"],
|
320
402
|
env["QUERY_STRING"].empty? ? "" : "?" + env["QUERY_STRING"],
|
321
403
|
' - ',
|
322
|
-
status.to_s[0..3]
|
404
|
+
logger.colorize(status.to_s[0..3], :bold),
|
323
405
|
' ',
|
324
406
|
code_to_name(status)
|
325
407
|
] * '',
|
@@ -343,3 +425,4 @@ module Kernel # @private
|
|
343
425
|
Padrino.logger
|
344
426
|
end
|
345
427
|
end # Kernel
|
428
|
+
|
data/lib/padrino-core/server.rb
CHANGED
@@ -17,7 +17,7 @@ module Padrino
|
|
17
17
|
#
|
18
18
|
class Server < Rack::Server
|
19
19
|
# Server Handlers
|
20
|
-
Handlers = [:thin, :mongrel, :webrick]
|
20
|
+
Handlers = [:thin, :mongrel, :trinidad, :webrick]
|
21
21
|
|
22
22
|
# Starts the application on the available server with specified options.
|
23
23
|
def self.start(app, opts={})
|
data/lib/padrino-core/version.rb
CHANGED
data/test/test_logger.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
+
require 'lumberjack'
|
3
|
+
require 'logger'
|
2
4
|
|
3
5
|
describe "PadrinoLogger" do
|
4
6
|
|
@@ -98,3 +100,55 @@ describe "PadrinoLogger" do
|
|
98
100
|
end
|
99
101
|
end
|
100
102
|
end
|
103
|
+
|
104
|
+
describe "alternate logger: Lumberjack" do
|
105
|
+
def setup_logger
|
106
|
+
@log = StringIO.new
|
107
|
+
Padrino.logger = Lumberjack::Logger.new(@log, :level => :debug)
|
108
|
+
end
|
109
|
+
|
110
|
+
should "annotate the logger to support additional Padrino fancyness" do
|
111
|
+
setup_logger
|
112
|
+
Padrino.logger.debug("Debug message")
|
113
|
+
assert_match(/Debug message/, @log.string)
|
114
|
+
end
|
115
|
+
|
116
|
+
should "colorize log output after colorize! is called" do
|
117
|
+
setup_logger
|
118
|
+
Padrino.logger.colorize!
|
119
|
+
|
120
|
+
mock_app do
|
121
|
+
enable :logging
|
122
|
+
get("/"){ "Foo" }
|
123
|
+
end
|
124
|
+
get "/"
|
125
|
+
|
126
|
+
assert_match /\e\[1m200\e\[0m OK/, @log.string
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "alternate logger: stdlib logger" do
|
131
|
+
def setup_logger
|
132
|
+
@log = StringIO.new
|
133
|
+
Padrino.logger = Logger.new(@log)
|
134
|
+
end
|
135
|
+
|
136
|
+
should "annotate the logger to support additional Padrino fancyness" do
|
137
|
+
setup_logger
|
138
|
+
Padrino.logger.debug("Debug message")
|
139
|
+
assert_match(/Debug message/, @log.string)
|
140
|
+
end
|
141
|
+
|
142
|
+
should "colorize log output after colorize! is called" do
|
143
|
+
setup_logger
|
144
|
+
Padrino.logger.colorize!
|
145
|
+
|
146
|
+
mock_app do
|
147
|
+
enable :logging
|
148
|
+
get("/"){ "Foo" }
|
149
|
+
end
|
150
|
+
get "/"
|
151
|
+
|
152
|
+
assert_match /\e\[1m200\e\[0m OK/, @log.string
|
153
|
+
end
|
154
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 99
|
5
5
|
prerelease: 7
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 10
|
9
9
|
- 6
|
10
|
-
-
|
11
|
-
version: 0.10.6.
|
10
|
+
- b
|
11
|
+
version: 0.10.6.b
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Padrino Team
|
@@ -19,7 +19,8 @@ autorequire:
|
|
19
19
|
bindir: bin
|
20
20
|
cert_chain: []
|
21
21
|
|
22
|
-
date: 2012-01-23 00:00:00
|
22
|
+
date: 2012-01-23 00:00:00 -08:00
|
23
|
+
default_executable:
|
23
24
|
dependencies:
|
24
25
|
- !ruby/object:Gem::Dependency
|
25
26
|
name: tilt
|
@@ -186,6 +187,7 @@ files:
|
|
186
187
|
- test/test_restful_routing.rb
|
187
188
|
- test/test_router.rb
|
188
189
|
- test/test_routing.rb
|
190
|
+
has_rdoc: true
|
189
191
|
homepage: http://www.padrinorb.com
|
190
192
|
licenses: []
|
191
193
|
|
@@ -217,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
217
219
|
requirements: []
|
218
220
|
|
219
221
|
rubyforge_project: padrino-core
|
220
|
-
rubygems_version: 1.
|
222
|
+
rubygems_version: 1.6.2
|
221
223
|
signing_key:
|
222
224
|
specification_version: 3
|
223
225
|
summary: The required Padrino core gem
|
@@ -248,4 +250,3 @@ test_files:
|
|
248
250
|
- test/test_restful_routing.rb
|
249
251
|
- test/test_router.rb
|
250
252
|
- test/test_routing.rb
|
251
|
-
has_rdoc:
|