LogSimple 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/INSTALL +23 -0
- data/LICENSE +340 -0
- data/README +168 -0
- data/lib/logsimple.rb +254 -0
- data/test/unit/test_behaviours.rb +242 -0
- data/test/unit/test_logsimple.rb +152 -0
- metadata +58 -0
data/CHANGELOG
ADDED
data/INSTALL
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
= 1. RubyGems, network install
|
3
|
+
|
4
|
+
If you have RubyGems (http://www.rubygems.org/) and an access to the internet, you should be able
|
5
|
+
to install LogSimple directly from the internet. Simply run :
|
6
|
+
|
7
|
+
gem install LogSimple
|
8
|
+
|
9
|
+
= 2. Rake + RubyGems, local install
|
10
|
+
|
11
|
+
If you have Rake (http://rake.rubyforge.org/) and RubyGems installed, cd in the base directory of
|
12
|
+
LogSimple (where this file is located) and run :
|
13
|
+
rake gem
|
14
|
+
gem install pkg/LogSimple.<version number>.gem
|
15
|
+
|
16
|
+
You can uninstall at any point by doing :
|
17
|
+
gem uninstall LogSimple
|
18
|
+
|
19
|
+
= 3. Manual install, local install
|
20
|
+
|
21
|
+
You can just copy the LogSimple directory to the place of your choice - and make sure 'lib' is in
|
22
|
+
your include path before using it.
|
23
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,340 @@
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
2
|
+
Version 2, June 1991
|
3
|
+
|
4
|
+
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
5
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
7
|
+
of this license document, but changing it is not allowed.
|
8
|
+
|
9
|
+
Preamble
|
10
|
+
|
11
|
+
The licenses for most software are designed to take away your
|
12
|
+
freedom to share and change it. By contrast, the GNU General Public
|
13
|
+
License is intended to guarantee your freedom to share and change free
|
14
|
+
software--to make sure the software is free for all its users. This
|
15
|
+
General Public License applies to most of the Free Software
|
16
|
+
Foundation's software and to any other program whose authors commit to
|
17
|
+
using it. (Some other Free Software Foundation software is covered by
|
18
|
+
the GNU Lesser General Public License instead.) You can apply it to
|
19
|
+
your programs, too.
|
20
|
+
|
21
|
+
When we speak of free software, we are referring to freedom, not
|
22
|
+
price. Our General Public Licenses are designed to make sure that you
|
23
|
+
have the freedom to distribute copies of free software (and charge for
|
24
|
+
this service if you wish), that you receive source code or can get it
|
25
|
+
if you want it, that you can change the software or use pieces of it
|
26
|
+
in new free programs; and that you know you can do these things.
|
27
|
+
|
28
|
+
To protect your rights, we need to make restrictions that forbid
|
29
|
+
anyone to deny you these rights or to ask you to surrender the rights.
|
30
|
+
These restrictions translate to certain responsibilities for you if you
|
31
|
+
distribute copies of the software, or if you modify it.
|
32
|
+
|
33
|
+
For example, if you distribute copies of such a program, whether
|
34
|
+
gratis or for a fee, you must give the recipients all the rights that
|
35
|
+
you have. You must make sure that they, too, receive or can get the
|
36
|
+
source code. And you must show them these terms so they know their
|
37
|
+
rights.
|
38
|
+
|
39
|
+
We protect your rights with two steps: (1) copyright the software, and
|
40
|
+
(2) offer you this license which gives you legal permission to copy,
|
41
|
+
distribute and/or modify the software.
|
42
|
+
|
43
|
+
Also, for each author's protection and ours, we want to make certain
|
44
|
+
that everyone understands that there is no warranty for this free
|
45
|
+
software. If the software is modified by someone else and passed on, we
|
46
|
+
want its recipients to know that what they have is not the original, so
|
47
|
+
that any problems introduced by others will not reflect on the original
|
48
|
+
authors' reputations.
|
49
|
+
|
50
|
+
Finally, any free program is threatened constantly by software
|
51
|
+
patents. We wish to avoid the danger that redistributors of a free
|
52
|
+
program will individually obtain patent licenses, in effect making the
|
53
|
+
program proprietary. To prevent this, we have made it clear that any
|
54
|
+
patent must be licensed for everyone's free use or not licensed at all.
|
55
|
+
|
56
|
+
The precise terms and conditions for copying, distribution and
|
57
|
+
modification follow.
|
58
|
+
|
59
|
+
GNU GENERAL PUBLIC LICENSE
|
60
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
61
|
+
|
62
|
+
0. This License applies to any program or other work which contains
|
63
|
+
a notice placed by the copyright holder saying it may be distributed
|
64
|
+
under the terms of this General Public License. The "Program", below,
|
65
|
+
refers to any such program or work, and a "work based on the Program"
|
66
|
+
means either the Program or any derivative work under copyright law:
|
67
|
+
that is to say, a work containing the Program or a portion of it,
|
68
|
+
either verbatim or with modifications and/or translated into another
|
69
|
+
language. (Hereinafter, translation is included without limitation in
|
70
|
+
the term "modification".) Each licensee is addressed as "you".
|
71
|
+
|
72
|
+
Activities other than copying, distribution and modification are not
|
73
|
+
covered by this License; they are outside its scope. The act of
|
74
|
+
running the Program is not restricted, and the output from the Program
|
75
|
+
is covered only if its contents constitute a work based on the
|
76
|
+
Program (independent of having been made by running the Program).
|
77
|
+
Whether that is true depends on what the Program does.
|
78
|
+
|
79
|
+
1. You may copy and distribute verbatim copies of the Program's
|
80
|
+
source code as you receive it, in any medium, provided that you
|
81
|
+
conspicuously and appropriately publish on each copy an appropriate
|
82
|
+
copyright notice and disclaimer of warranty; keep intact all the
|
83
|
+
notices that refer to this License and to the absence of any warranty;
|
84
|
+
and give any other recipients of the Program a copy of this License
|
85
|
+
along with the Program.
|
86
|
+
|
87
|
+
You may charge a fee for the physical act of transferring a copy, and
|
88
|
+
you may at your option offer warranty protection in exchange for a fee.
|
89
|
+
|
90
|
+
2. You may modify your copy or copies of the Program or any portion
|
91
|
+
of it, thus forming a work based on the Program, and copy and
|
92
|
+
distribute such modifications or work under the terms of Section 1
|
93
|
+
above, provided that you also meet all of these conditions:
|
94
|
+
|
95
|
+
a) You must cause the modified files to carry prominent notices
|
96
|
+
stating that you changed the files and the date of any change.
|
97
|
+
|
98
|
+
b) You must cause any work that you distribute or publish, that in
|
99
|
+
whole or in part contains or is derived from the Program or any
|
100
|
+
part thereof, to be licensed as a whole at no charge to all third
|
101
|
+
parties under the terms of this License.
|
102
|
+
|
103
|
+
c) If the modified program normally reads commands interactively
|
104
|
+
when run, you must cause it, when started running for such
|
105
|
+
interactive use in the most ordinary way, to print or display an
|
106
|
+
announcement including an appropriate copyright notice and a
|
107
|
+
notice that there is no warranty (or else, saying that you provide
|
108
|
+
a warranty) and that users may redistribute the program under
|
109
|
+
these conditions, and telling the user how to view a copy of this
|
110
|
+
License. (Exception: if the Program itself is interactive but
|
111
|
+
does not normally print such an announcement, your work based on
|
112
|
+
the Program is not required to print an announcement.)
|
113
|
+
|
114
|
+
These requirements apply to the modified work as a whole. If
|
115
|
+
identifiable sections of that work are not derived from the Program,
|
116
|
+
and can be reasonably considered independent and separate works in
|
117
|
+
themselves, then this License, and its terms, do not apply to those
|
118
|
+
sections when you distribute them as separate works. But when you
|
119
|
+
distribute the same sections as part of a whole which is a work based
|
120
|
+
on the Program, the distribution of the whole must be on the terms of
|
121
|
+
this License, whose permissions for other licensees extend to the
|
122
|
+
entire whole, and thus to each and every part regardless of who wrote it.
|
123
|
+
|
124
|
+
Thus, it is not the intent of this section to claim rights or contest
|
125
|
+
your rights to work written entirely by you; rather, the intent is to
|
126
|
+
exercise the right to control the distribution of derivative or
|
127
|
+
collective works based on the Program.
|
128
|
+
|
129
|
+
In addition, mere aggregation of another work not based on the Program
|
130
|
+
with the Program (or with a work based on the Program) on a volume of
|
131
|
+
a storage or distribution medium does not bring the other work under
|
132
|
+
the scope of this License.
|
133
|
+
|
134
|
+
3. You may copy and distribute the Program (or a work based on it,
|
135
|
+
under Section 2) in object code or executable form under the terms of
|
136
|
+
Sections 1 and 2 above provided that you also do one of the following:
|
137
|
+
|
138
|
+
a) Accompany it with the complete corresponding machine-readable
|
139
|
+
source code, which must be distributed under the terms of Sections
|
140
|
+
1 and 2 above on a medium customarily used for software interchange; or,
|
141
|
+
|
142
|
+
b) Accompany it with a written offer, valid for at least three
|
143
|
+
years, to give any third party, for a charge no more than your
|
144
|
+
cost of physically performing source distribution, a complete
|
145
|
+
machine-readable copy of the corresponding source code, to be
|
146
|
+
distributed under the terms of Sections 1 and 2 above on a medium
|
147
|
+
customarily used for software interchange; or,
|
148
|
+
|
149
|
+
c) Accompany it with the information you received as to the offer
|
150
|
+
to distribute corresponding source code. (This alternative is
|
151
|
+
allowed only for noncommercial distribution and only if you
|
152
|
+
received the program in object code or executable form with such
|
153
|
+
an offer, in accord with Subsection b above.)
|
154
|
+
|
155
|
+
The source code for a work means the preferred form of the work for
|
156
|
+
making modifications to it. For an executable work, complete source
|
157
|
+
code means all the source code for all modules it contains, plus any
|
158
|
+
associated interface definition files, plus the scripts used to
|
159
|
+
control compilation and installation of the executable. However, as a
|
160
|
+
special exception, the source code distributed need not include
|
161
|
+
anything that is normally distributed (in either source or binary
|
162
|
+
form) with the major components (compiler, kernel, and so on) of the
|
163
|
+
operating system on which the executable runs, unless that component
|
164
|
+
itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering
|
167
|
+
access to copy from a designated place, then offering equivalent
|
168
|
+
access to copy the source code from the same place counts as
|
169
|
+
distribution of the source code, even though third parties are not
|
170
|
+
compelled to copy the source along with the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program
|
173
|
+
except as expressly provided under this License. Any attempt
|
174
|
+
otherwise to copy, modify, sublicense or distribute the Program is
|
175
|
+
void, and will automatically terminate your rights under this License.
|
176
|
+
However, parties who have received copies, or rights, from you under
|
177
|
+
this License will not have their licenses terminated so long as such
|
178
|
+
parties remain in full compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not
|
181
|
+
signed it. However, nothing else grants you permission to modify or
|
182
|
+
distribute the Program or its derivative works. These actions are
|
183
|
+
prohibited by law if you do not accept this License. Therefore, by
|
184
|
+
modifying or distributing the Program (or any work based on the
|
185
|
+
Program), you indicate your acceptance of this License to do so, and
|
186
|
+
all its terms and conditions for copying, distributing or modifying
|
187
|
+
the Program or works based on it.
|
188
|
+
|
189
|
+
6. Each time you redistribute the Program (or any work based on the
|
190
|
+
Program), the recipient automatically receives a license from the
|
191
|
+
original licensor to copy, distribute or modify the Program subject to
|
192
|
+
these terms and conditions. You may not impose any further
|
193
|
+
restrictions on the recipients' exercise of the rights granted herein.
|
194
|
+
You are not responsible for enforcing compliance by third parties to
|
195
|
+
this License.
|
196
|
+
|
197
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
198
|
+
infringement or for any other reason (not limited to patent issues),
|
199
|
+
conditions are imposed on you (whether by court order, agreement or
|
200
|
+
otherwise) that contradict the conditions of this License, they do not
|
201
|
+
excuse you from the conditions of this License. If you cannot
|
202
|
+
distribute so as to satisfy simultaneously your obligations under this
|
203
|
+
License and any other pertinent obligations, then as a consequence you
|
204
|
+
may not distribute the Program at all. For example, if a patent
|
205
|
+
license would not permit royalty-free redistribution of the Program by
|
206
|
+
all those who receive copies directly or indirectly through you, then
|
207
|
+
the only way you could satisfy both it and this License would be to
|
208
|
+
refrain entirely from distribution of the Program.
|
209
|
+
|
210
|
+
If any portion of this section is held invalid or unenforceable under
|
211
|
+
any particular circumstance, the balance of the section is intended to
|
212
|
+
apply and the section as a whole is intended to apply in other
|
213
|
+
circumstances.
|
214
|
+
|
215
|
+
It is not the purpose of this section to induce you to infringe any
|
216
|
+
patents or other property right claims or to contest validity of any
|
217
|
+
such claims; this section has the sole purpose of protecting the
|
218
|
+
integrity of the free software distribution system, which is
|
219
|
+
implemented by public license practices. Many people have made
|
220
|
+
generous contributions to the wide range of software distributed
|
221
|
+
through that system in reliance on consistent application of that
|
222
|
+
system; it is up to the author/donor to decide if he or she is willing
|
223
|
+
to distribute software through any other system and a licensee cannot
|
224
|
+
impose that choice.
|
225
|
+
|
226
|
+
This section is intended to make thoroughly clear what is believed to
|
227
|
+
be a consequence of the rest of this License.
|
228
|
+
|
229
|
+
8. If the distribution and/or use of the Program is restricted in
|
230
|
+
certain countries either by patents or by copyrighted interfaces, the
|
231
|
+
original copyright holder who places the Program under this License
|
232
|
+
may add an explicit geographical distribution limitation excluding
|
233
|
+
those countries, so that distribution is permitted only in or among
|
234
|
+
countries not thus excluded. In such case, this License incorporates
|
235
|
+
the limitation as if written in the body of this License.
|
236
|
+
|
237
|
+
9. The Free Software Foundation may publish revised and/or new versions
|
238
|
+
of the General Public License from time to time. Such new versions will
|
239
|
+
be similar in spirit to the present version, but may differ in detail to
|
240
|
+
address new problems or concerns.
|
241
|
+
|
242
|
+
Each version is given a distinguishing version number. If the Program
|
243
|
+
specifies a version number of this License which applies to it and "any
|
244
|
+
later version", you have the option of following the terms and conditions
|
245
|
+
either of that version or of any later version published by the Free
|
246
|
+
Software Foundation. If the Program does not specify a version number of
|
247
|
+
this License, you may choose any version ever published by the Free Software
|
248
|
+
Foundation.
|
249
|
+
|
250
|
+
10. If you wish to incorporate parts of the Program into other free
|
251
|
+
programs whose distribution conditions are different, write to the author
|
252
|
+
to ask for permission. For software which is copyrighted by the Free
|
253
|
+
Software Foundation, write to the Free Software Foundation; we sometimes
|
254
|
+
make exceptions for this. Our decision will be guided by the two goals
|
255
|
+
of preserving the free status of all derivatives of our free software and
|
256
|
+
of promoting the sharing and reuse of software generally.
|
257
|
+
|
258
|
+
NO WARRANTY
|
259
|
+
|
260
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
261
|
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
262
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
263
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
264
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
265
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
266
|
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
267
|
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
268
|
+
REPAIR OR CORRECTION.
|
269
|
+
|
270
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
271
|
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
272
|
+
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
273
|
+
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
274
|
+
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
275
|
+
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
276
|
+
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
277
|
+
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
278
|
+
POSSIBILITY OF SUCH DAMAGES.
|
279
|
+
|
280
|
+
END OF TERMS AND CONDITIONS
|
281
|
+
|
282
|
+
How to Apply These Terms to Your New Programs
|
283
|
+
|
284
|
+
If you develop a new program, and you want it to be of the greatest
|
285
|
+
possible use to the public, the best way to achieve this is to make it
|
286
|
+
free software which everyone can redistribute and change under these terms.
|
287
|
+
|
288
|
+
To do so, attach the following notices to the program. It is safest
|
289
|
+
to attach them to the start of each source file to most effectively
|
290
|
+
convey the exclusion of warranty; and each file should have at least
|
291
|
+
the "copyright" line and a pointer to where the full notice is found.
|
292
|
+
|
293
|
+
<one line to give the program's name and a brief idea of what it does.>
|
294
|
+
Copyright (C) <year> <name of author>
|
295
|
+
|
296
|
+
This program is free software; you can redistribute it and/or modify
|
297
|
+
it under the terms of the GNU General Public License as published by
|
298
|
+
the Free Software Foundation; either version 2 of the License, or
|
299
|
+
(at your option) any later version.
|
300
|
+
|
301
|
+
This program is distributed in the hope that it will be useful,
|
302
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
303
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
304
|
+
GNU General Public License for more details.
|
305
|
+
|
306
|
+
You should have received a copy of the GNU General Public License along
|
307
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
308
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
309
|
+
|
310
|
+
Also add information on how to contact you by electronic and paper mail.
|
311
|
+
|
312
|
+
If the program is interactive, make it output a short notice like this
|
313
|
+
when it starts in an interactive mode:
|
314
|
+
|
315
|
+
Gnomovision version 69, Copyright (C) year name of author
|
316
|
+
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
317
|
+
This is free software, and you are welcome to redistribute it
|
318
|
+
under certain conditions; type `show c' for details.
|
319
|
+
|
320
|
+
The hypothetical commands `show w' and `show c' should show the appropriate
|
321
|
+
parts of the General Public License. Of course, the commands you use may
|
322
|
+
be called something other than `show w' and `show c'; they could even be
|
323
|
+
mouse-clicks or menu items--whatever suits your program.
|
324
|
+
|
325
|
+
You should also get your employer (if you work as a programmer) or your
|
326
|
+
school, if any, to sign a "copyright disclaimer" for the program, if
|
327
|
+
necessary. Here is a sample; alter the names:
|
328
|
+
|
329
|
+
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
330
|
+
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
331
|
+
|
332
|
+
<signature of Ty Coon>, 1 April 1989
|
333
|
+
Ty Coon, President of Vice
|
334
|
+
|
335
|
+
This General Public License does not permit incorporating your program into
|
336
|
+
proprietary programs. If your program is a subroutine library, you may
|
337
|
+
consider it more useful to permit linking proprietary applications with the
|
338
|
+
library. If this is what you want to do, use the GNU Lesser General
|
339
|
+
Public License instead of this License.
|
340
|
+
-
|
data/README
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
|
2
|
+
= LogSimple
|
3
|
+
|
4
|
+
LogSimple - a Ruby logging library. LogSimple logs messages differently according to their level. For instance an 'error' message will go to syslog, but a 'critical' message will get emailed to the administrator. Log levels, and the behaviour associated with a given level, can be defined dynamically
|
5
|
+
by the user. Behaviours include 'formatters' (to modify the message) and 'outputters' (to
|
6
|
+
output the message), and while a certain number of predefined behaviours are provided as part of the
|
7
|
+
library, users can create new ones.
|
8
|
+
|
9
|
+
Copyright (c) 2006 Netuxo
|
10
|
+
|
11
|
+
Author : Netuxo (http://www.netuxo.co.uk) ; contact anselm at netuxo dot co dot uk
|
12
|
+
|
13
|
+
Licence : GPL V2 (See LICENSE[link:files/LICENSE.html])
|
14
|
+
|
15
|
+
Sponsor : War Resisters' International (http://www.wri-irg.org)
|
16
|
+
|
17
|
+
Ruby Forge project page : http://rubyforge.org/projects/logsimple/
|
18
|
+
|
19
|
+
Similar projects : Logger[http://ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html],
|
20
|
+
which is part of the Ruby standard library and Log4r[http://log4r.sourceforge.net/], inspired by the Apache Log4j
|
21
|
+
|
22
|
+
= Overview
|
23
|
+
== Outputters
|
24
|
+
Log levels are defined dynamically. Each level can be given any number of behaviours to apply when logging at that level. For
|
25
|
+
instance :
|
26
|
+
|
27
|
+
require 'logsimple.rb'
|
28
|
+
include LogSimple
|
29
|
+
|
30
|
+
Log.add_behaviour :debug, :log_stdout
|
31
|
+
Log.debug "message"
|
32
|
+
|
33
|
+
The line with the call to "add_behaviour" creates the log level "debug" (if it didn't already exist) and associates the behaviour "log_stdout".
|
34
|
+
The following line logs the message "message" at the level debug - so it gets logged to stdout.
|
35
|
+
|
36
|
+
Further behaviours can be added :
|
37
|
+
|
38
|
+
Log.add_behaviour :debug, :log_syslog
|
39
|
+
Log.debug "second message"
|
40
|
+
|
41
|
+
Now "second message" will be logged to stdout and then to syslog.
|
42
|
+
|
43
|
+
Behaviours can also have parameters associated, for instance :
|
44
|
+
|
45
|
+
Log.add_behaviour :info, :log_file
|
46
|
+
Log.info "hello", {:filename => "mylog.txt"}
|
47
|
+
|
48
|
+
Will log the message "hello" to the file "mylog.txt". Behaviours can be given default parameters
|
49
|
+
to avoid specifying them every time :
|
50
|
+
|
51
|
+
Log.add_parameters :log_file, {:filename => "mylog.txt"}
|
52
|
+
Log.info "another hello"
|
53
|
+
|
54
|
+
Will log "another hello" to the file "mylog.txt". In this case the default parameter applies
|
55
|
+
to the behaviour ':log_file', so changing the parameter will affect any log level that
|
56
|
+
uses this behaviour. It is also possible to set a parameter for only a specific log level :
|
57
|
+
|
58
|
+
Log.add_parameters :log_file, {:filename => "mylog.txt"}
|
59
|
+
Log.add_level_parameters :info, :log_file, {:filename => "theotherlog.txt"}
|
60
|
+
Log.add_behaviour :info, :log_file
|
61
|
+
Log.add_behaviour :debug, :log_file
|
62
|
+
Log.info "info"
|
63
|
+
Log.debug "debug"
|
64
|
+
|
65
|
+
Here the message "debug" goes to the generic file - mylog.txt - while the "info" message goes
|
66
|
+
to theotherlog.txt
|
67
|
+
|
68
|
+
== Formatters
|
69
|
+
|
70
|
+
Some behaviours are used to modifiy the message. For instance :
|
71
|
+
|
72
|
+
Log.add_behaviour :error, :append_time, :log_stdout
|
73
|
+
Log.error "error"
|
74
|
+
|
75
|
+
The logged message will have the current time appended to it. Behaviours are called in the
|
76
|
+
order they are defined. So :
|
77
|
+
|
78
|
+
Log.add_behaviour :error, :log_stdout, :append_time, :log_file
|
79
|
+
|
80
|
+
Will cause errors to first be logged to stdout (without the time) then to a file, with the time.
|
81
|
+
|
82
|
+
= Default behaviours, and default log levels
|
83
|
+
|
84
|
+
The following behaviours are defined by default :
|
85
|
+
|
86
|
+
- :*null*, which does not perform any action. The message is still sent to observers.
|
87
|
+
- :*log_stderr*, to log to stderr
|
88
|
+
- :*log_stdout*, to log to stdout
|
89
|
+
- :*log_file*, to log to a file. Requires the :*file_name* parameter to be set.
|
90
|
+
|
91
|
+
- :*log_syslog*, to log to syslog (when available). Will use the optional parameters
|
92
|
+
:*name* and :*level*. :name represents the application name (will use $0 if not set) and
|
93
|
+
:level represents the syslog error level. If not given, will log at syslog level 'err'.
|
94
|
+
The options are debug, info, notice, warning, err, alert, emerg and crit
|
95
|
+
|
96
|
+
- :*log_email* to send an email with the log (SMTP only). Requires :*smtp_server*, :*smtp_username*,
|
97
|
+
:*smtp_password*, :*from* and :*to* (may be an array) parameters to be set. If the optional
|
98
|
+
parameter :*subject* is set, then it is used for subject instead of $0 ; if the optional
|
99
|
+
parameter :*smtp_port* is set then it is used for port instead of 25 ; if the optional
|
100
|
+
parameter :*smtp_helo* is set then it is used for HELO message ; if the optional parameter
|
101
|
+
:*smpt_authtype* is set, then it is used to determine the type of authentification (:plain,
|
102
|
+
:login or :cram_md5). :plain is used by default.
|
103
|
+
|
104
|
+
- :*log_exec*, to call an executable with the log. Requires :*executable* parameter to be set
|
105
|
+
- :*raise*, to raise an exception. Will use the optional :*class_name* parameter to raise the
|
106
|
+
execption (otherwise, raises a RuntimeError exception)
|
107
|
+
- :*exit*, to terminate execution imediately
|
108
|
+
- :*append_level* Appends the log level at the begining of the message
|
109
|
+
- :*append_time* Appends the date/time at the begining of the message
|
110
|
+
|
111
|
+
By default, the following two log levels are created :
|
112
|
+
|
113
|
+
- :*warning*, for recovarable errors. Default behaviour is to append the level and log to stderr
|
114
|
+
- :*error*, for unrecovarable errors. Default behaviour is to append the level and log to
|
115
|
+
stderr and raise.
|
116
|
+
|
117
|
+
= Adding your own behaviours
|
118
|
+
|
119
|
+
Any behaviour can be added at run time ; provided there is a class instance function do_#{behaviour}
|
120
|
+
that takes the message string and a hash of parameters as parameters :
|
121
|
+
|
122
|
+
def Log.do_puts(message, p)
|
123
|
+
puts "Via puts : " + message
|
124
|
+
end
|
125
|
+
|
126
|
+
Log.add_behaviour :debug, :puts
|
127
|
+
Log.debug "bug"
|
128
|
+
|
129
|
+
Will output "Via puts : bug".
|
130
|
+
Behaviours that modify the message (such as :append_level) are defined by a class instance function
|
131
|
+
mod_#{behaviour} that takes the output level, the message string and a hash of parameters and
|
132
|
+
returns the modified string. For instance :
|
133
|
+
|
134
|
+
def Log.mod_append_prog(level, message, p)
|
135
|
+
"[" + $0.to_s + "] " + message
|
136
|
+
end
|
137
|
+
|
138
|
+
Will cause the program name to be appended at the begining of the message.
|
139
|
+
|
140
|
+
|
141
|
+
= Observers
|
142
|
+
|
143
|
+
Observers can be added as Proc objects. Those are called with the content of the message
|
144
|
+
before each message gets called :
|
145
|
+
|
146
|
+
last_log = nil
|
147
|
+
Log.add_observer {|m| last_log = m}
|
148
|
+
|
149
|
+
= Streams
|
150
|
+
|
151
|
+
It is possible to obtain stream objects linked to a particular log level which support the "<<"
|
152
|
+
operator. This is usefull to pass the logger as a generic stream to external classes. For instance:
|
153
|
+
|
154
|
+
o = Log.get_stream(:error)
|
155
|
+
o << "message"
|
156
|
+
|
157
|
+
The last line has the same effect as Log.error "message". Note that parameters cannot be passed to
|
158
|
+
stream objects. An extra string can be provided to a stream that gets appended at the begining
|
159
|
+
of every message :
|
160
|
+
|
161
|
+
o1 = Log.get_stream(:error, "[Module A] ")
|
162
|
+
o2 = Log.get_stream(:error, "[Module B] ")
|
163
|
+
o1 << "one"
|
164
|
+
o2 << "two"
|
165
|
+
|
166
|
+
Will log '[Module A] one' and '[Module B] two' to the error log.
|
167
|
+
|
168
|
+
|
data/lib/logsimple.rb
ADDED
@@ -0,0 +1,254 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Netuxo
|
3
|
+
# Contact anselm@netuxo.co.uk
|
4
|
+
#
|
5
|
+
# This file is part of LogSimple.
|
6
|
+
#
|
7
|
+
# LogSimple is free software; you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as published by
|
9
|
+
# the Free Software Foundation; either version 2 of the License, or
|
10
|
+
# (at your option) any later version.
|
11
|
+
#
|
12
|
+
# LogSimple is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public License
|
18
|
+
# along with Foobar; if not, write to the Free Software
|
19
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
20
|
+
#++
|
21
|
+
|
22
|
+
require 'syslog'
|
23
|
+
require 'digest/md5'
|
24
|
+
require 'net/smtp'
|
25
|
+
|
26
|
+
# LogSimple is a namespace for the Log class, to avoid potential nameclashes. (though in practice
|
27
|
+
# doing 'include LogSimple' is a plus, since otherwise every log command needs to be prefixed
|
28
|
+
# with the name space)
|
29
|
+
module LogSimple
|
30
|
+
end
|
31
|
+
|
32
|
+
# The Log class - a static class through which all the logging is done. See the
|
33
|
+
# README[link:files/README.html] for usage instructions.
|
34
|
+
class LogSimple::Log
|
35
|
+
@behaviour = Hash.new
|
36
|
+
@parameters = Hash.new
|
37
|
+
@level_parameters = Hash.new
|
38
|
+
|
39
|
+
class <<self
|
40
|
+
# Query all the possible behaviours, as defined by class instance methods do_#{behaviour}
|
41
|
+
def behaviours
|
42
|
+
res = Array.new
|
43
|
+
methods.each do |method|
|
44
|
+
if method =~ /^(do|mod)_(.+)$/ then res.push($2.to_sym) end
|
45
|
+
end
|
46
|
+
res
|
47
|
+
end
|
48
|
+
|
49
|
+
# Query the behaviour for a given log level. Returns an array with the behaviour flags
|
50
|
+
# that apply.
|
51
|
+
def behaviour(level)
|
52
|
+
@behaviour[level]
|
53
|
+
end
|
54
|
+
|
55
|
+
# Resets the behaviour for a given log level
|
56
|
+
def reset_behaviour(level)
|
57
|
+
@behaviour[level] = Array.new
|
58
|
+
@parameters[level] = Hash.new
|
59
|
+
end
|
60
|
+
|
61
|
+
# Adds a number of behaviours for the given level
|
62
|
+
def add_behaviour(level, *behaviours)
|
63
|
+
@behaviour[level] ||= Array.new
|
64
|
+
@behaviour[level].concat(behaviours)
|
65
|
+
|
66
|
+
name = "#{level.to_s}"
|
67
|
+
if !method_defined?(name)
|
68
|
+
class_eval("class <<self
|
69
|
+
def #{name}(message, inparams = Hash.new)
|
70
|
+
log(message, \"#{level.to_s}\".to_sym, inparams)
|
71
|
+
end
|
72
|
+
end")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Sets the parameters for a given behaviour.
|
77
|
+
def add_parameters(behaviour, params)
|
78
|
+
@parameters[behaviour] ||= Hash.new
|
79
|
+
@parameters[behaviour].merge!(params)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Sets the parameters for a given behaviour for a given level. These overide the
|
83
|
+
# ones given for a given behaviour only.
|
84
|
+
def add_level_parameters(level, behaviour, params)
|
85
|
+
@level_parameters[level] ||= Hash.new
|
86
|
+
@level_parameters[level][behaviour] ||= Hash.new
|
87
|
+
@level_parameters[level][behaviour].merge!(params)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Removes all parameters associated with a given behaviour
|
91
|
+
def remove_parameters(behaviour)
|
92
|
+
@parameters.delete(behaviour)
|
93
|
+
@level_parameters.each_value do |h|
|
94
|
+
h.delete(behaviour)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Log a message with the given level, and parameters (as a hash). These parameters
|
99
|
+
# override the ones given per level or per behaviour.
|
100
|
+
def log(message, level = :error, inparams = Hash.new)
|
101
|
+
@observers ||= Array.new
|
102
|
+
@observers.each {|p| p.call(message)}
|
103
|
+
|
104
|
+
@behaviour[level].each do |b|
|
105
|
+
params = Hash.new
|
106
|
+
params.merge!(@parameters[b]) if @parameters.has_key?(b)
|
107
|
+
params.merge!(@level_parameters[level][b]) if @level_parameters.has_key?(level) &&
|
108
|
+
@level_parameters[level].has_key?(b)
|
109
|
+
params.merge!(inparams)
|
110
|
+
|
111
|
+
if respond_to?("mod_#{b}".to_sym, true)
|
112
|
+
message = send("mod_#{b}".to_sym, level, message, params)
|
113
|
+
elsif respond_to?("do_#{b}".to_sym, true)
|
114
|
+
send("do_#{b}".to_sym, message, params)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Adds an observer to the class. Those are called with the message before the message gets
|
120
|
+
# logged
|
121
|
+
def add_observer(&obs)
|
122
|
+
@observers ||= Array.new
|
123
|
+
@observers.push(obs)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Gets a stream object for the given log level
|
127
|
+
def get_stream(level, pre = '')
|
128
|
+
o = Object.new
|
129
|
+
|
130
|
+
# 'def' method courtesy of http://www.bigbold.com/snippets/posts/show/2316
|
131
|
+
# Allows us to dynamically add methods to an object with closures.
|
132
|
+
def o.def(name, &block)
|
133
|
+
(class << self; self end).send(:define_method, name, block)
|
134
|
+
end
|
135
|
+
o.def(:<<) do |s|
|
136
|
+
Log.log(pre.to_s + s.to_s, level)
|
137
|
+
self
|
138
|
+
end
|
139
|
+
o
|
140
|
+
end
|
141
|
+
|
142
|
+
# :section: Behavious handlers
|
143
|
+
# The following are the individual predefined behaviour handlers
|
144
|
+
#
|
145
|
+
|
146
|
+
# null handler
|
147
|
+
def do_null(message, p)
|
148
|
+
end
|
149
|
+
|
150
|
+
# Logs a message to stderr
|
151
|
+
def do_log_stderr(message, p)
|
152
|
+
$stderr << message << "\n"
|
153
|
+
end
|
154
|
+
|
155
|
+
# Logs a message to stdout
|
156
|
+
def do_log_stdout(message, p)
|
157
|
+
$stdout << message << "\n"
|
158
|
+
end
|
159
|
+
|
160
|
+
# Logs a message to file. Requires the :file_name parameter
|
161
|
+
def do_log_file(message, p)
|
162
|
+
File.open(p[:file_name], 'a+') do |f|
|
163
|
+
f << message << "\n"
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# Logs to syslog (when available). Will use the optional parameters :name and :level.
|
168
|
+
# :name represents the application name (will use $0 if not set) and :level represents
|
169
|
+
# the syslog error level. If not given, will log at syslog level 'error'. The options are
|
170
|
+
# debug, info, notice, warning, err, alert, emerg and crit
|
171
|
+
def do_log_syslog(message, p)
|
172
|
+
level = 'err'
|
173
|
+
app = $0
|
174
|
+
|
175
|
+
level = p[:level] if p.has_key?(:level)
|
176
|
+
app = p[:name] if p.has_key?(:name)
|
177
|
+
|
178
|
+
log = Syslog.open(app)
|
179
|
+
log.send(level.to_sym, message)
|
180
|
+
log.close
|
181
|
+
end
|
182
|
+
|
183
|
+
# Sends an email with the log (SMTP only). Requires :smtp_server, :smtp_username, :smtp_password,
|
184
|
+
# :from and :to (may be an array) parameters to be set. If the optional parameter :subject is set,
|
185
|
+
# then it is used for subject instead of $0 ; if the optional parameter :smtp_port is set then it
|
186
|
+
# is used for port instead of 25 ; if the optional parameter :smtp_helo is set then it is used for
|
187
|
+
# HELO message ; if the optional parameter smpt_authtype is set, then it is used to determine the
|
188
|
+
# type of authentification (:plain, :login or :cram_md5). :plain is used by default.
|
189
|
+
def do_log_email(message, p)
|
190
|
+
port = 25
|
191
|
+
port = p[:smtp_port] if p.has_key?(:smtp_port)
|
192
|
+
helo = nil
|
193
|
+
helo = p[:smtp_helo] if p.has_key?(:smtp_helo)
|
194
|
+
authtype = :plain
|
195
|
+
authtype = p[:smtp_authtype] if p.has_key?(:smtp_authtype)
|
196
|
+
subject = $0
|
197
|
+
subject = p[:subject] if p.has_key?(:subject)
|
198
|
+
p[:from] = p[:from].joing(',') if (p[:from].kind_of? Array)
|
199
|
+
date_str = Time.now.strftime("%c")
|
200
|
+
mid = Digest::MD5.hexdigest(date_str + "-" + message).to_s
|
201
|
+
|
202
|
+
msgstr = "
|
203
|
+
From: #{p[:from]}
|
204
|
+
To: #{p[:to]}
|
205
|
+
Subject: #{subject}
|
206
|
+
Date: #{date_str}
|
207
|
+
Message-Id: <#{mid}>
|
208
|
+
|
209
|
+
#{message}
|
210
|
+
|
211
|
+
"
|
212
|
+
|
213
|
+
Net::SMTP.start(p[:smtp_server], port, helo, p[:smtp_username], p[:smtp_password],
|
214
|
+
authtype) do |smtp|
|
215
|
+
smtp.send_message(msgstr, p[:from], p[:to])
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
# Runs an executable with the log. Requires :executable parameter to be set
|
220
|
+
def do_log_exec(message, p)
|
221
|
+
exec(p[:executable].to_s + " " +message) if fork.nil?
|
222
|
+
end
|
223
|
+
|
224
|
+
# Raises an exception with the log. Will use the optional :class_name parameter to raise the
|
225
|
+
# execption (otherwise, raises a RuntimeError exception). #{:class_name}.new should take one string
|
226
|
+
# parameter (the log message)
|
227
|
+
def do_raise(message, p)
|
228
|
+
if (p.has_key?(:class_name))
|
229
|
+
raise Kernel.const_get(p[:class_name]).new(message)
|
230
|
+
end
|
231
|
+
|
232
|
+
raise message
|
233
|
+
end
|
234
|
+
|
235
|
+
# To terminate execution imediately
|
236
|
+
def do_exit(message, p)
|
237
|
+
exit
|
238
|
+
end
|
239
|
+
|
240
|
+
# Adds the output level in front of the message
|
241
|
+
def mod_append_level(level, message, p)
|
242
|
+
"[" + level.to_s + "] " + message.to_s
|
243
|
+
end
|
244
|
+
|
245
|
+
# Adds the date/time in front of the message
|
246
|
+
def mod_append_time(level, message, p)
|
247
|
+
"[" + Time.now.strftime("%H:%M:%S %d/%m/%y") + "] " + message.to_s
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
LogSimple::Log.add_behaviour :warning, :append_level, :log_stderr
|
254
|
+
LogSimple::Log.add_behaviour :error, :append_level, :log_stderr, :raise
|
@@ -0,0 +1,242 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Netuxo
|
3
|
+
# Contact anselm@netuxo.co.uk
|
4
|
+
#
|
5
|
+
# This file is part of LogSimple.
|
6
|
+
#
|
7
|
+
# LogSimple is free software; you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as published by
|
9
|
+
# the Free Software Foundation; either version 2 of the License, or
|
10
|
+
# (at your option) any later version.
|
11
|
+
#
|
12
|
+
# LogSimple is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public License
|
18
|
+
# along with Foobar; if not, write to the Free Software
|
19
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
20
|
+
#++
|
21
|
+
|
22
|
+
$:.unshift(File.dirname(__FILE__) + "/../../lib")
|
23
|
+
|
24
|
+
require 'test/unit'
|
25
|
+
require 'stringio'
|
26
|
+
require 'logsimple.rb'
|
27
|
+
include LogSimple
|
28
|
+
|
29
|
+
# Set the File class to return canned data. Do this in a way that won't interfere with Test::Unit
|
30
|
+
class File
|
31
|
+
class << self
|
32
|
+
alias :old_open :open
|
33
|
+
def open(name, mode)
|
34
|
+
if Kernel::caller[0] !~ /do_log_file/
|
35
|
+
old_open(name,mode)
|
36
|
+
else
|
37
|
+
$glob << "File " << name << " mode " << mode
|
38
|
+
yield $glob
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Set the syslog class to return canned data
|
45
|
+
def Syslog.open(app)
|
46
|
+
$glob << app.to_s
|
47
|
+
o = Object.new
|
48
|
+
class <<o
|
49
|
+
def send(level, message)
|
50
|
+
$glob << level.to_s << message
|
51
|
+
end
|
52
|
+
def close
|
53
|
+
end
|
54
|
+
end
|
55
|
+
o
|
56
|
+
end
|
57
|
+
|
58
|
+
# Set the SMTP class to return canned data
|
59
|
+
module Net
|
60
|
+
def SMTP.start(server, port, helo, username, password, authtype)
|
61
|
+
$glob << server << port << helo << username << password << authtype
|
62
|
+
o = Object.new
|
63
|
+
class <<o
|
64
|
+
def send_message(msg, from, to)
|
65
|
+
$glob << from << to << msg
|
66
|
+
end
|
67
|
+
end
|
68
|
+
yield o
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Set the Time class to return canned data. Do this so that it doesn't interfere with
|
73
|
+
# Test::Unit
|
74
|
+
class Time
|
75
|
+
class << self
|
76
|
+
alias :old_now :now
|
77
|
+
def now
|
78
|
+
if Kernel::caller[0] !~ /do_log_email|append_time/
|
79
|
+
old_now
|
80
|
+
else
|
81
|
+
o = Object.new
|
82
|
+
class <<o
|
83
|
+
def strftime(mode)
|
84
|
+
"strftime " + mode
|
85
|
+
end
|
86
|
+
end
|
87
|
+
o
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Set the 'exec' and 'fork' to behave as canned
|
94
|
+
module Kernel
|
95
|
+
alias :old_fork :fork
|
96
|
+
alias :old_exec :exec
|
97
|
+
def fork
|
98
|
+
if Kernel::caller[0] !~ /do_log_exec/
|
99
|
+
old_fork
|
100
|
+
else
|
101
|
+
nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
def exec(str)
|
105
|
+
if Kernel::caller[0] !~ /do_log_exec/
|
106
|
+
old_exec(str)
|
107
|
+
else
|
108
|
+
$glob << str
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Set 'exit' to behave cannedely
|
114
|
+
module Kernel
|
115
|
+
alias :old_exit :exit
|
116
|
+
def exit(c = 0)
|
117
|
+
if Kernel::caller[0] !~ /do_exit/
|
118
|
+
old_exit c
|
119
|
+
else
|
120
|
+
$glob << "Exit!"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Add a custom behaviour to the Log class
|
126
|
+
def Log.do_newb(m, p)
|
127
|
+
$glob << m << "-"
|
128
|
+
p.each {|n,v| $glob << n.to_s << ":" << v.to_s}
|
129
|
+
end
|
130
|
+
|
131
|
+
# This class tests the predefined behaviours of the Log class
|
132
|
+
class TestTranslator < Test::Unit::TestCase
|
133
|
+
# Test stderr and stdout
|
134
|
+
def test_basic
|
135
|
+
$stdout = StringIO.new("", "w")
|
136
|
+
$stderr = StringIO.new("", "w")
|
137
|
+
|
138
|
+
Log.reset_behaviour :test
|
139
|
+
Log.reset_behaviour :test2
|
140
|
+
Log.add_behaviour :test, :log_stdout
|
141
|
+
Log.add_behaviour :test2, :log_stderr
|
142
|
+
|
143
|
+
Log.test "message 1"
|
144
|
+
Log.test2 "message 2"
|
145
|
+
|
146
|
+
assert_equal("message 1\n", $stdout.string)
|
147
|
+
assert_equal("message 2\n", $stderr.string)
|
148
|
+
|
149
|
+
$stdout = STDOUT
|
150
|
+
$stderr = STDERR
|
151
|
+
end
|
152
|
+
|
153
|
+
# Test loggin to file
|
154
|
+
def test_file
|
155
|
+
$glob = StringIO.new("", "w")
|
156
|
+
Log.reset_behaviour :test
|
157
|
+
Log.add_behaviour :test, :log_file
|
158
|
+
|
159
|
+
Log.test "message", {:file_name => 'test.txt'}
|
160
|
+
assert_equal("File test.txt mode a+message\n", $glob.string)
|
161
|
+
end
|
162
|
+
|
163
|
+
# Test logging to syslog
|
164
|
+
def test_syslog
|
165
|
+
$glob = StringIO.new("", "w")
|
166
|
+
Log.reset_behaviour :test
|
167
|
+
Log.add_behaviour :test, :log_syslog
|
168
|
+
|
169
|
+
Log.test "message"
|
170
|
+
assert_equal($0 + "errmessage", $glob.string)
|
171
|
+
end
|
172
|
+
|
173
|
+
# Test sending an email
|
174
|
+
def test_email
|
175
|
+
$glob = StringIO.new("", "w")
|
176
|
+
Log.reset_behaviour :test
|
177
|
+
Log.add_behaviour :test, :log_email
|
178
|
+
|
179
|
+
Log.test "message", {:smtp_server => 'server', :smtp_username => 'username',
|
180
|
+
:smtp_password => 'password', :from => 'from', :to => 'to',
|
181
|
+
:subject => 'subject', :smtp_port => 'port', :smtp_helo => 'helo',
|
182
|
+
:smtp_authtype => 'authtype'}
|
183
|
+
|
184
|
+
assert_equal("serverporthelousernamepasswordauthtypefromto\n From: from\n To: to\n Subject: subject\n Date: strftime %c\n Message-Id: <594f5e59481d8f72bb81aa1d55867f8d>\n \n message\n\n ", $glob.string)
|
185
|
+
end
|
186
|
+
|
187
|
+
# Test the exec logger
|
188
|
+
def test_exec
|
189
|
+
$glob = StringIO.new("", "w")
|
190
|
+
Log.reset_behaviour :test
|
191
|
+
Log.add_behaviour :test, :log_exec
|
192
|
+
|
193
|
+
Log.test "message", {:executable => 'hello'}
|
194
|
+
|
195
|
+
assert_equal("hello message", $glob.string)
|
196
|
+
end
|
197
|
+
|
198
|
+
# Test the raise
|
199
|
+
def test_raise
|
200
|
+
Log.reset_behaviour :test
|
201
|
+
Log.add_behaviour :test, :raise
|
202
|
+
|
203
|
+
assert_raise(RuntimeError) {
|
204
|
+
Log.test "hello"
|
205
|
+
}
|
206
|
+
|
207
|
+
assert_raise(ArgumentError) {
|
208
|
+
Log.test "hello", {:class_name => 'ArgumentError'}
|
209
|
+
}
|
210
|
+
end
|
211
|
+
|
212
|
+
# Test exit
|
213
|
+
def test_exit
|
214
|
+
$glob = StringIO.new("", "w")
|
215
|
+
Log.reset_behaviour :test
|
216
|
+
Log.add_behaviour :test, :exit
|
217
|
+
|
218
|
+
Log.test "message"
|
219
|
+
assert_equal("Exit!", $glob.string)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Test append level
|
223
|
+
def test_appendlevel
|
224
|
+
$glob = StringIO.new("", "w")
|
225
|
+
Log.reset_behaviour :test
|
226
|
+
Log.add_behaviour :test, :append_level, :newb
|
227
|
+
|
228
|
+
Log.test "hello"
|
229
|
+
assert_equal("[test] hello-", $glob.string)
|
230
|
+
end
|
231
|
+
|
232
|
+
# Test append time
|
233
|
+
def test_appendtime
|
234
|
+
$glob = StringIO.new("", "w")
|
235
|
+
Log.reset_behaviour :test
|
236
|
+
Log.add_behaviour :test, :append_time, :newb
|
237
|
+
|
238
|
+
Log.test "hello"
|
239
|
+
assert_equal("[strftime %H:%M:%S %d/%m/%y] hello-", $glob.string)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
@@ -0,0 +1,152 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Netuxo
|
3
|
+
# Contact anselm@netuxo.co.uk
|
4
|
+
#
|
5
|
+
# This file is part of LogSimple.
|
6
|
+
#
|
7
|
+
# LogSimple is free software; you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as published by
|
9
|
+
# the Free Software Foundation; either version 2 of the License, or
|
10
|
+
# (at your option) any later version.
|
11
|
+
#
|
12
|
+
# LogSimple is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public License
|
18
|
+
# along with Foobar; if not, write to the Free Software
|
19
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
20
|
+
#++
|
21
|
+
|
22
|
+
$:.unshift(File.dirname(__FILE__) + "/../../lib")
|
23
|
+
|
24
|
+
require 'test/unit'
|
25
|
+
require 'logsimple.rb'
|
26
|
+
include LogSimple
|
27
|
+
|
28
|
+
# Add a custom behaviour to the Log class
|
29
|
+
$glob = ''
|
30
|
+
def Log.do_newb(m, p)
|
31
|
+
$glob << m << "-"
|
32
|
+
p.each {|n,v| $glob << n.to_s << ":" << v.to_s}
|
33
|
+
end
|
34
|
+
def Log.mod_boink(l,m,p)
|
35
|
+
"boink! " + m
|
36
|
+
end
|
37
|
+
|
38
|
+
class TestTranslator < Test::Unit::TestCase
|
39
|
+
# Test querying behaviours, predefined behaviours, existence of default log levels,
|
40
|
+
# behaviours for default log levels.
|
41
|
+
# Test adding/removing behaviours to log levels, creating new log levels
|
42
|
+
def test_one
|
43
|
+
# Check all default behaviours (+ the 'newb' one we added)
|
44
|
+
assert_equal([:append_level, :append_time, :boink, :exit, :log_email, :log_exec, :log_file,
|
45
|
+
:log_stderr, :log_stdout, :log_syslog, :newb, :null, :raise],
|
46
|
+
Log.behaviours.sort {|a,b| a.to_s <=> b.to_s})
|
47
|
+
|
48
|
+
# Check behaviours for default log levels (and test they are
|
49
|
+
# indeed defaults!)
|
50
|
+
assert_equal([:append_level, :log_stderr, :raise],
|
51
|
+
Log.behaviour(:error).sort {|a,b| a.to_s <=>b.to_s})
|
52
|
+
assert_equal([:append_level, :log_stderr],
|
53
|
+
Log.behaviour(:warning).sort {|a,b| a.to_s <=>b.to_s})
|
54
|
+
|
55
|
+
|
56
|
+
# Check reseting behaviours
|
57
|
+
Log.reset_behaviour :error
|
58
|
+
assert_equal([], Log.behaviour(:error))
|
59
|
+
|
60
|
+
# Check adding behaviours
|
61
|
+
Log.add_behaviour :newlevel, :log_stdout
|
62
|
+
assert_equal([:log_stdout], Log.behaviour(:newlevel))
|
63
|
+
Log.add_behaviour :newlevel, :raise, :exit
|
64
|
+
assert_equal([:log_stdout, :raise, :exit], Log.behaviour(:newlevel))
|
65
|
+
end
|
66
|
+
|
67
|
+
# Test custom behaviour. Test logging. Test parameters (log parameters,
|
68
|
+
# level parameters and behaviour parameters)
|
69
|
+
def test_two
|
70
|
+
# Test basic login
|
71
|
+
$glob = ''
|
72
|
+
Log.add_behaviour :ll, :newb
|
73
|
+
Log.ll "hello"
|
74
|
+
assert_equal("hello-", $glob)
|
75
|
+
|
76
|
+
# Test log parameters
|
77
|
+
$glob = ''
|
78
|
+
Log.ll "hello", {:bicycle => :good}
|
79
|
+
assert_equal("hello-bicycle:good", $glob)
|
80
|
+
|
81
|
+
# Test behaviour parameters
|
82
|
+
$glob = ''
|
83
|
+
Log.add_parameters :newb, {:car => :bad}
|
84
|
+
Log.ll "hello"
|
85
|
+
assert_equal("hello-car:bad", $glob)
|
86
|
+
|
87
|
+
# Test level parameters (and removing behaviour parameters)
|
88
|
+
$glob = ''
|
89
|
+
Log.remove_parameters :newb
|
90
|
+
Log.add_behaviour :mm, :newb
|
91
|
+
Log.add_behaviour :nn, :newb
|
92
|
+
Log.add_level_parameters :mm, :newb, {:airplane => :verybad}
|
93
|
+
Log.mm "hello"
|
94
|
+
Log.nn "boink"
|
95
|
+
assert_equal("hello-airplane:verybadboink-", $glob)
|
96
|
+
|
97
|
+
# Test removing level parameters
|
98
|
+
$glob = ''
|
99
|
+
Log.remove_parameters :newb
|
100
|
+
Log.mm "hello"
|
101
|
+
assert_equal("hello-", $glob)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Test observers
|
105
|
+
def test_three
|
106
|
+
$glob2 = ''
|
107
|
+
Log.add_observer {|m| $glob2 << m}
|
108
|
+
Log.add_behaviour :dummy, :null
|
109
|
+
|
110
|
+
Log.dummy "hello"
|
111
|
+
assert_equal("hello", $glob2)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Test adding modifying behaviours
|
115
|
+
def test_four
|
116
|
+
# Test user defined modifying behaviour
|
117
|
+
$glob = ''
|
118
|
+
Log.reset_behaviour :blip
|
119
|
+
Log.add_behaviour :blip, :boink, :newb
|
120
|
+
Log.blip "hello"
|
121
|
+
assert_equal("boink! hello-", $glob)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Test stream objects
|
125
|
+
def test_five
|
126
|
+
$glob = ''
|
127
|
+
|
128
|
+
Log.reset_behaviour :blip
|
129
|
+
Log.add_behaviour :blip, :append_level, :newb
|
130
|
+
|
131
|
+
o = Log.get_stream :blip
|
132
|
+
o << "hello"
|
133
|
+
|
134
|
+
assert_equal("[blip] hello-", $glob)
|
135
|
+
|
136
|
+
# Make sure stream objects don't overlap each other (ie. they get the level right)
|
137
|
+
$glob = ''
|
138
|
+
Log.reset_behaviour :blop
|
139
|
+
Log.add_behaviour :blop, :append_level, :newb
|
140
|
+
(Log.get_stream :blip) << "blip"
|
141
|
+
(Log.get_stream :blop) << "blop"
|
142
|
+
assert_equal("[blip] blip-[blop] blop-", $glob)
|
143
|
+
|
144
|
+
# Check they append strings correctly
|
145
|
+
$glob = ''
|
146
|
+
Log.reset_behaviour :blop
|
147
|
+
Log.add_behaviour :blop, :newb
|
148
|
+
(Log.get_stream :blop, "[something] ") << "hello"
|
149
|
+
assert_equal("[something] hello-", $glob)
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: LogSimple
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.1"
|
7
|
+
date: 2006-09-04 00:00:00 +01:00
|
8
|
+
summary: LogSimple is a Ruby logging library
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: anselm@netuxo.co.uk
|
12
|
+
homepage: logsimple.rubyforge.org
|
13
|
+
rubyforge_project: logsimple
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Netuxo
|
30
|
+
files:
|
31
|
+
- test/unit
|
32
|
+
- test/unit/test_logsimple.rb
|
33
|
+
- test/unit/test_behaviours.rb
|
34
|
+
- lib/logsimple.rb
|
35
|
+
- README
|
36
|
+
- INSTALL
|
37
|
+
- LICENSE
|
38
|
+
- CHANGELOG
|
39
|
+
test_files:
|
40
|
+
- test/unit/test_logsimple.rb
|
41
|
+
rdoc_options:
|
42
|
+
- --main
|
43
|
+
- README
|
44
|
+
- --title
|
45
|
+
- LogSimple
|
46
|
+
extra_rdoc_files:
|
47
|
+
- README
|
48
|
+
- INSTALL
|
49
|
+
- LICENSE
|
50
|
+
- CHANGELOG
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
dependencies: []
|
58
|
+
|