ABCLogger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/ABCLogger.gemspec +23 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +296 -0
- data/Rakefile +2 -0
- data/lib/ABCLogger.rb +294 -0
- data/lib/ABCLogger/exceptions.rb +6 -0
- data/lib/ABCLogger/version.rb +10 -0
- data/lib/ABCSharedLogger.rb +34 -0
- data/test/ABCTester.rb +86 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f2f96b6e895c677e8e1c4e100f7ba0aa4daa393
|
4
|
+
data.tar.gz: 3b66442c2014efd60780fe7f2782391ff44e5dac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a8452e0aa27ea1d1a9b898cdfb884468a3095b3139768b2190e047da8c5aa311c522c4522dff8926da63471c99c14f4c9d4e3d945ad6346b595398f543a8107
|
7
|
+
data.tar.gz: 2b8455e3404d1413d6110e26968f7c40a9026a89b4c40d0799012e59f75f325a575258af2743c90f49e314b0f794b1ab538fd5ea481a22a2c10124352d5023ff
|
data/.gitignore
ADDED
data/ABCLogger.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ABCLogger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ABCLogger"
|
8
|
+
spec.version = ABCVersion::VERSION
|
9
|
+
spec.authors = ["Keith"]
|
10
|
+
spec.email = ["keithk23@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby class for logging to a logfile.}
|
12
|
+
spec.description = %q{Ruby class for logging to stdout, stderr, or a file.}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Keith Kowalski
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,296 @@
|
|
1
|
+
# ABCLogger
|
2
|
+
|
3
|
+
ABCLogger logs messages to stdout, stderr, or a file.<br>
|
4
|
+
ABCSharedLogger is the singleton implementaion.<br>
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```
|
11
|
+
gem 'ABCLogger'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install ABCLogger
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### ABCLogger Examples
|
25
|
+
require 'ABCLogger'
|
26
|
+
|
27
|
+
#### Example of STDOUT logging
|
28
|
+
|
29
|
+
##### Sample code
|
30
|
+
```
|
31
|
+
logger = ABCLogger.new
|
32
|
+
logger.set_level(:debug)
|
33
|
+
|
34
|
+
logger.log(:debug, 'init', 'Test message')
|
35
|
+
logger.log(:error, 'Test message')
|
36
|
+
logger.log('post init', 'Next message')
|
37
|
+
logger.log('Msg only ARG Test message')
|
38
|
+
|
39
|
+
age = 42
|
40
|
+
logger.log_formatted(:warning, 'init','Value was: %d', age)
|
41
|
+
logger.log_formatted(:info, 'Value was: %d', age)
|
42
|
+
logger.log_formatted('not_init','Value was: %d', age)
|
43
|
+
logger.log_formatted('Value was: %d', age)
|
44
|
+
|
45
|
+
name = 'Gunny'
|
46
|
+
job = 'Developer'
|
47
|
+
logger.log_formatted_vars(:debug, 'next','name: %s, age: %d, job: %s', name, age, job)
|
48
|
+
```
|
49
|
+
|
50
|
+
##### Sample output
|
51
|
+
|
52
|
+
STDOUT<br>
|
53
|
+
|
54
|
+
```
|
55
|
+
09/15/2014|02:36:00PM|ABCTester.rb|DEBUG|init|Test message
|
56
|
+
09/15/2014|02:36:00PM|ABCTester.rb|ERROR|init|Test message
|
57
|
+
09/15/2014|02:36:00PM|ABCTester.rb|ERROR|post init|Next message
|
58
|
+
09/15/2014|02:36:00PM|ABCTester.rb|ERROR|post init|Msg only ARG Test message
|
59
|
+
09/15/2014|02:36:00PM|ABCTester.rb|WARNING|init|Value was: 42
|
60
|
+
09/15/2014|02:36:00PM|ABCTester.rb|INFO|init|Value was: 42
|
61
|
+
09/15/2014|02:36:00PM|ABCTester.rb|INFO|not_init|Value was: 42
|
62
|
+
09/15/2014|02:36:00PM|ABCTester.rb|INFO|not_init|Value was: 42
|
63
|
+
09/15/2014|02:36:00PM|ABCTester.rb|DEBUG|next|name: Gunny, age: 42, job: Developer
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Example of file logging
|
67
|
+
|
68
|
+
##### Sample code #open
|
69
|
+
```
|
70
|
+
logger = ABCLogger.new
|
71
|
+
logger.set_level(:debug)
|
72
|
+
|
73
|
+
logger.open('ABCTester.log')
|
74
|
+
logger.log(:warning, 'file', 'Test message')
|
75
|
+
logger.log(:fatal, 'Test message')
|
76
|
+
logger.log('Msg only ARGS Test message')
|
77
|
+
logger.log_exception(:unknown, Exception.new('File exception'))
|
78
|
+
logger.close
|
79
|
+
```
|
80
|
+
|
81
|
+
##### Sample output
|
82
|
+
|
83
|
+
File: ABCTester.log<br>
|
84
|
+
|
85
|
+
```
|
86
|
+
09/15/2014|06:45:00PM|ABCTester.rb|WARNING|file|Test message
|
87
|
+
09/15/2014|06:45:00PM|ABCTester.rb|FATAL|file|Test message
|
88
|
+
09/15/2014|06:45:00PM|ABCTester.rb|FATAL|file|Msg only ARGS Test message
|
89
|
+
09/15/2014|06:45:00PM|ABCTester.rb|UNKNOWN|file|File exception
|
90
|
+
```
|
91
|
+
|
92
|
+
##### Sample code #open_with_date
|
93
|
+
```
|
94
|
+
logger = ABCLogger.new
|
95
|
+
logger.set_level(:debug)
|
96
|
+
|
97
|
+
logger.open_with_date
|
98
|
+
logger.log(:warning, 'file', 'Test message' )
|
99
|
+
logger.log(:fatal, 'Test message' )
|
100
|
+
logger.log('Msg only ARGS Test message' )
|
101
|
+
logger.log_exception(:unknown, Exception.new('File exception'))
|
102
|
+
logger.close
|
103
|
+
```
|
104
|
+
|
105
|
+
##### Sample output
|
106
|
+
|
107
|
+
File: ABCLogger_20140916.log<br>
|
108
|
+
|
109
|
+
```
|
110
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|file|Test message
|
111
|
+
09/16/2014|02:57:35PM|ABCTester.rb|FATAL|file|Test message
|
112
|
+
09/16/2014|02:57:35PM|ABCTester.rb|FATAL|file|Msg only ARGS Test message
|
113
|
+
09/16/2014|02:57:35PM|ABCTester.rb|UNKNOWN|file|File exception
|
114
|
+
```
|
115
|
+
|
116
|
+
#### Example of custom output
|
117
|
+
|
118
|
+
##### Sample code
|
119
|
+
```
|
120
|
+
logger = ABCLogger.new
|
121
|
+
logger.set_level(:debug)
|
122
|
+
|
123
|
+
logger.set_output(STDERR)
|
124
|
+
logger.log('Testing to STDERR')
|
125
|
+
logger.log(:info, 'Testing to STDERR')
|
126
|
+
|
127
|
+
logger.set_output(STDOUT)
|
128
|
+
logger.log('Testing to STDOUT')
|
129
|
+
logger.log(:warning, 'Testing to STDOUT')
|
130
|
+
|
131
|
+
file = File.open('testing.log', 'a+')
|
132
|
+
logger.set_output(file)
|
133
|
+
logger.log('Testing to file')
|
134
|
+
logger.log(:warning, 'Testing to file')
|
135
|
+
logger.set_output(STDOUT)
|
136
|
+
file.close
|
137
|
+
```
|
138
|
+
|
139
|
+
##### Sample output
|
140
|
+
|
141
|
+
STDERR<br>
|
142
|
+
|
143
|
+
```
|
144
|
+
09/16/2014|02:57:35PM|ABCTester.rb|DEBUG|next|Testing to STDERR
|
145
|
+
09/16/2014|02:57:35PM|ABCTester.rb|INFO|next|Testing to STDERR
|
146
|
+
```
|
147
|
+
|
148
|
+
STDOUT<br>
|
149
|
+
|
150
|
+
```
|
151
|
+
09/16/2014|02:57:35PM|ABCTester.rb|INFO|next|Testing to STDOUT
|
152
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|next|Testing to STDOUT
|
153
|
+
```
|
154
|
+
|
155
|
+
File: testing.log<br>
|
156
|
+
|
157
|
+
```
|
158
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|next|Testing to file
|
159
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|next|Testing to file
|
160
|
+
```
|
161
|
+
|
162
|
+
### ABCSharedLogger Examples
|
163
|
+
require 'ABCSharedLogger'
|
164
|
+
|
165
|
+
#### Example of STDOUT logging
|
166
|
+
|
167
|
+
##### Sample code
|
168
|
+
```
|
169
|
+
ABCSharedLogger.instance.set_program('ABCTester')
|
170
|
+
ABCSharedLogger.instance.set_level(:warning)
|
171
|
+
ABCSharedLogger.instance.log(:error, 'Testing singleton')
|
172
|
+
ABCSharedLogger.instance.log('Testing singleton again')
|
173
|
+
|
174
|
+
log = ABCSharedLogger.instance
|
175
|
+
log.log(:warning,'ReadData','New test 1')
|
176
|
+
log.log('New test 2')
|
177
|
+
log.log_formatted_vars(:fatal, 'ErrorHandler', 'RC = %d', errno)
|
178
|
+
```
|
179
|
+
|
180
|
+
##### Sample output
|
181
|
+
|
182
|
+
STDOUT<br>
|
183
|
+
|
184
|
+
```
|
185
|
+
09/16/2014|03:59:27PM|ABCTester|ERROR|Main|Testing singleton
|
186
|
+
09/16/2014|03:59:27PM|ABCTester|ERROR|Main|Testing singleton again
|
187
|
+
09/16/2014|03:59:27PM|ABCTester|WARNING|ReadData|New test 1
|
188
|
+
09/16/2014|03:59:27PM|ABCTester|WARNING|ReadData|New test 2
|
189
|
+
09/16/2014|03:59:27PM|ABCTester|FATAL|ErrorHandler|RC = 7
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
#### Example of file logging
|
194
|
+
|
195
|
+
##### Sample code #open
|
196
|
+
```
|
197
|
+
flog = ABCSharedLogger.instance
|
198
|
+
flog.set_level(:debug)
|
199
|
+
|
200
|
+
flog.open('testing.log')
|
201
|
+
flog.log(:warning, 'Testing again.......')
|
202
|
+
flog.log('Again.....')
|
203
|
+
flog.log_exception(Exception.new ('Test exception'))
|
204
|
+
flog.log(:fatal, 'This is weird')
|
205
|
+
flog.close
|
206
|
+
```
|
207
|
+
|
208
|
+
##### Sample output
|
209
|
+
|
210
|
+
File: ABCTester.log<br>
|
211
|
+
|
212
|
+
```
|
213
|
+
09/16/2014|04:05:50PM|ABCTester|WARNING|ErrorHandler|Testing again.......
|
214
|
+
09/16/2014|04:05:50PM|ABCTester|WARNING|ErrorHandler|Again.....
|
215
|
+
09/16/2014|04:05:50PM|ABCTester|WARNING|ErrorHandler|Test exception
|
216
|
+
09/16/2014|04:05:50PM|ABCTester|FATAL|ErrorHandler|This is weird
|
217
|
+
```
|
218
|
+
|
219
|
+
##### Sample code #open_with_date
|
220
|
+
```
|
221
|
+
dlog = ABCSharedLogger.instance
|
222
|
+
dlog.set_program('AppZ')
|
223
|
+
|
224
|
+
dlog.open_with_date(nil, 'ABCTst', 'log')
|
225
|
+
dlog.log :info, 'Testing again.......'
|
226
|
+
dlog.log 'Again.....'
|
227
|
+
dlog.log_exception ( Exception.new ('Test exception'))
|
228
|
+
dlog.log(:fatal, 'This is weird')
|
229
|
+
dlog.close
|
230
|
+
```
|
231
|
+
|
232
|
+
##### Sample output
|
233
|
+
|
234
|
+
File: ABCTst_20140916.log<br>
|
235
|
+
|
236
|
+
```
|
237
|
+
09/16/2014|04:07:10PM|AppZ|INFO|ErrorHandler|Testing again.......
|
238
|
+
09/16/2014|04:07:10PM|AppZ|INFO|ErrorHandler|Again.....
|
239
|
+
09/16/2014|04:07:10PM|AppZ|INFO|ErrorHandler|Test exception
|
240
|
+
09/16/2014|04:07:10PM|AppZ|FATAL|ErrorHandler|This is weird
|
241
|
+
```
|
242
|
+
|
243
|
+
#### Example of custom output
|
244
|
+
|
245
|
+
##### Sample code
|
246
|
+
```
|
247
|
+
logger = ABCSharedLogger.instance
|
248
|
+
logger.set_level(:debug)
|
249
|
+
|
250
|
+
logger.set_output(STDERR)
|
251
|
+
logger.log('Testing to STDERR')
|
252
|
+
logger.log(:info, 'Testing to STDERR')
|
253
|
+
|
254
|
+
logger.set_output(STDOUT)
|
255
|
+
logger.log('Testing to STDOUT')
|
256
|
+
logger.log(:warning, 'Testing to STDOUT')
|
257
|
+
|
258
|
+
file = File.open('testing.log', 'a+')
|
259
|
+
logger.set_output(file)
|
260
|
+
logger.log('Testing to file')
|
261
|
+
logger.log(:warning, 'Testing to file')
|
262
|
+
logger.set_output(STDOUT)
|
263
|
+
file.close
|
264
|
+
```
|
265
|
+
|
266
|
+
##### Sample output
|
267
|
+
|
268
|
+
STDERR<br>
|
269
|
+
|
270
|
+
```
|
271
|
+
09/16/2014|02:57:35PM|ABCTester.rb|DEBUG|next|Testing to STDERR
|
272
|
+
09/16/2014|02:57:35PM|ABCTester.rb|INFO|next|Testing to STDERR
|
273
|
+
```
|
274
|
+
|
275
|
+
STDOUT<br>
|
276
|
+
|
277
|
+
```
|
278
|
+
09/16/2014|02:57:35PM|ABCTester.rb|INFO|next|Testing to STDOUT
|
279
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|next|Testing to STDOUT
|
280
|
+
```
|
281
|
+
|
282
|
+
File: testing.log<br>
|
283
|
+
|
284
|
+
```
|
285
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|next|Testing to file
|
286
|
+
09/16/2014|02:57:35PM|ABCTester.rb|WARNING|next|Testing to file
|
287
|
+
```
|
288
|
+
|
289
|
+
|
290
|
+
## Contributing
|
291
|
+
|
292
|
+
1. Fork it ( https://github.com/keith23/ABCLogger/fork )
|
293
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
294
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
295
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
296
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/ABCLogger.rb
ADDED
@@ -0,0 +1,294 @@
|
|
1
|
+
require 'ABCLogger/version'
|
2
|
+
require 'ABCLogger/exceptions'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
# ABCLogger logs messages to stdout, stderr, or a file
|
6
|
+
class ABCLogger
|
7
|
+
|
8
|
+
#include Singleton
|
9
|
+
|
10
|
+
private
|
11
|
+
DEBUG_LOG = false
|
12
|
+
|
13
|
+
# Output file open flag
|
14
|
+
attr_reader :outfile_open
|
15
|
+
# Output file
|
16
|
+
attr_reader :outfile
|
17
|
+
# Saved file name
|
18
|
+
attr_reader :file_name
|
19
|
+
# Saved log level
|
20
|
+
attr_reader :level_save
|
21
|
+
|
22
|
+
# Log levels
|
23
|
+
attr_reader :levels
|
24
|
+
# Log level names
|
25
|
+
attr_reader :lnames
|
26
|
+
|
27
|
+
public
|
28
|
+
LEVELS = {:debug => 0, :info => 1, :warning => 2,
|
29
|
+
:error => 3, :fatal => 4, :unknown => 5}
|
30
|
+
|
31
|
+
# Logging level
|
32
|
+
# @return [symbol] current log level
|
33
|
+
attr_reader :level
|
34
|
+
# Logging enabled
|
35
|
+
# @return [boolean] logging enabled flag
|
36
|
+
attr_reader :enabled
|
37
|
+
# Logging location
|
38
|
+
# @return [string] log message location
|
39
|
+
attr_reader :location
|
40
|
+
# Program name
|
41
|
+
# @return [string] program name
|
42
|
+
attr_reader :program
|
43
|
+
|
44
|
+
# Set logging level
|
45
|
+
# @param [int] level
|
46
|
+
def set_level(level)
|
47
|
+
@level = level
|
48
|
+
end
|
49
|
+
|
50
|
+
# Turn logging on and off
|
51
|
+
# @param [boolean] flag
|
52
|
+
def set_enabled(flag)
|
53
|
+
@enabled = flag
|
54
|
+
end
|
55
|
+
|
56
|
+
# Set log message location
|
57
|
+
# @param [string] name
|
58
|
+
def set_location(name)
|
59
|
+
@location = name
|
60
|
+
end
|
61
|
+
|
62
|
+
# Set program name
|
63
|
+
# @param [string] name
|
64
|
+
def set_program(name)
|
65
|
+
@program = name
|
66
|
+
end
|
67
|
+
|
68
|
+
# open the logfile
|
69
|
+
# @param [string] filename
|
70
|
+
def open(filename = 'ABCLogger.log')
|
71
|
+
begin
|
72
|
+
@outfile = File.open(filename, 'a+')
|
73
|
+
@file_name = filename
|
74
|
+
@outfile_open = true
|
75
|
+
if DEBUG_LOG
|
76
|
+
puts "***ABCLogger:file: #{@file_name} was opened"
|
77
|
+
end
|
78
|
+
rescue SystemCallError => exc
|
79
|
+
handle_exception(exc)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# open the logfile
|
84
|
+
# @param [string] path nil for no path
|
85
|
+
# @param [string] prefix file name prefix
|
86
|
+
# @param [string] ext file name extension
|
87
|
+
def open_with_date(path = nil, prefix = 'ABCLogger', ext = 'log')
|
88
|
+
begin
|
89
|
+
d = DateTime.now
|
90
|
+
if path == nil
|
91
|
+
@file_name = prefix << '_' << d.strftime('%Y%m%d') << '.' << ext
|
92
|
+
else
|
93
|
+
@file_name = path << '/' << prefix << '_' << d.strftime('%Y%m%d') << '.' << ext
|
94
|
+
end
|
95
|
+
@outfile = File.open(@file_name, 'a+')
|
96
|
+
@outfile_open = true
|
97
|
+
if DEBUG_LOG
|
98
|
+
puts "***ABCLogger:file: #{@file_name} was opened"
|
99
|
+
end
|
100
|
+
rescue SystemCallError => exc
|
101
|
+
handle_exception(exc)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# close the logfile
|
106
|
+
def close
|
107
|
+
begin
|
108
|
+
@outfile.close
|
109
|
+
@outfile = STDOUT
|
110
|
+
@outfile_open = false
|
111
|
+
if DEBUG_LOG
|
112
|
+
puts "***ABCLogger:file: #{@file_name} was closed"
|
113
|
+
end
|
114
|
+
@file_name = nil
|
115
|
+
rescue SystemCallError => exc
|
116
|
+
handle_exception(exc)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Set output to STDOUT, STDERR, or a File
|
121
|
+
# @param output [object] output for log
|
122
|
+
def set_output(output = STDOUT)
|
123
|
+
if @outfile_open
|
124
|
+
close
|
125
|
+
end
|
126
|
+
@outfile = output
|
127
|
+
if DEBUG_LOG
|
128
|
+
puts "***ABCLogger:ouput was changed to: #{output_name}"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Log message with level
|
133
|
+
# @param level [string] logging level, optional
|
134
|
+
# @param loc [string] location string, optional
|
135
|
+
# @param msg [string] message string
|
136
|
+
def log(level = nil, loc = nil, msg)
|
137
|
+
|
138
|
+
if level == nil
|
139
|
+
level = @level_save
|
140
|
+
end
|
141
|
+
|
142
|
+
if level.is_a? Symbol
|
143
|
+
#puts 'level is symbol'
|
144
|
+
@level_save = level
|
145
|
+
end
|
146
|
+
|
147
|
+
if level.is_a? String
|
148
|
+
#puts 'level is string'
|
149
|
+
if loc == nil
|
150
|
+
loc = level
|
151
|
+
level = @level_save
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
write_msg(@outfile, level, loc, msg)
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
# Log formatted message - single arg or array of args
|
160
|
+
# @param level [int] log level, optional
|
161
|
+
# @param loc [string] message location, optional
|
162
|
+
# @param msg [string] message format string
|
163
|
+
# @param args [arg or array of args]
|
164
|
+
def log_formatted(level = nil, loc = nil, msg, args)
|
165
|
+
formatted = sprintf(msg, *args)
|
166
|
+
log(level, loc, formatted)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Log formatted message - variable number of args
|
170
|
+
# @param level [int] log level
|
171
|
+
# @param loc [string] message location
|
172
|
+
# @param msg [string] message format string
|
173
|
+
# @param args [arg1 arg2 ... argN] variable number of args
|
174
|
+
def log_formatted_vars(level, loc, msg, *args)
|
175
|
+
formatted = sprintf(msg, *args)
|
176
|
+
log(level, loc, formatted)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Log exception to logfile
|
180
|
+
# @param level [int] log level, optional
|
181
|
+
# @param loc [string] message location, optional
|
182
|
+
# @param exc [exception] exception to log
|
183
|
+
def log_exception(level = nil, loc = nil, exc)
|
184
|
+
log(level, loc, exc.message)
|
185
|
+
end
|
186
|
+
|
187
|
+
# Initialize ABCLogger
|
188
|
+
# @return [ABCLogger]
|
189
|
+
def initialize
|
190
|
+
@levels = {:debug => 0, :info => 1, :warning => 2,
|
191
|
+
:error => 3, :fatal => 4, :unknown => 5}
|
192
|
+
@lnames = %w(DEBUG INFO WARNING ERROR FATAL UNKNOWN)
|
193
|
+
@level = :debug
|
194
|
+
@enabled = true
|
195
|
+
@location = 'Main'
|
196
|
+
@outfile_open = false
|
197
|
+
@outfile = STDOUT
|
198
|
+
@file_name = nil
|
199
|
+
@level_save = :debug
|
200
|
+
@program = $0
|
201
|
+
end
|
202
|
+
|
203
|
+
private
|
204
|
+
|
205
|
+
def write_msg(output, level = :debug, loc = nil, msg)
|
206
|
+
|
207
|
+
if loc == nil
|
208
|
+
loc = @location
|
209
|
+
else
|
210
|
+
@location = loc
|
211
|
+
end
|
212
|
+
|
213
|
+
#if DEBUG_LOG and 1 == 2
|
214
|
+
# puts @levels[@level]
|
215
|
+
# puts @levels[level]
|
216
|
+
#end
|
217
|
+
|
218
|
+
case level
|
219
|
+
when Integer
|
220
|
+
# puts "INTEGER level_name(#{level})"
|
221
|
+
lvl_int = level
|
222
|
+
when Symbol
|
223
|
+
# puts "SYMBOL level_name(#{level})"
|
224
|
+
lvl_int = @levels[level]
|
225
|
+
when String
|
226
|
+
# puts "STRING level_name(#{level})"
|
227
|
+
lvl_int = 0
|
228
|
+
else
|
229
|
+
# puts "NOT SURE level_name(#{level})"
|
230
|
+
lvl_int = 0
|
231
|
+
end
|
232
|
+
|
233
|
+
@level_save = lvl_int
|
234
|
+
|
235
|
+
if @enabled
|
236
|
+
if @levels[@level] <= lvl_int # @levels[level]
|
237
|
+
output.puts "#{date_str}|#{@program}|#{level_name(level)}|#{loc}|#{msg}"
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
def date_str
|
244
|
+
d = DateTime.now
|
245
|
+
d.strftime('%m/%d/%Y|%I:%M:%S%p')
|
246
|
+
end
|
247
|
+
|
248
|
+
def level_name(sym)
|
249
|
+
#puts "level_name(#{sym})"
|
250
|
+
case sym
|
251
|
+
when Integer
|
252
|
+
#puts "INTEGER level_name(#{sym})"
|
253
|
+
return @lnames[sym]
|
254
|
+
when Symbol
|
255
|
+
#puts "SYMBOL level_name(#{sym})"
|
256
|
+
when String
|
257
|
+
#puts "STRING level_name(#{sym})"
|
258
|
+
else
|
259
|
+
#puts "NOT SURE level_name(#{sym})"
|
260
|
+
end
|
261
|
+
#if sym == nil
|
262
|
+
# sym = 0
|
263
|
+
#end
|
264
|
+
lvl = @levels[sym]
|
265
|
+
@lnames[lvl]
|
266
|
+
end
|
267
|
+
|
268
|
+
# make private
|
269
|
+
def output_name
|
270
|
+
if @outfile == STDOUT
|
271
|
+
return 'STDOUT'
|
272
|
+
end
|
273
|
+
if @outfile == STDERR
|
274
|
+
return 'STDERR'
|
275
|
+
end
|
276
|
+
@file_name
|
277
|
+
end
|
278
|
+
|
279
|
+
# log exception and raise
|
280
|
+
def handle_exception(exc)
|
281
|
+
self.log_exception(:fatal, '***ABCLogger***', exc)
|
282
|
+
raise ABCLoggerException.new(exc.message)
|
283
|
+
end
|
284
|
+
|
285
|
+
#def test_func(sym)
|
286
|
+
# lvl = @levels[sym]
|
287
|
+
# puts "#{lvl}|#{@lnames[lvl]}"
|
288
|
+
#end
|
289
|
+
|
290
|
+
end
|
291
|
+
|
292
|
+
#module ABCLogger
|
293
|
+
# Your code goes here...
|
294
|
+
#end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# ABCSharedLogger
|
2
|
+
require 'ABCLogger'
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
# Singleton version of ABCLogger
|
6
|
+
class ABCSharedLogger < ABCLogger
|
7
|
+
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
# Instance method for ABCSharedLogger
|
11
|
+
# @return [ABCSharedLogger] instance
|
12
|
+
def self.instance
|
13
|
+
|
14
|
+
if DEBUG_LOG
|
15
|
+
puts '***ABCSharedLogger::instance'
|
16
|
+
end
|
17
|
+
|
18
|
+
@@instance ||= new
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
|
26
|
+
if DEBUG_LOG
|
27
|
+
puts '***ABCSharedLogger::initialize'
|
28
|
+
end
|
29
|
+
|
30
|
+
super
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/test/ABCTester.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Test program for ABCLogger a ruby logfile class
|
2
|
+
require 'ABCLogger'
|
3
|
+
require 'ABCSharedLogger'
|
4
|
+
|
5
|
+
errno = 7
|
6
|
+
age = 42
|
7
|
+
name = 'Gunny'
|
8
|
+
job = 'Developer'
|
9
|
+
|
10
|
+
# ABCLogger STDOUT example
|
11
|
+
logger = ABCLogger.new
|
12
|
+
logger.set_level(:debug)
|
13
|
+
|
14
|
+
logger.log(:debug, 'init', 'Test message')
|
15
|
+
logger.log(:error, 'Test message' )
|
16
|
+
logger.log('post init', 'Next message')
|
17
|
+
logger.log('Msg only ARG Test message')
|
18
|
+
|
19
|
+
logger.log_formatted(:warning, 'init','Value was: %d', age)
|
20
|
+
logger.log_formatted(:info, 'Value was: %d', age)
|
21
|
+
logger.log_formatted('not_init','Value was: %d', age)
|
22
|
+
logger.log_formatted('Value was: %d', age)
|
23
|
+
|
24
|
+
logger.log_formatted_vars(:debug, 'next',
|
25
|
+
'name: %s, age: %d, job: %s', name, age, job)
|
26
|
+
|
27
|
+
|
28
|
+
# ABCLogger File example
|
29
|
+
#logger.open('ABCTester.log')
|
30
|
+
#logger.open_with_date('/home/testing', 'ABCLog', 'log')
|
31
|
+
logger.open_with_date
|
32
|
+
logger.log(:warning, 'file', 'Test message' )
|
33
|
+
logger.log(:fatal, 'Test message' )
|
34
|
+
logger.log('Msg only ARGS Test message' )
|
35
|
+
logger.log_exception(:unknown, Exception.new('File exception'))
|
36
|
+
logger.close
|
37
|
+
|
38
|
+
# ABCLogger Custom example
|
39
|
+
logger.set_output(STDERR)
|
40
|
+
logger.log('Testing to STDERR')
|
41
|
+
logger.log(:info, 'Testing to STDERR')
|
42
|
+
|
43
|
+
logger.set_output(STDOUT)
|
44
|
+
logger.log('Testing to STDOUT')
|
45
|
+
logger.log(:warning, 'Testing to STDOUT')
|
46
|
+
|
47
|
+
file = File.open('testing.log', 'a+')
|
48
|
+
logger.set_output(file)
|
49
|
+
logger.log('Testing to file')
|
50
|
+
logger.log(:warning, 'Testing to file')
|
51
|
+
logger.set_output(STDOUT)
|
52
|
+
file.close
|
53
|
+
|
54
|
+
|
55
|
+
# ABCSharedLogger example
|
56
|
+
ABCSharedLogger.instance.set_program('ABCTester')
|
57
|
+
ABCSharedLogger.instance.set_level(:warning)
|
58
|
+
ABCSharedLogger.instance.log(:error, 'Testing singleton')
|
59
|
+
ABCSharedLogger.instance.log('Testing singleton again')
|
60
|
+
|
61
|
+
log = ABCSharedLogger.instance
|
62
|
+
log.log(:warning,'ReadData','New test 1')
|
63
|
+
log.log('New test 2')
|
64
|
+
log.log_formatted_vars(:fatal, 'ErrorHandler', 'RC = %d', errno)
|
65
|
+
|
66
|
+
# ABCSharedLogger example
|
67
|
+
flog = ABCSharedLogger.instance
|
68
|
+
flog.set_level(:debug)
|
69
|
+
|
70
|
+
flog.open('testing.log')
|
71
|
+
flog.log(:warning, 'Testing again.......')
|
72
|
+
flog.log('Again.....')
|
73
|
+
flog.log_exception(Exception.new ('Test exception'))
|
74
|
+
flog.log(:fatal, 'This is weird')
|
75
|
+
flog.close
|
76
|
+
|
77
|
+
# ABCSharedLogger example
|
78
|
+
dlog = ABCSharedLogger.instance
|
79
|
+
dlog.set_program('AppZ')
|
80
|
+
|
81
|
+
dlog.open_with_date(nil, 'ABCTst', 'log')
|
82
|
+
dlog.log :info, 'Testing again.......'
|
83
|
+
dlog.log 'Again.....'
|
84
|
+
dlog.log_exception ( Exception.new ('Test exception'))
|
85
|
+
dlog.log(:fatal, 'This is weird')
|
86
|
+
dlog.close
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ABCLogger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Ruby class for logging to stdout, stderr, or a file.
|
42
|
+
email:
|
43
|
+
- keithk23@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- ABCLogger.gemspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/ABCLogger.rb
|
55
|
+
- lib/ABCLogger/exceptions.rb
|
56
|
+
- lib/ABCLogger/version.rb
|
57
|
+
- lib/ABCSharedLogger.rb
|
58
|
+
- test/ABCTester.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.14
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Ruby class for logging to a logfile.
|
83
|
+
test_files:
|
84
|
+
- test/ABCTester.rb
|
85
|
+
has_rdoc:
|