hq-logger 0.0.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.
data/lib/hq/logger.rb ADDED
@@ -0,0 +1,217 @@
1
+ module HQ
2
+ class Logger
3
+
4
+ attr_accessor :hostname
5
+
6
+ MESSAGE_TYPES = [
7
+ :trace,
8
+ :timing,
9
+ :debug,
10
+ :detail,
11
+ :notice,
12
+ :warning,
13
+ :error,
14
+ ]
15
+
16
+ def initialize
17
+
18
+ require "hq/tools/logger/multi-logger"
19
+
20
+ @multi_logger =
21
+ MultiLogger.new
22
+
23
+ end
24
+
25
+ def message text, level, *content
26
+ message text, level, *content
27
+ end
28
+
29
+ def message_partial text, level, *content
30
+ message_partial text, level, options
31
+ end
32
+
33
+ def message_complete text, level, *content
34
+ message_complete text, level, options
35
+ end
36
+
37
+ def trace text, *contents
38
+ message text, :trace, *contents
39
+ end
40
+
41
+ def timing text, *contents
42
+ message text, :timing, *contents
43
+ end
44
+
45
+ def debug text, *contents
46
+ message text, :debug, *contents
47
+ end
48
+
49
+ def detail text, *contents
50
+ message text, :detail, *contents
51
+ end
52
+
53
+ def notice text, *contents
54
+ message text, :notice, *contents
55
+ end
56
+
57
+ def warning text, *contents
58
+ message text, :warning, *contents
59
+ end
60
+
61
+ def error text, *contents
62
+ message text, :error, *contents
63
+ end
64
+
65
+ def time text, level = :timing
66
+
67
+ time_start =
68
+ Time.now
69
+
70
+ begin
71
+
72
+ yield
73
+
74
+ ensure
75
+
76
+ time_end =
77
+ Time.now
78
+
79
+ timing_ms =
80
+ ((time_end - time_start) * 1000).to_i
81
+
82
+ timing_str =
83
+ case timing_ms
84
+ when (0...1000)
85
+ "%dms" % [ timing_ms ]
86
+ when (1000...10000)
87
+ "%.2fs" % [ timing_ms.to_f / 1000 ]
88
+ when (10000...100000)
89
+ "%.1fs" % [ timing_ms.to_f / 1000 ]
90
+ else
91
+ "%ds" % [ timing_ms / 1000 ]
92
+ end
93
+
94
+ message \
95
+ "#{text} took #{timing_str}",
96
+ level
97
+
98
+ end
99
+
100
+ end
101
+
102
+ def die text, status = 1
103
+ error text
104
+ exit status
105
+ end
106
+
107
+ def add_auto str
108
+
109
+ level, format, filename =
110
+ str.split ":", 3
111
+
112
+ add_target \
113
+ filename ? File.open(filename, "a") : STDOUT,
114
+ format || :ansi,
115
+ level || hq_config["default-log-level"] || :detail
116
+
117
+ end
118
+
119
+ def add_target out, format, level
120
+
121
+ raise "Invalid log level #{level}" \
122
+ unless MESSAGE_TYPES.include? level.to_sym
123
+
124
+ logger =
125
+ case format.to_sym
126
+
127
+ when :ansi
128
+ require "hq/tools/logger/ansi-logger"
129
+ HQ::Tools::Logger::AnsiLogger.new
130
+
131
+ when :html
132
+ require "hq/tools/logger/html-logger"
133
+ HQ::Tools::Logger::HtmlLogger.new
134
+
135
+ when :raw
136
+ require "hq/tools/logger/raw-logger"
137
+ HQ::Tools::Logger::RawLogger.new
138
+
139
+ when :text
140
+ require "hq/tools/logger/text-logger"
141
+ HQ::Tools::Logger::TextLogger.new
142
+
143
+ else
144
+ raise "Error"
145
+
146
+ end
147
+
148
+ logger.out = out
149
+ logger.level = level
150
+
151
+ @multi_logger.add_logger logger
152
+
153
+ end
154
+
155
+ def add_logger logger
156
+ @multi_logger.add_logger logger
157
+ end
158
+
159
+ def self.level_includes level_1, level_2
160
+
161
+ index_1 =
162
+ MESSAGE_TYPES.index(level_1.to_sym)
163
+
164
+ index_2 =
165
+ MESSAGE_TYPES.index(level_2.to_sym)
166
+
167
+ return index_1 <= index_2
168
+
169
+ end
170
+
171
+ def output content, mode = :normal
172
+
173
+ raise "Must provide hostname" \
174
+ unless content["hostname"]
175
+
176
+ @multi_logger.output \
177
+ content,
178
+ { mode: mode }
179
+
180
+ end
181
+
182
+ def message text, level, *content
183
+
184
+ output({
185
+ "type" => "log",
186
+ "hostname" => hostname,
187
+ "level" => level,
188
+ "text" => text,
189
+ "content" => content,
190
+ })
191
+
192
+ end
193
+
194
+ def message_partial text, level, *content
195
+
196
+ output({
197
+ "type" => "log",
198
+ "level" => level,
199
+ "text" => text,
200
+ "content" => content
201
+ }, :partial)
202
+
203
+ end
204
+
205
+ def message_complete text, level, *content
206
+
207
+ output({
208
+ "type" => "log",
209
+ "level" => level,
210
+ "text" => text,
211
+ "content" => content
212
+ }, :complete)
213
+
214
+ end
215
+
216
+ end
217
+ end
@@ -0,0 +1,108 @@
1
+ require "hq/logger/ansi-logger"
2
+ require "hq/logger/logger-examples"
3
+
4
+ module HQ
5
+ class Logger
6
+
7
+ describe AnsiLogger do
8
+
9
+ include_examples "a logger"
10
+
11
+ context "#output" do
12
+
13
+ def ansi_line colour, text
14
+ return [
15
+ AnsiLogger::ANSI_CODES[:bold],
16
+ AnsiLogger::ANSI_CODES[:blue],
17
+ "hostname:",
18
+ " ",
19
+ AnsiLogger::ANSI_CODES[colour],
20
+ text,
21
+ AnsiLogger::ANSI_CODES[:normal],
22
+ "\n",
23
+ ]
24
+ end
25
+
26
+ it "log without content" do
27
+
28
+ output_for(sample_log_without_content).should == [
29
+ ansi_line(:cyan, "|text"),
30
+ ].flatten.join
31
+
32
+ end
33
+
34
+ it "log with content" do
35
+
36
+ output_for(sample_log_with_content).should == [
37
+ ansi_line(:cyan, "|text"),
38
+ ansi_line(:normal, "| content 1"),
39
+ ansi_line(:normal, "| content 2"),
40
+ ].flatten.join
41
+
42
+ end
43
+
44
+ it "exception" do
45
+
46
+ output_for(sample_exception).should == [
47
+ ansi_line(:cyan, "|text"),
48
+ ansi_line(:normal, "| message"),
49
+ ansi_line(:normal, "| backtrace 1"),
50
+ ansi_line(:normal, "| backtrace 2"),
51
+ ].join
52
+
53
+ end
54
+
55
+ it "diff" do
56
+
57
+ output_for(sample_diff).should == [
58
+ ansi_line(:cyan, "|text"),
59
+ ansi_line(:magenta, "| diff 1"),
60
+ ansi_line(:magenta, "| diff 2"),
61
+ ansi_line(:magenta, "| diff 3"),
62
+ ansi_line(:red, "| diff 4"),
63
+ ansi_line(:blue, "| diff 5"),
64
+ ansi_line(:white, "| diff 6"),
65
+ ].join
66
+
67
+ end
68
+
69
+ it "command with output" do
70
+
71
+ output_for(sample_command_with_output).should == [
72
+ ansi_line(:cyan, "|text"),
73
+ ansi_line(:normal, "| output 1"),
74
+ ansi_line(:normal, "| output 2"),
75
+ ].join
76
+
77
+ end
78
+
79
+ it "command without output" do
80
+
81
+ output_for(sample_command_without_output).should == [
82
+ ansi_line(:cyan, "|text"),
83
+ ].join
84
+
85
+ end
86
+
87
+ it "command-output" do
88
+
89
+ output_for(sample_command_output).should == [
90
+ ansi_line(:normal, "| text"),
91
+ ].join
92
+
93
+ end
94
+
95
+ end
96
+
97
+ context "#valid_modes" do
98
+
99
+ it "returns [ :normal, :partial ]" do
100
+ subject.valid_modes.should == [ :normal, :partial ]
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+ end
@@ -0,0 +1,148 @@
1
+ require "hq/logger/html-logger"
2
+ require "hq/logger/logger-examples"
3
+
4
+ module HQ
5
+ class Logger
6
+
7
+ describe HtmlLogger do
8
+
9
+ include_examples "a logger"
10
+
11
+ context "#output" do
12
+
13
+ it "log without content" do
14
+
15
+ output_for(sample_log_without_content).should == [
16
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
17
+ "|\t<div class=\"hq-log-head\">\n",
18
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
19
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
20
+ "|\t</div>\n",
21
+ "|</div>\n",
22
+ ].join
23
+
24
+ end
25
+
26
+ it "log with empty content" do
27
+
28
+ output_for(sample_log_with_empty_content).should == [
29
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
30
+ "|\t<div class=\"hq-log-head\">\n",
31
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
32
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
33
+ "|\t</div>\n",
34
+ "|</div>\n",
35
+ ].join
36
+
37
+ end
38
+
39
+ it "log with content" do
40
+
41
+ output_for(sample_log_with_content).should == [
42
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
43
+ "|\t<div class=\"hq-log-head\">\n",
44
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
45
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
46
+ "|\t</div>\n",
47
+ "|\t<div class=\"hq-log-content\">\n",
48
+ "|\t\t<div class=\"hq-log-simple\">content 1</div>\n",
49
+ "|\t\t<div class=\"hq-log-simple\">content 2</div>\n",
50
+ "|\t</div>\n",
51
+ "|</div>\n",
52
+ ].join
53
+
54
+ end
55
+
56
+ it "exception" do
57
+
58
+ output_for(sample_exception).should == [
59
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
60
+ "|\t<div class=\"hq-log-head\">\n",
61
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
62
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
63
+ "|\t</div>\n",
64
+ "|\t<div class=\"hq-log-content\">\n",
65
+ "|\t\t<div class=\"hq-log-exception\">\n",
66
+ "|\t\t\t<div class=\"hq-log-exception-message\">message</div>\n",
67
+ "|\t\t\t<div class=\"hq-log-exception-backtrace\">\n",
68
+ "|\t\t\t\t<div class=\"hq-log-exception-backtrace-line\">" +
69
+ "backtrace 1</div>\n",
70
+ "|\t\t\t\t<div class=\"hq-log-exception-backtrace-line\">" +
71
+ "backtrace 2</div>\n",
72
+ "|\t\t\t</div>\n",
73
+ "|\t\t</div>\n",
74
+ "|\t</div>\n",
75
+ "|</div>\n",
76
+ ].join
77
+
78
+ end
79
+
80
+ it "diff" do
81
+
82
+ output_for(sample_diff).should == [
83
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
84
+ "|\t<div class=\"hq-log-head\">\n",
85
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
86
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
87
+ "|\t</div>\n",
88
+ "|\t<div class=\"hq-log-content\">\n",
89
+ "|\t\t<div class=\"hq-log-diff\">\n",
90
+ "|\t\t\t<div class=\"hq-log-diff-minus-minus-minus\">diff 1</div>\n",
91
+ "|\t\t\t<div class=\"hq-log-diff-plus-plus-plus\">diff 2</div>\n",
92
+ "|\t\t\t<div class=\"hq-log-diff-at-at\">diff 3</div>\n",
93
+ "|\t\t\t<div class=\"hq-log-diff-minus\">diff 4</div>\n",
94
+ "|\t\t\t<div class=\"hq-log-diff-plus\">diff 5</div>\n",
95
+ "|\t\t\t<div class=\"hq-log-diff-else\">diff 6</div>\n",
96
+ "|\t\t</div>\n",
97
+ "|\t</div>\n",
98
+ "|</div>\n",
99
+ ].join
100
+
101
+ end
102
+
103
+ it "command with output" do
104
+
105
+ output_for(sample_command_with_output).should == [
106
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
107
+ "|\t<div class=\"hq-log-head\">\n",
108
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
109
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
110
+ "|\t</div>\n",
111
+ "|\t<div class=\"hq-log-content\">\n",
112
+ "|\t\t<div class=\"hq-log-command-output\">\n",
113
+ "|\t\t\t<div class=\"hq-log-command-output-line\">output 1</div>\n",
114
+ "|\t\t\t<div class=\"hq-log-command-output-line\">output 2</div>\n",
115
+ "|\t\t</div>\n",
116
+ "|\t</div>\n",
117
+ "|</div>\n",
118
+ ].join
119
+
120
+ end
121
+
122
+ it "command without output" do
123
+
124
+ output_for(sample_command_without_output).should == [
125
+ "|<div class=\"hq-log-item hq-log-item-debug\">\n",
126
+ "|\t<div class=\"hq-log-head\">\n",
127
+ "|\t\t<div class=\"hq-log-hostname\">hostname</div>\n",
128
+ "|\t\t<div class=\"hq-log-text\">text</div>\n",
129
+ "|\t</div>\n",
130
+ "|</div>\n",
131
+ ].join
132
+
133
+ end
134
+
135
+ end
136
+
137
+ context "#valid_modes" do
138
+
139
+ it "returns [ :normal, :complete ]" do
140
+ subject.valid_modes.should == [ :normal, :complete ]
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,30 @@
1
+ require "hq/logger/multi-logger"
2
+
3
+ module HQ
4
+ class Logger
5
+
6
+ describe MultiLogger do
7
+
8
+ context "#output" do
9
+
10
+ it "outputs to all added loggers" do
11
+
12
+ logger_1 = double "logger 1"
13
+ logger_2 = double "logger 2"
14
+
15
+ subject.add_logger logger_1
16
+ subject.add_logger logger_2
17
+
18
+ logger_1.should_receive(:output).with(:a, :b, :c)
19
+ logger_2.should_receive(:output).with(:a, :b, :c)
20
+
21
+ subject.output :a, :b, :c
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ require "hq/logger/raw-logger"
2
+ require "hq/logger/logger-examples"
3
+
4
+ module HQ
5
+ class Logger
6
+
7
+ describe RawLogger do
8
+
9
+ include_examples "a logger"
10
+
11
+ context "#output" do
12
+
13
+ it "outputs JSON" do
14
+
15
+ json = output_for(sample_log_with_content)
16
+
17
+ MultiJson.load(json).should == {
18
+ "mode" => "normal",
19
+ "content" => [
20
+ {
21
+ "type" => "log",
22
+ "level" => "debug",
23
+ "hostname" => "hostname",
24
+ "text" => "text",
25
+ "content" => [
26
+ "content 1",
27
+ "content 2",
28
+ ],
29
+ },
30
+ ],
31
+ }
32
+
33
+ end
34
+
35
+ end
36
+
37
+ context "#valid_modes" do
38
+
39
+ it "returns [ :normal, :partial, :complete ]" do
40
+ subject.valid_modes.should == [ :normal, :partial, :complete ]
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,95 @@
1
+ require "hq/logger/logger-examples"
2
+ require "hq/logger/text-logger"
3
+
4
+ module HQ
5
+ class Logger
6
+
7
+ describe TextLogger do
8
+
9
+ include_examples "a logger"
10
+
11
+ context "#output" do
12
+
13
+ it "log without content" do
14
+
15
+ output_for(sample_log_without_content).should == [
16
+ "hostname debug: |text\n"
17
+ ].join
18
+
19
+ end
20
+
21
+ it "log with content" do
22
+
23
+ output_for(sample_log_with_content).should == [
24
+ "hostname debug: |text\n",
25
+ "hostname debug: | content 1\n",
26
+ "hostname debug: | content 2\n",
27
+ ].join
28
+
29
+ end
30
+
31
+ it "exception" do
32
+
33
+ output_for(sample_exception).should == [
34
+ "hostname debug: |text\n",
35
+ "hostname debug: | message\n",
36
+ "hostname debug: | backtrace 1\n",
37
+ "hostname debug: | backtrace 2\n",
38
+ ].join
39
+
40
+ end
41
+
42
+ it "diff" do
43
+
44
+ output_for(sample_diff).should == [
45
+ "hostname debug: |text\n",
46
+ "hostname debug: | diff 1\n",
47
+ "hostname debug: | diff 2\n",
48
+ "hostname debug: | diff 3\n",
49
+ "hostname debug: | diff 4\n",
50
+ "hostname debug: | diff 5\n",
51
+ "hostname debug: | diff 6\n",
52
+ ].join
53
+
54
+ end
55
+
56
+ it "command with output" do
57
+
58
+ output_for(sample_command_with_output).should == [
59
+ "hostname debug: |text\n",
60
+ "hostname debug: | output 1\n",
61
+ "hostname debug: | output 2\n",
62
+ ].join
63
+
64
+ end
65
+
66
+ it "command without output" do
67
+
68
+ output_for(sample_command_without_output).should == [
69
+ "hostname debug: |text\n",
70
+ ].join
71
+
72
+ end
73
+
74
+ it "command-output" do
75
+
76
+ output_for(sample_command_output).should == [
77
+ "hostname debug: | text\n",
78
+ ].join
79
+
80
+ end
81
+
82
+ end
83
+
84
+ context "#valid_modes" do
85
+
86
+ it "returns [ :normal, :partial ]" do
87
+ subject.valid_modes.should == [ :normal, :partial ]
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,9 @@
1
+ require "hq/logger"
2
+
3
+ module HQ
4
+
5
+ describe Logger do
6
+ # TODO tests here please
7
+ end
8
+
9
+ end