logsly 1.2.0 → 1.3.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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/lib/logsly/colors.rb +2 -2
  4. data/lib/logsly/logging182/appender.rb +290 -0
  5. data/lib/logsly/logging182/appenders/buffering.rb +398 -0
  6. data/lib/logsly/logging182/appenders/console.rb +81 -0
  7. data/lib/logsly/logging182/appenders/email.rb +178 -0
  8. data/lib/logsly/logging182/appenders/file.rb +85 -0
  9. data/lib/logsly/logging182/appenders/growl.rb +200 -0
  10. data/lib/logsly/logging182/appenders/io.rb +84 -0
  11. data/lib/logsly/logging182/appenders/rolling_file.rb +338 -0
  12. data/lib/logsly/logging182/appenders/string_io.rb +92 -0
  13. data/lib/logsly/logging182/appenders/syslog.rb +215 -0
  14. data/lib/logsly/logging182/appenders.rb +64 -0
  15. data/lib/logsly/logging182/color_scheme.rb +248 -0
  16. data/lib/logsly/logging182/config/configurator.rb +187 -0
  17. data/lib/logsly/logging182/config/yaml_configurator.rb +190 -0
  18. data/lib/logsly/logging182/diagnostic_context.rb +332 -0
  19. data/lib/logsly/logging182/layout.rb +132 -0
  20. data/lib/logsly/logging182/layouts/basic.rb +38 -0
  21. data/lib/logsly/logging182/layouts/parseable.rb +256 -0
  22. data/lib/logsly/logging182/layouts/pattern.rb +568 -0
  23. data/lib/logsly/logging182/layouts.rb +9 -0
  24. data/lib/logsly/logging182/log_event.rb +44 -0
  25. data/lib/logsly/logging182/logger.rb +509 -0
  26. data/lib/logsly/logging182/proxy.rb +59 -0
  27. data/lib/logsly/logging182/rails_compat.rb +36 -0
  28. data/lib/logsly/logging182/repository.rb +231 -0
  29. data/lib/logsly/logging182/root_logger.rb +60 -0
  30. data/lib/logsly/logging182/stats.rb +277 -0
  31. data/lib/logsly/logging182/utils.rb +231 -0
  32. data/lib/logsly/logging182.rb +559 -0
  33. data/lib/logsly/outputs.rb +5 -5
  34. data/lib/logsly/version.rb +1 -1
  35. data/lib/logsly.rb +6 -6
  36. data/logsly.gemspec +4 -2
  37. data/test/unit/colors_tests.rb +3 -3
  38. data/test/unit/logsly_tests.rb +14 -14
  39. data/test/unit/outputs_tests.rb +34 -24
  40. metadata +45 -6
@@ -0,0 +1,231 @@
1
+
2
+ require 'thread'
3
+ require 'rbconfig'
4
+
5
+ # --------------------------------------------------------------------------
6
+ class Hash
7
+
8
+ # call-seq:
9
+ # getopt( key, default = nil, :as => class )
10
+ #
11
+ # Returns the value associated with the _key_. If the has does not contain
12
+ # the _key_, then the _default_ value is returned.
13
+ #
14
+ # Optionally, the value can be converted into to an instance of the given
15
+ # _class_. The supported classes are:
16
+ #
17
+ # Integer
18
+ # Float
19
+ # Array
20
+ # String
21
+ # Symbol
22
+ #
23
+ # If the value is +nil+, then no conversion will be performed.
24
+ #
25
+ def getopt( *args )
26
+ opts = args.last.instance_of?(Hash) ? args.pop : {}
27
+ key, default = args
28
+
29
+ val = if has_key?(key); self[key]
30
+ elsif has_key?(key.to_s); self[key.to_s]
31
+ elsif has_key?(key.to_s.intern); self[key.to_s.intern]
32
+ else default end
33
+
34
+ return if val.nil?
35
+ return val unless opts.has_key?(:as)
36
+
37
+ case opts[:as].name.intern
38
+ when :Integer; Integer(val)
39
+ when :Float; Float(val)
40
+ when :Array; Array(val)
41
+ when :String; String(val)
42
+ when :Symbol; String(val).intern
43
+ else val end
44
+ end
45
+ end
46
+
47
+ # --------------------------------------------------------------------------
48
+ class String
49
+
50
+ # call-seq:
51
+ # reduce( width, ellipses = '...' ) #=> string
52
+ #
53
+ # Reduce the size of the current string to the given _width_ by removing
54
+ # characters from the middle of the string and replacing them with
55
+ # _ellipses_. If the _width_ is greater than the length of the string, the
56
+ # string is returned unchanged. If the _width_ is less than the length of
57
+ # the _ellipses_, then the _ellipses_ are returned.
58
+ #
59
+ def reduce( width, ellipses = '...')
60
+ raise ArgumentError, "width cannot be negative: #{width}" if width < 0
61
+
62
+ return self if length <= width
63
+
64
+ remove = length - width + ellipses.length
65
+ return ellipses.dup if remove >= length
66
+
67
+ left_end = (length + 1 - remove) / 2
68
+ right_start = left_end + remove
69
+
70
+ left = self[0,left_end]
71
+ right = self[right_start,length-right_start]
72
+
73
+ left << ellipses << right
74
+ end
75
+ end
76
+
77
+ # --------------------------------------------------------------------------
78
+ class Module
79
+
80
+ # call-seq:
81
+ # logger_name #=> string
82
+ #
83
+ # Returns a predictable logger name for the current module or class. If
84
+ # used within an anonymous class, the first non-anonymous class name will
85
+ # be used as the logger name. If used within a meta-class, the name of the
86
+ # actual class will be used as the logger name. If used within an
87
+ # anonymous module, the string 'anonymous' will be returned.
88
+ #
89
+ def logger_name
90
+ return name unless name.nil? or name.empty?
91
+
92
+ # check if this is a metaclass (or eigenclass)
93
+ if ancestors.include? Class
94
+ inspect =~ %r/#<Class:([^#>]+)>/
95
+ return $1
96
+ end
97
+
98
+ # see if we have a superclass
99
+ if respond_to? :superclass
100
+ return superclass.logger_name
101
+ end
102
+
103
+ # we are an anonymous module
104
+ ::Logsly::Logging182.log_internal(-2) {
105
+ 'cannot return a predictable, unique name for anonymous modules'
106
+ }
107
+ return 'anonymous'
108
+ end
109
+ end
110
+
111
+ # --------------------------------------------------------------------------
112
+ module Kernel
113
+
114
+ # call-seq:
115
+ # require?( string )
116
+ #
117
+ # Attempt to the load the library named _string_ using the standard
118
+ # Kernel#require method. Returns +true+ if the library was successfully
119
+ # loaded. Returns +false+ if the library could not be loaded. This method
120
+ # will never raise an exception.
121
+ #
122
+ def require?( string )
123
+ require string
124
+ return true
125
+ rescue LoadError
126
+ return false
127
+ end
128
+ end # module Kernel
129
+
130
+ # --------------------------------------------------------------------------
131
+ class File
132
+
133
+ # Returns <tt>true</tt> if another process holds an exclusive lock on the
134
+ # file. Returns <tt>false</tt> if this is not the case.
135
+ #
136
+ # If a <tt>block</tt> of code is passed to this method, it will be run iff
137
+ # this process can obtain an exclusive lock on the file. The block will be
138
+ # run while this lock is held, and the exclusive lock will be released when
139
+ # the method returns.
140
+ #
141
+ # The exclusive lock is requested in a non-blocking mode. This method will
142
+ # return immediately (and the block will not be executed) if an exclusive
143
+ # lock cannot be obtained.
144
+ #
145
+ def flock?
146
+ status = flock(LOCK_EX|LOCK_NB)
147
+ case status
148
+ when false; true
149
+ when 0; block_given? ? yield : false
150
+ else
151
+ raise SystemCallError, "flock failed with status: #{status}"
152
+ end
153
+ ensure
154
+ flock LOCK_UN
155
+ end
156
+
157
+ # Execute a <tt>block</tt> in the context of a shared lock on this file. A
158
+ # shared lock will be obtained on the file, the block executed, and the lock
159
+ # released.
160
+ #
161
+ def flock_sh
162
+ flock LOCK_SH
163
+ yield
164
+ ensure
165
+ flock LOCK_UN
166
+ end
167
+
168
+ # :stopdoc:
169
+ conf = defined?(RbConfig) ? RbConfig::CONFIG : Config::CONFIG
170
+ if conf['host_os'] =~ /mswin|windows|cygwin|mingw/i
171
+ # don't lock files on windows
172
+ undef :flock?, :flock_sh
173
+ def flock?() yield; end
174
+ def flock_sh() yield; end
175
+ end
176
+ # :startdoc:
177
+
178
+ end
179
+
180
+ # --------------------------------------------------------------------------
181
+ module FileUtils
182
+
183
+ # Concatenate the contents of the _src_ file to the end of the _dest_ file.
184
+ # If the _dest_ file does not exist, then the _src_ file is copied to the
185
+ # _dest_ file using +copy_file+.
186
+ #
187
+ def concat( src, dest )
188
+ if File.exist?(dest)
189
+ bufsize = File.stat(dest).blksize || 8192
190
+ buffer = String.new
191
+
192
+ File.open(dest, 'a') { |d|
193
+ File.open(src, 'r') { |r|
194
+ while bytes = r.read(bufsize, buffer)
195
+ d.syswrite bytes
196
+ end
197
+ }
198
+ }
199
+ else
200
+ copy_file(src, dest)
201
+ end
202
+ end
203
+ module_function :concat
204
+ end
205
+
206
+ # --------------------------------------------------------------------------
207
+ class ReentrantMutex < Mutex
208
+
209
+ def initialize
210
+ super
211
+ @locker = nil
212
+ end
213
+
214
+ alias :original_synchronize :synchronize
215
+
216
+ def synchronize
217
+ if @locker == Thread.current
218
+ yield
219
+ else
220
+ original_synchronize {
221
+ begin
222
+ @locker = Thread.current
223
+ yield
224
+ ensure
225
+ @locker = nil
226
+ end
227
+ }
228
+ end
229
+ end
230
+ end # ReentrantMutex
231
+