hq-logger 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ require "hq/logger/io-logger"
2
+
3
+ module HQ
4
+ class Logger
5
+
6
+ class AnsiLogger < IoLogger
7
+
8
+ ANSI_CODES = {
9
+ :normal => "\e[0m",
10
+ :bold => "\e[1m",
11
+ :black => "\e[30m",
12
+ :red => "\e[31m",
13
+ :green => "\e[32m",
14
+ :yellow => "\e[33m",
15
+ :blue => "\e[34m",
16
+ :magenta => "\e[35m",
17
+ :cyan => "\e[36m",
18
+ :white => "\e[37m",
19
+ }
20
+
21
+ MESSAGE_COLOURS = {
22
+
23
+ :normal => :normal,
24
+
25
+ :hostname => :blue,
26
+
27
+ :timing => :blue,
28
+ :trace => :magenta,
29
+ :debug => :cyan,
30
+ :detail => :white,
31
+ :notice => :green,
32
+ :warning => :yellow,
33
+ :error => :red,
34
+
35
+ :command_output => :white,
36
+ }
37
+
38
+ DIFF_COLOURS = {
39
+ :minus_minus_minus => :magenta,
40
+ :plus_plus_plus => :magenta,
41
+ :at_at => :magenta,
42
+ :minus => :red,
43
+ :plus => :blue,
44
+ :else => :white,
45
+ }
46
+
47
+ def valid_modes
48
+ [ :normal, :partial ]
49
+ end
50
+
51
+ def ansi_line text, stuff, colour, prefix = ""
52
+
53
+ raise "No such colour: #{colour}" \
54
+ unless MESSAGE_COLOURS[colour] || ANSI_CODES[colour]
55
+
56
+ out.print \
57
+ ANSI_CODES[:bold],
58
+ ANSI_CODES[MESSAGE_COLOURS[:hostname]],
59
+ stuff[:hostname],
60
+ ": ",
61
+ ANSI_CODES[colour] || ANSI_CODES[MESSAGE_COLOURS[colour]],
62
+ stuff[:prefix] + prefix,
63
+ text,
64
+ ANSI_CODES[:normal],
65
+ "\n"
66
+
67
+ end
68
+
69
+ def output_real content, stuff
70
+
71
+ if content.is_a? String
72
+ ansi_line content, stuff, :normal
73
+ return
74
+ end
75
+
76
+ case content["type"]
77
+
78
+ when "log"
79
+
80
+ ansi_line content["text"], stuff, stuff[:level]
81
+
82
+ if content["content"]
83
+ content["content"].each do
84
+ |item|
85
+ output item, stuff, " "
86
+ end
87
+ end
88
+
89
+ when "exception"
90
+
91
+ ansi_line content["text"], stuff, stuff[:level]
92
+ ansi_line content["message"], stuff, :normal, " "
93
+
94
+ content["backtrace"].each do |frame|
95
+ ansi_line frame, stuff, :normal, " "
96
+ end
97
+
98
+ when "diff"
99
+
100
+ ansi_line content["text"], stuff, stuff[:level]
101
+
102
+ content["content"].each do
103
+ |line|
104
+ colour = DIFF_COLOURS[line["type"].gsub("-", "_")[5..-1].to_sym]
105
+ ansi_line line["text"], stuff, colour, " "
106
+ end
107
+
108
+ when "command"
109
+
110
+ ansi_line content["text"], stuff, stuff[:level]
111
+
112
+ if content["output"]
113
+
114
+ content["output"].each do
115
+ |line|
116
+ ansi_line line, stuff, :normal, " "
117
+ end
118
+
119
+ end
120
+
121
+ when "command-output"
122
+
123
+ ansi_line content["text"], stuff, :normal, " "
124
+
125
+ else
126
+
127
+ pp content
128
+ raise "Error"
129
+
130
+ end
131
+
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,288 @@
1
+ require "hq/tools/escape"
2
+ require "hq/logger/io-logger"
3
+
4
+ module HQ
5
+ class Logger
6
+
7
+ class HtmlLogger < IoLogger
8
+
9
+ include Tools::Escape
10
+
11
+ def valid_modes
12
+ [ :normal, :complete ]
13
+ end
14
+
15
+ def output_real content, stuff
16
+
17
+ if content.is_a? String
18
+
19
+ out.print \
20
+ stuff[:prefix],
21
+ "<div class=\"hq-log-simple\">",
22
+ esc_ht(content),
23
+ "</div>\n"
24
+
25
+ return
26
+
27
+ end
28
+
29
+ case content["type"]
30
+
31
+ when "log"
32
+
33
+ out.print \
34
+ stuff[:prefix],
35
+ "<div class=\"hq-log-item hq-log-item-",
36
+ esc_ht(content["level"].to_s),
37
+ "\">\n"
38
+
39
+ out.print \
40
+ stuff[:prefix],
41
+ "\t<div class=\"hq-log-head\">\n"
42
+
43
+ out.print \
44
+ stuff[:prefix],
45
+ "\t\t<div class=\"hq-log-hostname\">",
46
+ esc_ht(stuff[:hostname]),
47
+ "</div>\n"
48
+
49
+ out.print \
50
+ stuff[:prefix],
51
+ "\t\t<div class=\"hq-log-text\">",
52
+ esc_ht(content["text"]),
53
+ "</div>\n"
54
+
55
+ out.print \
56
+ stuff[:prefix],
57
+ "\t</div>\n"
58
+
59
+ if content["content"] && ! content["content"].empty?
60
+
61
+ out.print \
62
+ stuff[:prefix],
63
+ "\t<div class=\"hq-log-content\">\n"
64
+
65
+ content["content"].each do
66
+ |item|
67
+ output item, stuff, "\t\t"
68
+ end
69
+
70
+ out.print \
71
+ stuff[:prefix],
72
+ "\t</div>\n"
73
+
74
+ end
75
+
76
+ out.print \
77
+ stuff[:prefix],
78
+ "</div>\n"
79
+
80
+ when "exception"
81
+
82
+ out.print \
83
+ stuff[:prefix],
84
+ "<div class=\"hq-log-item hq-log-item-",
85
+ esc_ht(content["level"]),
86
+ "\">\n"
87
+
88
+ out.print \
89
+ stuff[:prefix],
90
+ "\t<div class=\"hq-log-head\">\n"
91
+
92
+ out.print \
93
+ stuff[:prefix],
94
+ "\t\t<div class=\"hq-log-hostname\">",
95
+ esc_ht(stuff[:hostname]),
96
+ "</div>\n"
97
+
98
+ out.print \
99
+ stuff[:prefix],
100
+ "\t\t<div class=\"hq-log-text\">",
101
+ esc_ht(content["text"]),
102
+ "</div>\n"
103
+
104
+ out.print \
105
+ stuff[:prefix],
106
+ "\t</div>\n"
107
+
108
+ out.print \
109
+ stuff[:prefix],
110
+ "\t<div class=\"hq-log-content\">\n"
111
+
112
+ out.print \
113
+ stuff[:prefix],
114
+ "\t\t<div class=\"hq-log-exception\">\n"
115
+
116
+ out.print \
117
+ stuff[:prefix],
118
+ "\t\t\t<div class=\"hq-log-exception-message\">",
119
+ esc_ht(content["message"]),
120
+ "</div>\n"
121
+
122
+ out.print \
123
+ stuff[:prefix],
124
+ "\t\t\t<div class=\"hq-log-exception-backtrace\">\n"
125
+
126
+ content["backtrace"].each do
127
+ |line|
128
+
129
+ out.print \
130
+ stuff[:prefix],
131
+ "\t\t\t\t<div class=\"hq-log-exception-backtrace-line\">",
132
+ esc_ht(line),
133
+ "</div>\n"
134
+
135
+ end
136
+
137
+ out.print \
138
+ stuff[:prefix],
139
+ "\t\t\t</div>\n"
140
+
141
+ out.print \
142
+ stuff[:prefix],
143
+ "\t\t</div>\n"
144
+
145
+ out.print \
146
+ stuff[:prefix],
147
+ "\t</div>\n"
148
+
149
+ out.print \
150
+ stuff[:prefix],
151
+ "</div>\n"
152
+
153
+ when "diff"
154
+
155
+ out.print \
156
+ stuff[:prefix],
157
+ "<div class=\"hq-log-item hq-log-item-",
158
+ esc_ht(content["level"]),
159
+ "\">\n"
160
+
161
+ out.print \
162
+ stuff[:prefix],
163
+ "\t<div class=\"hq-log-head\">\n"
164
+
165
+ out.print \
166
+ stuff[:prefix],
167
+ "\t\t<div class=\"hq-log-hostname\">",
168
+ esc_ht(stuff[:hostname]),
169
+ "</div>\n"
170
+
171
+ out.print \
172
+ stuff[:prefix],
173
+ "\t\t<div class=\"hq-log-text\">",
174
+ esc_ht(content["text"]),
175
+ "</div>\n"
176
+
177
+ out.print \
178
+ stuff[:prefix],
179
+ "\t</div>\n"
180
+
181
+ out.print \
182
+ stuff[:prefix],
183
+ "\t<div class=\"hq-log-content\">\n"
184
+
185
+ out.print \
186
+ stuff[:prefix],
187
+ "\t\t<div class=\"hq-log-diff\">\n"
188
+
189
+ content["content"].each do
190
+ |line|
191
+
192
+ out.print \
193
+ stuff[:prefix],
194
+ "\t\t\t<div class=\"hq-log-",
195
+ esc_ht(line["type"]),
196
+ "\">",
197
+ esc_ht(line["text"]),
198
+ "</div>\n"
199
+
200
+ end
201
+
202
+ out.print \
203
+ stuff[:prefix],
204
+ "\t\t</div>\n"
205
+
206
+ out.print \
207
+ stuff[:prefix],
208
+ "\t</div>\n"
209
+
210
+ out.print \
211
+ stuff[:prefix],
212
+ "</div>\n"
213
+
214
+ when "command"
215
+
216
+ out.print \
217
+ stuff[:prefix],
218
+ "<div class=\"hq-log-item hq-log-item-",
219
+ esc_ht(content["level"]),
220
+ "\">\n"
221
+
222
+ out.print \
223
+ stuff[:prefix],
224
+ "\t<div class=\"hq-log-head\">\n"
225
+
226
+ out.print \
227
+ stuff[:prefix],
228
+ "\t\t<div class=\"hq-log-hostname\">",
229
+ esc_ht(stuff[:hostname]),
230
+ "</div>\n"
231
+
232
+ out.print \
233
+ stuff[:prefix],
234
+ "\t\t<div class=\"hq-log-text\">",
235
+ esc_ht(content["text"]),
236
+ "</div>\n"
237
+
238
+ out.print \
239
+ stuff[:prefix],
240
+ "\t</div>\n"
241
+
242
+ if content["output"]
243
+
244
+ out.print \
245
+ stuff[:prefix],
246
+ "\t<div class=\"hq-log-content\">\n"
247
+
248
+ out.print \
249
+ stuff[:prefix],
250
+ "\t\t<div class=\"hq-log-command-output\">\n"
251
+
252
+ content["output"].each do
253
+ |line|
254
+
255
+ out.print \
256
+ stuff[:prefix],
257
+ "\t\t\t<div class=\"hq-log-command-output-line\">",
258
+ esc_ht(line),
259
+ "</div>\n"
260
+
261
+ end
262
+
263
+ out.print \
264
+ stuff[:prefix],
265
+ "\t\t</div>\n"
266
+
267
+ out.print \
268
+ stuff[:prefix],
269
+ "\t</div>\n"
270
+
271
+ end
272
+
273
+ out.print \
274
+ stuff[:prefix],
275
+ "</div>\n"
276
+
277
+ else
278
+
279
+ pp content
280
+
281
+ end
282
+
283
+ end
284
+
285
+ end
286
+
287
+ end
288
+ end
@@ -0,0 +1,52 @@
1
+ module HQ
2
+ class Logger
3
+
4
+ class IoLogger
5
+
6
+ attr_accessor :out
7
+ attr_accessor :level
8
+
9
+ def fix_stuff old_stuff, content, prefix = nil
10
+
11
+ content = {} \
12
+ unless content.is_a? Hash
13
+
14
+ new_stuff = {
15
+ hostname: content["hostname"] || old_stuff[:hostname],
16
+ level: (content["level"] || old_stuff[:level]).to_sym,
17
+ prefix: (old_stuff[:prefix] || "") + (prefix || ""),
18
+ mode: old_stuff[:mode].to_sym,
19
+ }
20
+
21
+ raise "No hostname" \
22
+ unless new_stuff[:hostname]
23
+
24
+ raise "No level" \
25
+ unless new_stuff[:level]
26
+
27
+ return new_stuff
28
+
29
+ end
30
+
31
+ def output content, stuff = {}, prefix = ""
32
+
33
+ stuff = fix_stuff stuff, content, prefix
34
+
35
+ # check we want to output this entry
36
+
37
+ return unless Logger.level_includes \
38
+ level,
39
+ stuff[:level]
40
+
41
+ return unless valid_modes.include? stuff[:mode].to_sym
42
+
43
+ # output it
44
+
45
+ output_real content, stuff
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ module HQ
2
+ class Logger
3
+
4
+ class MultiLogger
5
+
6
+ def initialize
7
+ @loggers = []
8
+ end
9
+
10
+ def add_logger logger
11
+ @loggers << logger
12
+ end
13
+
14
+ def output content, stuff, prefix = ""
15
+ @loggers.each do
16
+ |logger|
17
+ logger.output content, stuff, prefix
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ require "hq/logger/io-logger"
2
+
3
+ require "multi_json"
4
+
5
+ module HQ
6
+ class Logger
7
+
8
+ class RawLogger < IoLogger
9
+
10
+ def valid_modes
11
+ [ :normal, :partial, :complete ]
12
+ end
13
+
14
+ def output_real content, stuff
15
+
16
+ data = {
17
+ mode: stuff[:mode],
18
+ content: [ content ],
19
+ }
20
+
21
+ data_json = begin
22
+ MultiJson.dump data
23
+ rescue
24
+ out.puts "ERROR encoding #{content} (#{content.encoding})"
25
+ end
26
+
27
+ out.print MultiJson.dump(data) + "\n"
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,95 @@
1
+ require "hq/logger/io-logger"
2
+
3
+ module HQ
4
+ class Logger
5
+
6
+ class TextLogger < IoLogger
7
+
8
+ def valid_modes
9
+ [ :normal, :partial ]
10
+ end
11
+
12
+ def text_line text, stuff, prefix = ""
13
+
14
+ out.print \
15
+ stuff[:hostname],
16
+ " ",
17
+ stuff[:level],
18
+ ": ",
19
+ stuff[:prefix] + prefix,
20
+ text,
21
+ "\n"
22
+
23
+ end
24
+
25
+ def output_real content, stuff
26
+
27
+ if content.is_a? String
28
+ text_line content, stuff
29
+ return
30
+ end
31
+
32
+ case content["type"]
33
+
34
+ when "log"
35
+
36
+ text_line content["text"], stuff
37
+
38
+ if content["content"]
39
+
40
+ content["content"].each do
41
+ |item|
42
+ output item, stuff, " "
43
+ end
44
+
45
+ end
46
+
47
+ when "exception"
48
+
49
+ text_line content["text"], stuff
50
+ text_line content["message"], stuff, " "
51
+
52
+ content["backtrace"].each do
53
+ |frame|
54
+ output frame, stuff, " "
55
+ end
56
+
57
+ when "diff"
58
+
59
+ text_line content["text"], stuff
60
+
61
+ content["content"].each do
62
+ |line|
63
+ output line["text"], stuff, " "
64
+ end
65
+
66
+ when "command"
67
+
68
+ text_line content["text"], stuff
69
+
70
+ if content["output"]
71
+
72
+ content["output"].each do
73
+ |line|
74
+ output line, stuff, " "
75
+ end
76
+
77
+ end
78
+
79
+ when "command-output"
80
+
81
+ text_line content["text"], stuff, " "
82
+
83
+ else
84
+
85
+ pp content
86
+ raise "Error"
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end