koa-utils 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/koa-utils/logger.rb +77 -44
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28698360221534e0bd5c1b86b6979352f3f8876c
|
4
|
+
data.tar.gz: 7e30b74a6c131b38f072f37fe7af282b850ef495
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 044ba256ce989617459fb8e57e42f98b876513c55eb312f503f077e87fa5ce461d25fa177640e18ab756d4405de07f6496793b89efda685ee5a26e555df31eac
|
7
|
+
data.tar.gz: 11c03c3b2b21bcf57942c7867002f22e3638c7505d5d59c0af7fdd15075ef69a971cbc287168146b98e5d25e538302f9b9ac2c6f7272432ba55b95bb3eb375e2
|
data/lib/koa-utils/logger.rb
CHANGED
@@ -1,52 +1,85 @@
|
|
1
1
|
$stdout.sync = true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
module KOAUtils
|
3
|
+
class StringLogger
|
4
|
+
def initialize
|
5
|
+
@msgs = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def puts(msg)
|
9
|
+
@msgs << msg
|
10
|
+
end
|
11
|
+
|
12
|
+
def flush
|
13
|
+
@msgs.length.times.map {@msgs.pop}
|
9
14
|
end
|
10
|
-
@prefix ||= ""
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.prefix(name)
|
14
|
-
appname+name
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.librato_log(type, name, val, data)
|
18
|
-
name = prefix(name)
|
19
|
-
data["#{type}##{name}"] = val
|
20
|
-
log(data)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.log(data)
|
24
|
-
$stdout.puts(stringify(data))
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.measure_block(name, data = {})
|
28
|
-
start = Time.now
|
29
|
-
result = yield
|
30
|
-
elapsed = (Time.now.to_f - start.to_f) * 1000
|
31
|
-
measure(name, elapsed.round.to_s+"ms")
|
32
|
-
result
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.count(name, val, data = {})
|
36
|
-
librato_log("count", name, val, data)
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.measure(name, val, data = {})
|
40
|
-
librato_log("measure", name, val, data)
|
41
15
|
end
|
42
16
|
|
43
|
-
|
44
|
-
|
17
|
+
module NullLogger
|
18
|
+
def self.puts(*args)
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.flush(*args)
|
23
|
+
|
24
|
+
end
|
45
25
|
end
|
46
26
|
|
47
|
-
|
48
|
-
|
49
|
-
|
27
|
+
module Logger
|
28
|
+
def self.appname
|
29
|
+
@prefix ||= ENV['APP_NAME'].downcase if ENV['APP_NAME']
|
30
|
+
if defined? ::Rails
|
31
|
+
@prefix ||= Rails.application.class.parent_name.downcase+"."
|
32
|
+
end
|
33
|
+
@prefix ||= ""
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.prefix(name)
|
37
|
+
appname+name
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.librato_log(type, name, val, data)
|
41
|
+
name = prefix(name)
|
42
|
+
data["#{type}##{name}"] = val
|
43
|
+
log(data)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.out=(o)
|
47
|
+
@out = o
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.out
|
51
|
+
@out || $stdout
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.log(data)
|
55
|
+
out.puts(stringify(data))
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.measure_block(name, data = {})
|
59
|
+
start = Time.now
|
60
|
+
result = yield
|
61
|
+
elapsed = (Time.now.to_f - start.to_f) * 1000
|
62
|
+
measure(name, elapsed.round.to_s+"ms")
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.count(name, val, data = {})
|
67
|
+
librato_log("count", name, val, data)
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.measure(name, val, data = {})
|
71
|
+
librato_log("measure", name, val, data)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.sample(name, val, data = {})
|
75
|
+
librato_log("sample", name, val, data)
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.stringify(data)
|
79
|
+
data.reduce(out=String.new) do |s, tup|
|
80
|
+
s << [tup.first, tup.last].join("=") << " "
|
81
|
+
end
|
50
82
|
end
|
51
83
|
end
|
52
|
-
|
84
|
+
|
85
|
+
end
|