rcommons 0.8.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/COPYING +202 -0
- data/Changes.rdoc +4 -0
- data/README.rdoc +21 -0
- data/Rakefile +277 -0
- data/doc/jamis.rb +589 -0
- data/lib/commons.rb +44 -0
- data/lib/commons/io/filename_utils.rb +54 -0
- data/lib/commons/lang/builder/to_s_builder.rb +124 -0
- data/lib/commons/lang/builder/to_s_style.rb +493 -0
- data/lib/commons/lang/class_loader.rb +77 -0
- data/lib/commons/lang/class_utils.rb +50 -0
- data/lib/commons/lang/object_utils.rb +61 -0
- data/lib/commons/lang/state_error.rb +41 -0
- data/lib/commons/lang/system_utils.rb +113 -0
- data/lib/commons/lang/time/date_utils.rb +47 -0
- data/lib/commons/lang/time/duration_format_utils.rb +279 -0
- data/lib/commons/lang/time/stop_watch.rb +207 -0
- data/lib/commons/logging/impl/log4r_logger.rb +159 -0
- data/lib/commons/logging/impl/log_factory_impl.rb +253 -0
- data/lib/commons/logging/impl/no_op_log.rb +59 -0
- data/lib/commons/logging/impl/simple_log.rb +251 -0
- data/lib/commons/logging/log.rb +106 -0
- data/lib/commons/logging/log_configuration_error.rb +40 -0
- data/lib/commons/logging/log_factory.rb +177 -0
- data/lib/commons/ruby/exception.rb +46 -0
- data/lib/commons/ruby/log4r.rb +34 -0
- data/lib/commons/ruby/log4r/logger.rb +116 -0
- data/lib/commons/ruby/string.rb +55 -0
- data/lib/commons/ruby/test/unit.rb +33 -0
- data/lib/commons/ruby/test/unit/assertions.rb +50 -0
- data/lib/commons/util/properties.rb +84 -0
- data/test/commons/io/filename_utils_test.rb +69 -0
- data/test/commons/lang/builder/to_s_builder_test.rb +101 -0
- data/test/commons/lang/system_utils_test.rb +75 -0
- data/test/commons/lang/time/duration_format_utils_test.rb +261 -0
- data/test/commons/lang/time/stop_watch_test.rb +232 -0
- data/test/commons/logging/impl/log4r_logger_test.rb +133 -0
- data/test/commons/logging/log_factory_test.rb +80 -0
- data/test/commons/ruby/string_test.rb +64 -0
- data/test/commons/util/properties_test.rb +92 -0
- data/test/log4r/logger_test.rb +92 -0
- metadata +123 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
# = No Operation Logger
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Ruby version 1.8
|
5
|
+
#
|
6
|
+
# == Authors
|
7
|
+
# * Yomei Komiya
|
8
|
+
#
|
9
|
+
# == Copyright
|
10
|
+
# 2008 the original author or authors.
|
11
|
+
#
|
12
|
+
# == License
|
13
|
+
# Apache License 2.0
|
14
|
+
#
|
15
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
+
# you may not use this file except in compliance with the License.
|
17
|
+
# You may obtain a copy of the License at
|
18
|
+
#
|
19
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
20
|
+
#
|
21
|
+
# Unless required by applicable law or agreed to in writing, software
|
22
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
+
# See the License for the specific language governing permissions and
|
25
|
+
# limitations under the License.
|
26
|
+
#++
|
27
|
+
# == Version
|
28
|
+
# SVN: $Id: no_op_log.rb 77 2008-10-12 12:07:49Z whitestar $
|
29
|
+
#
|
30
|
+
# == Since
|
31
|
+
# File available since Release 0.8.0
|
32
|
+
|
33
|
+
require 'commons/logging/log'
|
34
|
+
|
35
|
+
module Commons
|
36
|
+
module Logging
|
37
|
+
module Impl
|
38
|
+
|
39
|
+
class NoOpLog
|
40
|
+
include Commons::Logging::Log
|
41
|
+
|
42
|
+
def initialize(name = nil)
|
43
|
+
# do nothing.
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def log(level, message, exception = nil)
|
48
|
+
# do nothing.
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def enabled_for?(level)
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,251 @@
|
|
1
|
+
# = Simple Log
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Ruby version 1.8
|
5
|
+
#
|
6
|
+
# == Authors
|
7
|
+
# * Yomei Komiya
|
8
|
+
#
|
9
|
+
# == Copyright
|
10
|
+
# 2008 the original author or authors.
|
11
|
+
#
|
12
|
+
# == License
|
13
|
+
# Apache License 2.0
|
14
|
+
#
|
15
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
+
# you may not use this file except in compliance with the License.
|
17
|
+
# You may obtain a copy of the License at
|
18
|
+
#
|
19
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
20
|
+
#
|
21
|
+
# Unless required by applicable law or agreed to in writing, software
|
22
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
+
# See the License for the specific language governing permissions and
|
25
|
+
# limitations under the License.
|
26
|
+
#++
|
27
|
+
# == Version
|
28
|
+
# SVN: $Id: simple_log.rb 77 2008-10-12 12:07:49Z whitestar $
|
29
|
+
#
|
30
|
+
# == Since
|
31
|
+
# File available since Release 0.8.0
|
32
|
+
|
33
|
+
require 'commons/ruby/exception'
|
34
|
+
|
35
|
+
require 'commons/lang/system_utils'
|
36
|
+
require 'commons/logging/log'
|
37
|
+
require 'commons/logging/impl/no_op_log'
|
38
|
+
require 'commons/util/properties'
|
39
|
+
|
40
|
+
module Commons
|
41
|
+
module Logging
|
42
|
+
module Impl
|
43
|
+
|
44
|
+
class SimpleLog < NoOpLog
|
45
|
+
include Commons::Logging::Log
|
46
|
+
|
47
|
+
SYSTEM_PREFIX = 'commons.logging.simplelog.'
|
48
|
+
|
49
|
+
DEFAULT_DATE_TIME_FORMAT = '%Y/%m/%d %H:%M:%S %Z'
|
50
|
+
|
51
|
+
LOG_LEVEL_ALL = 0
|
52
|
+
|
53
|
+
LOG_LEVEL_TRACE = 1
|
54
|
+
|
55
|
+
LOG_LEVEL_DEBUG = 2
|
56
|
+
|
57
|
+
LOG_LEVEL_INFO = 3
|
58
|
+
|
59
|
+
LOG_LEVEL_WARN = 4
|
60
|
+
|
61
|
+
LOG_LEVEL_ERROR = 5
|
62
|
+
|
63
|
+
LOG_LEVEL_FATAL = 6
|
64
|
+
|
65
|
+
LOG_LEVEL_OFF = 7
|
66
|
+
|
67
|
+
@@simple_log_props = nil
|
68
|
+
|
69
|
+
@@show_log_name = false
|
70
|
+
|
71
|
+
@@show_short_name = true
|
72
|
+
|
73
|
+
@@show_date_time = false
|
74
|
+
|
75
|
+
@@date_time_format = DEFAULT_DATE_TIME_FORMAT
|
76
|
+
|
77
|
+
|
78
|
+
def self.get_string_property(name, default = nil)
|
79
|
+
prop = nil;
|
80
|
+
prop = ENV[name]
|
81
|
+
prop = prop == nil ? @@simple_log_props.get_property(name) : prop
|
82
|
+
return prop == nil ? default : prop
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def self.get_boolean_property(name, default)
|
87
|
+
prop = get_string_property(name)
|
88
|
+
prop.upcase! unless prop == nil
|
89
|
+
return prop == nil ? default : prop == 'TRUE'
|
90
|
+
end
|
91
|
+
class << self
|
92
|
+
private :get_boolean_property
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
def self.static_initialize
|
97
|
+
@@simple_log_props = Commons::Util::Properties.new
|
98
|
+
|
99
|
+
# Add props from the resource simplelog.properties
|
100
|
+
props_path = Commons::Lang::ClassLoader.get_resource('simplelog.properties')
|
101
|
+
File.open(props_path, 'r') {|file|
|
102
|
+
@@simple_log_props.load(file)
|
103
|
+
}
|
104
|
+
|
105
|
+
@@show_log_name = get_boolean_property(
|
106
|
+
SYSTEM_PREFIX + 'show_log_name', @@show_log_name)
|
107
|
+
@@show_short_name = get_boolean_property(
|
108
|
+
SYSTEM_PREFIX + 'show_short_name', @@show_short_name)
|
109
|
+
@@show_date_time = get_boolean_property(
|
110
|
+
SYSTEM_PREFIX + 'show_date_time', @@show_date_time)
|
111
|
+
end
|
112
|
+
class << self
|
113
|
+
protected :static_initialize
|
114
|
+
end
|
115
|
+
self.static_initialize
|
116
|
+
|
117
|
+
|
118
|
+
attr_accessor :stderr, :log_name, :current_log_level
|
119
|
+
|
120
|
+
protected :stderr, :log_name, :current_log_level
|
121
|
+
|
122
|
+
|
123
|
+
def initialize(name = nil)
|
124
|
+
super(name)
|
125
|
+
|
126
|
+
@stderr = false
|
127
|
+
|
128
|
+
@log_name = name
|
129
|
+
|
130
|
+
@current_log_level
|
131
|
+
|
132
|
+
@short_log_name = nil
|
133
|
+
|
134
|
+
# Set log level from properties
|
135
|
+
lvl = self.class.get_string_property(SYSTEM_PREFIX + 'log.' + @log_name)
|
136
|
+
i = name == nil ? -1 : name.rindex('::')
|
137
|
+
while lvl == nil && i > -1
|
138
|
+
name = name[0 ... i]
|
139
|
+
lvl = self.class.get_string_property(SYSTEM_PREFIX + 'log.' + name)
|
140
|
+
i = name.rindex('::')
|
141
|
+
end
|
142
|
+
|
143
|
+
if lvl == nil
|
144
|
+
lvl = self.class.get_string_property(SYSTEM_PREFIX + 'default_log')
|
145
|
+
end
|
146
|
+
|
147
|
+
lvl = lvl.upcase
|
148
|
+
case lvl
|
149
|
+
when 'ALL' then set_level(LOG_LEVEL_ALL)
|
150
|
+
when 'TRACE' then set_level(LOG_LEVEL_TRACE)
|
151
|
+
when 'DEBUG' then set_level(LOG_LEVEL_DEBUG)
|
152
|
+
when 'INFO' then set_level(LOG_LEVEL_INFO)
|
153
|
+
when 'WARN' then set_level(LOG_LEVEL_WARN)
|
154
|
+
when 'ERROR' then set_level(LOG_LEVEL_ERROR)
|
155
|
+
when 'FATAL' then set_level(LOG_LEVEL_FATAL)
|
156
|
+
when 'OFF' then set_level(LOG_LEVEL_OFF)
|
157
|
+
else set_level(LOG_LEVEL_INFO)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
def set_level(current_log_level)
|
163
|
+
@current_log_level = current_log_level
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
def get_level
|
168
|
+
return @current_log_level
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
def log(level, message, exception = nil)
|
173
|
+
if message == nil
|
174
|
+
message = '<null>'
|
175
|
+
end
|
176
|
+
|
177
|
+
buf = ''
|
178
|
+
|
179
|
+
if @@show_date_time
|
180
|
+
now = Time.now
|
181
|
+
buf.concat(
|
182
|
+
now.strftime(@@date_time_format) + ' +' + now.usec.to_s)
|
183
|
+
buf.concat(' ')
|
184
|
+
end
|
185
|
+
|
186
|
+
case level
|
187
|
+
when TRACE then buf.concat('[TRACE] ')
|
188
|
+
when DEBUG then buf.concat('[DEBUG] ')
|
189
|
+
when INFO then buf.concat('[INFO] ')
|
190
|
+
when WARN then buf.concat('[WARN] ')
|
191
|
+
when ERROR then buf.concat('[ERROR] ')
|
192
|
+
when FATAL then buf.concat('[FATAL] ')
|
193
|
+
end
|
194
|
+
|
195
|
+
if @@show_short_name
|
196
|
+
if @short_log_name == nil
|
197
|
+
# Cut all but the last component of the name
|
198
|
+
delim_rindex = @log_name.rindex('::')
|
199
|
+
if delim_rindex != nil
|
200
|
+
@short_log_name = @log_name[delim_rindex + 2 .. -1]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
buf.concat(@short_log_name).concat(' - ')
|
204
|
+
elsif @@show_log_name
|
205
|
+
buf.concat(@log_name).concat(' - ')
|
206
|
+
end
|
207
|
+
|
208
|
+
buf.concat(message.to_s)
|
209
|
+
|
210
|
+
# Append stack trace if not null
|
211
|
+
if exception != nil
|
212
|
+
buf.concat(
|
213
|
+
' <' + exception.class.name + ': ' + exception.message.to_s + '; ' \
|
214
|
+
+ exception.stack_trace_string + '> ')
|
215
|
+
end
|
216
|
+
|
217
|
+
write(buf);
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
def write(buffer)
|
222
|
+
$stderr.write(buffer.to_s + Commons::Lang::SystemUtils.line_separator)
|
223
|
+
end
|
224
|
+
protected :write
|
225
|
+
|
226
|
+
|
227
|
+
def enabled_for?(level)
|
228
|
+
case level
|
229
|
+
when TRACE
|
230
|
+
level = LOG_LEVEL_TRACE
|
231
|
+
when DEBUG
|
232
|
+
level = LOG_LEVEL_DEBUG
|
233
|
+
when INFO
|
234
|
+
level = LOG_LEVEL_INFO
|
235
|
+
when WARN
|
236
|
+
level = LOG_LEVEL_WARN
|
237
|
+
when ERROR
|
238
|
+
level = LOG_LEVEL_ERROR
|
239
|
+
when FATAL
|
240
|
+
level = LOG_LEVEL_FATAL
|
241
|
+
else
|
242
|
+
level = LOG_LEVEL_OFF
|
243
|
+
end
|
244
|
+
|
245
|
+
return (level >= @current_log_level)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# = Log
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Ruby version 1.8
|
5
|
+
#
|
6
|
+
# == Authors
|
7
|
+
# * Yomei Komiya
|
8
|
+
#
|
9
|
+
# == Copyright
|
10
|
+
# 2008 the original author or authors.
|
11
|
+
#
|
12
|
+
# == License
|
13
|
+
# Apache License 2.0
|
14
|
+
#
|
15
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
+
# you may not use this file except in compliance with the License.
|
17
|
+
# You may obtain a copy of the License at
|
18
|
+
#
|
19
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
20
|
+
#
|
21
|
+
# Unless required by applicable law or agreed to in writing, software
|
22
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
+
# See the License for the specific language governing permissions and
|
25
|
+
# limitations under the License.
|
26
|
+
#++
|
27
|
+
# == Version
|
28
|
+
# SVN: $Id: log.rb 77 2008-10-12 12:07:49Z whitestar $
|
29
|
+
#
|
30
|
+
# == Since
|
31
|
+
# File available since Release 0.8.0
|
32
|
+
|
33
|
+
module Commons
|
34
|
+
module Logging
|
35
|
+
|
36
|
+
module Log
|
37
|
+
TRACE = :TRACE
|
38
|
+
DEBUG = :DEBUG
|
39
|
+
INFO = :INFO
|
40
|
+
WARN = :WARN
|
41
|
+
ERROR = :ERROR
|
42
|
+
FATAL = :FATAL
|
43
|
+
|
44
|
+
|
45
|
+
def trace(message, exception = nil)
|
46
|
+
log(TRACE, message, exception)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def debug(message, exception = nil)
|
51
|
+
log(DEBUG, message, exception)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def info(message, exception = nil)
|
56
|
+
log(INFO, message, exception)
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def warn(message, exception = nil)
|
61
|
+
log(WARN, message, exception)
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
def error(message, exception = nil)
|
66
|
+
log(ERROR, message, exception)
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
def fatal(message, exception = nil)
|
71
|
+
log(FATAL, message, exception)
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def trace?
|
76
|
+
return enabled_for?(TRACE)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def debug?
|
81
|
+
return enabled_for?(DEBUG)
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
def info?
|
86
|
+
return enabled_for?(INFO)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def warn?
|
91
|
+
return enabled_for?(WARN)
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def error?
|
96
|
+
return enabled_for?(ERROR)
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def fatal?
|
101
|
+
return enabled_for?(FATAL)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# = Log Configuration Error
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Ruby version 1.8
|
5
|
+
#
|
6
|
+
# == Authors
|
7
|
+
# * Yomei Komiya
|
8
|
+
#
|
9
|
+
# == Copyright
|
10
|
+
# 2008 the original author or authors.
|
11
|
+
#
|
12
|
+
# == License
|
13
|
+
# Apache License 2.0
|
14
|
+
#
|
15
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
+
# you may not use this file except in compliance with the License.
|
17
|
+
# You may obtain a copy of the License at
|
18
|
+
#
|
19
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
20
|
+
#
|
21
|
+
# Unless required by applicable law or agreed to in writing, software
|
22
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
+
# See the License for the specific language governing permissions and
|
25
|
+
# limitations under the License.
|
26
|
+
#++
|
27
|
+
# == Version
|
28
|
+
# SVN: $Id: log_configuration_error.rb 77 2008-10-12 12:07:49Z whitestar $
|
29
|
+
#
|
30
|
+
# == Since
|
31
|
+
# File available since Release 0.8.0
|
32
|
+
|
33
|
+
module Commons
|
34
|
+
module Logging
|
35
|
+
|
36
|
+
class LogConfigurationError < RuntimeError
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|