Class: HQ::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/logger.rb,
lib/hq/logger/io-logger.rb,
lib/hq/logger/raw-logger.rb,
lib/hq/logger/ansi-logger.rb,
lib/hq/logger/html-logger.rb,
lib/hq/logger/text-logger.rb,
lib/hq/logger/multi-logger.rb

Defined Under Namespace

Classes: AnsiLogger, HtmlLogger, IoLogger, MultiLogger, RawLogger, TextLogger

Constant Summary collapse

MESSAGE_TYPES =
[
	:trace,
	:timing,
	:debug,
	:detail,
	:notice,
	:warning,
	:error,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



16
17
18
19
20
21
22
23
# File 'lib/hq/logger.rb', line 16

def initialize

	require "hq/logger/multi-logger"

	@multi_logger =
		MultiLogger.new

end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



4
5
6
# File 'lib/hq/logger.rb', line 4

def hostname
  @hostname
end

Class Method Details

.level_includes(level_1, level_2) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/hq/logger.rb', line 159

def self.level_includes level_1, level_2

	index_1 =
		MESSAGE_TYPES.index(level_1.to_sym)

	index_2 =
		MESSAGE_TYPES.index(level_2.to_sym)

	return index_1 <= index_2

end

Instance Method Details

#add_auto(str) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hq/logger.rb', line 107

def add_auto str

	level, format, filename =
		str.split ":", 3

	add_target \
		filename ? File.open(filename, "a") : STDOUT,
		format || :ansi,
		level || hq_config["default-log-level"] || :detail

end

#add_logger(logger) ⇒ Object



155
156
157
# File 'lib/hq/logger.rb', line 155

def add_logger logger
	@multi_logger.add_logger logger
end

#add_target(out, format, level) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hq/logger.rb', line 119

def add_target out, format, level

	raise "Invalid log level #{level}" \
		unless MESSAGE_TYPES.include? level.to_sym

	logger =
		case format.to_sym

			when :ansi
				require "hq/logger/ansi-logger"
				AnsiLogger.new

			when :html
				require "hq/logger/html-logger"
				HtmlLogger.new

			when :raw
				require "hq/logger/raw-logger"
				RawLogger.new

			when :text
				require "hq/logger/text-logger"
				TextLogger.new

			else
				raise "Error"

		end

	logger.out = out
	logger.level = level

	@multi_logger.add_logger logger

end

#debug(text, *contents) ⇒ Object



45
46
47
# File 'lib/hq/logger.rb', line 45

def debug text, *contents
	message text, :debug, *contents
end

#detail(text, *contents) ⇒ Object



49
50
51
# File 'lib/hq/logger.rb', line 49

def detail text, *contents
	message text, :detail, *contents
end

#die(text, status = 1) ⇒ Object



102
103
104
105
# File 'lib/hq/logger.rb', line 102

def die text, status = 1
	error text
	exit status
end

#error(text, *contents) ⇒ Object



61
62
63
# File 'lib/hq/logger.rb', line 61

def error text, *contents
	message text, :error, *contents
end

#message(text, level, *content) ⇒ Object



25
26
27
# File 'lib/hq/logger.rb', line 25

def message text, level, *content
	message text, level, *content
end

#message_complete(text, level, *content) ⇒ Object



33
34
35
# File 'lib/hq/logger.rb', line 33

def message_complete text, level, *content
	message_complete text, level, options
end

#message_partial(text, level, *content) ⇒ Object



29
30
31
# File 'lib/hq/logger.rb', line 29

def message_partial text, level, *content
	message_partial text, level, options
end

#notice(text, *contents) ⇒ Object



53
54
55
# File 'lib/hq/logger.rb', line 53

def notice text, *contents
	message text, :notice, *contents
end

#output(content, mode = :normal) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/hq/logger.rb', line 171

def output content, mode = :normal

	raise "Must provide hostname" \
		unless content["hostname"]

	@multi_logger.output \
		content,
		{ mode: mode }

end

#time(text, level = :timing) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hq/logger.rb', line 65

def time text, level = :timing

	time_start =
		Time.now

	begin

		yield

	ensure

		time_end =
			Time.now

		timing_ms =
			((time_end - time_start) * 1000).to_i

		timing_str =
			case timing_ms
				when (0...1000)
					"%dms" % [ timing_ms ]
				when (1000...10000)
					"%.2fs" % [ timing_ms.to_f / 1000 ]
				when (10000...100000)
					"%.1fs" % [ timing_ms.to_f / 1000 ]
				else
					"%ds" % [ timing_ms / 1000 ]
			end

		message \
			"#{text} took #{timing_str}",
			level

	end

end

#timing(text, *contents) ⇒ Object



41
42
43
# File 'lib/hq/logger.rb', line 41

def timing text, *contents
	message text, :timing, *contents
end

#trace(text, *contents) ⇒ Object



37
38
39
# File 'lib/hq/logger.rb', line 37

def trace text, *contents
	message text, :trace, *contents
end

#warning(text, *contents) ⇒ Object



57
58
59
# File 'lib/hq/logger.rb', line 57

def warning text, *contents
	message text, :warning, *contents
end