logjam 1.0.0 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # Copyright (c), 2015 Peter Wood
4
+ # See the license.txt for details of the licensing of the code in this file.
5
+
6
+ module LogJam
7
+ class Configuration < Configurative::Settings
8
+ sources *(Dir.glob(File.join(Dir.getwd, "logging.{yml,yaml,json}")) +
9
+ Dir.glob(File.join(Dir.getwd, "config", "logging.{yml,yaml,json}")) +
10
+ Dir.glob(File.join(Dir.getwd, "**", "application.{yml,yaml,json}")))
11
+ section "logging"
12
+ end
13
+ end
@@ -7,34 +7,17 @@ require 'stringio'
7
7
 
8
8
  module LogJam
9
9
  # This class provides the exception class used by the LogJam library.
10
- class LogJamError < StandardError
11
- # Attribute accessor/mutator declarations.
12
- attr_accessor :verbose
13
-
14
- # Constructor for the LogJamError class.
10
+ class Error < StandardError
11
+ # Constructor for the LogJam::Error class.
15
12
  #
16
13
  # ==== Parameters
17
14
  # message:: The message to be associated with the error.
18
15
  # cause:: Any underlying exception to be associated with the error.
19
16
  # Defaults to nil.
20
- def initialize(message, cause=nil, verbose=true)
17
+ def initialize(message, cause=nil)
21
18
  super(message)
22
- @cause = cause
23
- @verbose = verbose
24
- end
25
-
26
- # This method fetches a stringified interpretation of an exception.
27
- def to_s()
28
- text = StringIO.new
29
- text << super
30
- if @verbose
31
- text << "\n" + self.backtrace.join("\n")
32
- if !@cause.nil?
33
- text << "\n\nCause: #{@cause}"
34
- text << "\n" + @cause.backtrace.join("\n")
35
- end
36
- end
37
- text.string
19
+ @cause = cause
38
20
  end
21
+ attr_reader :cause
39
22
  end
40
- end
23
+ end
@@ -0,0 +1,62 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # Copyright (c), 2012 Peter Wood
4
+ # See the license.txt for details of the licensing of the code in this file.
5
+
6
+ module LogJam
7
+ # This class represents a specialization of the Ruby Logger class. The class
8
+ # retains a Ruby Logger instance within itself and delegates all standard
9
+ # logger calls to this instance. This allows for changes to the underlying
10
+ # logger without changing the containing one, thus bypassing people caching
11
+ # an instance.
12
+ class Logger < ::Logger
13
+ extend Forwardable
14
+ def_delegators :@log, :add, :close, :datetime_format, :datetime_format=,
15
+ :debug, :debug?, :error, :error?, :fatal, :fatal?,
16
+ :formatter, :formatter=, :info, :info?, :level,
17
+ :level=, :progname, :progname=, :sev_threshold,
18
+ :sev_threshold=, :unknown, :warn, :warn?
19
+
20
+ # Constructor for the Logger class. All parameters are passed
21
+ # straight through to create a standard Ruby Logger instance except
22
+ # when the first parameter is a Logger instance. In this case the
23
+ # Logger passed in is used rather than creating a new one.
24
+ #
25
+ # ==== Parameters
26
+ # logdev:: The log device to be used by the logger. This should be
27
+ # be a String containing a file path/name, an IO object that
28
+ # the logging details will be written to or another logger
29
+ # that you want to wrap.
30
+ # shift_age:: The maximum number of old log files to retain or a String
31
+ # containing the rotation frequency for the log.
32
+ # shift_size:: The maximum size that the logging output will be allowed
33
+ # to grow to before rotation occurs.
34
+ def initialize(logdev, shift_age=0, shift_size=1048576)
35
+ @log = (logdev.kind_of?(::Logger) ? logdev : ::Logger.new(logdev, shift_age, shift_size))
36
+ @name = nil
37
+ end
38
+
39
+ # Attribute accessor/mutator declaration.
40
+ attr_accessor :name
41
+
42
+ # This method fetches the standard Ruby Logger instance contained within
43
+ # a Logger object.
44
+ def logger
45
+ @log
46
+ end
47
+
48
+ # This method updates the logger instance contained within a Logger
49
+ # object.
50
+ #
51
+ # ==== Parameters
52
+ # logger:: The object to set as the contained logger. This should be an
53
+ # instance of the standard Ruby Logger class or something
54
+ # compatible with this.
55
+ def logger=(logger)
56
+ @log = logger
57
+ end
58
+
59
+ # Aliases
60
+ alias :log :add
61
+ end
62
+ end
@@ -0,0 +1,33 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # Copyright (c), 2013 Peter Wood
4
+ # See the license.txt for details of the licensing of the code in this file.
5
+
6
+ class Object
7
+ # This method provides an instance level accessor to obtain a logger. Unless
8
+ # a name is specified the logger returned is the default one.
9
+ def log
10
+ LogJam.get_logger
11
+ end
12
+
13
+ # This method provides a class level accessor to obtain a logger. Unless a
14
+ # name is specified the logger returned is the default one.
15
+ def self.log
16
+ LogJam.get_logger
17
+ end
18
+
19
+ def self.log=(logger)
20
+ LogJam.get_logger.logger = logger
21
+ end
22
+
23
+ # This method allows a class to specify the name of the logger that it uses
24
+ # once, generally within the class definition.
25
+ #
26
+ # ==== Parameters
27
+ # name:: The name of the logger used by the class.
28
+ # context:: A Hash of additional parameters that are specific to the class
29
+ # to which LogJam is being applied.
30
+ def self.set_logger_name(name, context={})
31
+ LogJam.apply(self, name, context)
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module LogJam
2
- VERSION="1.0.0"
3
- end
2
+ VERSION="1.2.3"
3
+ end
metadata CHANGED
@@ -1,32 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logjam
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.2.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Black North
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-26 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: json
16
29
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
30
  requirements:
19
- - - ! '>='
31
+ - - "~>"
20
32
  - !ruby/object:Gem::Version
21
- version: '0'
33
+ version: '2.3'
22
34
  type: :runtime
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
- - - ! '>='
38
+ - - "~>"
28
39
  - !ruby/object:Gem::Version
29
- version: '0'
40
+ version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: configurative
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
30
55
  description: LogJam is a library to simplify the use of logging across libraries and
31
56
  applications.
32
57
  email: ruby@blacknorth.com
@@ -34,35 +59,35 @@ executables: []
34
59
  extensions: []
35
60
  extra_rdoc_files: []
36
61
  files:
37
- - lib/logjam/version.rb
38
- - lib/logjam/logjam_logger.rb
39
- - lib/logjam/logjam.rb
40
- - lib/logjam/exceptions.rb
62
+ - README.md
41
63
  - lib/logjam.rb
64
+ - lib/logjam/configuration.rb
65
+ - lib/logjam/exceptions.rb
66
+ - lib/logjam/logger.rb
67
+ - lib/logjam/object.rb
68
+ - lib/logjam/version.rb
42
69
  - license.txt
43
- - README
44
70
  homepage: https://github.com/free-beer/LogJam
45
- licenses: []
46
- post_install_message:
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
47
75
  rdoc_options: []
48
76
  require_paths:
49
77
  - lib
50
78
  required_ruby_version: !ruby/object:Gem::Requirement
51
- none: false
52
79
  requirements:
53
- - - ! '>='
80
+ - - ">="
54
81
  - !ruby/object:Gem::Version
55
82
  version: '0'
56
83
  required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
84
  requirements:
59
- - - ! '>='
85
+ - - ">="
60
86
  - !ruby/object:Gem::Version
61
87
  version: '0'
62
88
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 1.8.24
65
- signing_key:
66
- specification_version: 3
89
+ rubygems_version: 3.1.2
90
+ signing_key:
91
+ specification_version: 4
67
92
  summary: A library to aggregate logging.
68
93
  test_files: []
data/README DELETED
@@ -1,321 +0,0 @@
1
- LogJam
2
- ======
3
- LogJam is a library that attempts to allow for the aggregation the distributing
4
- logger facilities across a range of classes. Goals in creating this library
5
- were...
6
-
7
- * Easy of use. Fall back on defaults as much as possible and allow the
8
- functionality to be integrated and used with the least amount of work.
9
-
10
- * Flexibility. After easy of use is taken into consideration it should be
11
- possible to use the library in a more advanced fashion if that is called
12
- for.
13
-
14
- * Minimize the code to use it. It shouldn't require a great deal of code to
15
- deploy or use the facilities and there should be no code required to pass
16
- entities such as loggers around.
17
-
18
- * Usable in libraries. I found myself writing a lot of common logging code
19
- when writing libraries and application and wanted to abstract that out. I
20
- wanted to minimize the burden this placed on library users at the same
21
- time.
22
-
23
- Configuration & Setup
24
- ---------------------
25
- The LogJam library is configured through a call to the configure() method on the
26
- LogJam module. This method accepts a single parameter that may be either a
27
- String, a Hash, an IO object or nil.
28
-
29
- If you pass a String in this is expected to contain the path and name of a file
30
- that contains the logging configuration. Logging configuration files can be
31
- provided in either YAML or JSON format. If you pass an IO object this is
32
- expected to be the source of the configuration and, again, this configuration
33
- should be in either YAML or JSON format. In either case, where a String or IO
34
- is specified, the data read must be translatable into a Hash, which brings us
35
- to the other type that the configure() method accepts as a parameter.
36
-
37
- A Hash passed to the configure() method is expected to contain a set of values
38
- that can be converted into a logging set up. Passing an empty Hash will result
39
- in set up containing a single, universal logger attached to the standard output
40
- stream being created and used by all classes that have LogJam functionality.
41
-
42
- The Hash passed to the configure() method can contain two keys that the library
43
- will recognise and use. The first of these is 'loggers', in either Symbol or
44
- String form. The value under the loggers key is expected to be an Array
45
- containing zero or more logger definitions. A logger definition is a Hash in
46
- which the following keys are recognised (either as Strings or Symbols)...
47
-
48
- Key Description
49
- ----------------------- --------------------------------------------------
50
- default A boolean indicating whether this logger is the
51
- default (i.e. the one to be used when no other
52
- fits the bill). Only one logger should be declared
53
- as a default.
54
- datetime_format The date/time format to be used by the logger. See
55
- the documentation for the standard Ruby Logger
56
- class for more details.
57
- file The path and name of the file that logging details
58
- will be written to. Two special values are
59
- recognised in this value. STDOUT and STDERR are
60
- translated to mean the standard output or error
61
- streams respectively.
62
- level The logging level to set on the logger. If not
63
- explicitly specified this defaults to DEBUG.
64
- max_size When rotation is set to an integer value this value
65
- can be set to indicate the maximum permitted file
66
- size for a log file.
67
- name The name to associate with the logger. This allows
68
- loggers to be tied to classes or for the creation
69
- of aliases that tie multiple names to a single
70
- logger. Note that you should always use Strings
71
- (and not Symbols) when specifying aliases.
72
- rotation The frequency with which the log file is rotated.
73
- This may be an integer to indicate how many old log
74
- files are retained or may be a String such as
75
- "daily", "weekly" or "monthly".
76
-
77
- A note on logger names. Logger names (including alias names) aren't hierarchical
78
- and should be unique.
79
-
80
- The second key recognised in the configuration Hash is 'aliases', again as
81
- either a String or Symbol. The value under the aliases key is expected to be a
82
- Hash that maps String keys to String values. The key values are expected to be
83
- alias names and the mapped values are expected to be the name of a logger
84
- declared in the logger section. An entry in the aliases Hash creates an alias
85
- for an existing logger under a different name.
86
-
87
- If you pass nil to the configure method it behaves in a slightly different
88
- fashion. In this case the method searches for a configuration file that it can
89
- use given a default set of files names (basically logging.yaml, logging.yml and
90
- logging.json in the current working directory and in a subdirectory of the
91
- current working directory called config). The first of these files that it finds
92
- it attempts to use as configuration for the logging set up. If it doesn't find
93
- a configuration file then this type of call becomes equivalent to passing an
94
- empty Hash to the configure() method.
95
-
96
- See the end of this document for some example configurations.
97
-
98
- Logging With The Library
99
- ------------------------
100
- The stated goals of the LogJam library are to avoid the need to pass Logger
101
- instances around while still allowing potentially complex configuration with a
102
- minimum of code. The first step in this process has been covered in the
103
- Configuration & Setup section in which it's explained how to configure logging
104
- from a single Hash or file. This section will provide details on how to deploy
105
- loggers to various classes.
106
-
107
- The LogJam library works by providing an extension to any class that uses it
108
- that provides access to the logger to be used by the class. To make use of this
109
- functionality a class must call the apply() method of the LogJam module. A
110
- typical call to this method might look like...
111
-
112
- LogJam.apply(self, "my_logger")
113
-
114
- This line would appear somewhere inside the definition for the class that will
115
- use logging. The first parameter to the call is the class that is to be extended
116
- with the LogJam functionality. The second parameter is the name of the logger
117
- that the class will use. Note that this parameter is optional and, if not
118
- specified or if a matching logger does not exist, the class will fall back in
119
- using the default logger.
120
-
121
- Once this line has been added to the class definition it will cause the class to
122
- be extended with three methods - two class level methods (one called log() and
123
- one called log=()) and an instance level method called log(). The log() methods
124
- retrieve the Logger instance to be used by the class instances. The log=()
125
- method allows the Logger instance associated with a class to be altered. Note
126
- the instance level log() method will only be added if an existing method with
127
- that name does not already exist on the class.
128
-
129
- The following complete (although contrived) example gives an overview of how
130
- this would work...
131
-
132
- require 'rubygems'
133
- require 'logjam'
134
-
135
- class Writer
136
- LogJam.apply(self, "echo")
137
-
138
- def initialize(stream=STDOUT)
139
- @stream = stream
140
- end
141
-
142
- def echo(message)
143
- Writer.log.debug("Echoed: #{message}")
144
- @stream.puts message
145
- end
146
- end
147
-
148
- begin
149
- LogJam.configure({:loggers => [{:name => "echo",
150
- :file => "echo.log"}]})
151
-
152
- writer = Writer.new
153
- writer.echo "This is a string containing my message."
154
- rescue => error
155
- puts "ERROR: #{error}\n" + error.backtrace.join("\n")
156
- end
157
-
158
- In this example we create a Writer class that can echo a String on a stream. In
159
- doing this it also logs the message echoed at the debug level. We use the
160
- LogJam.apply() method to extend this class with logging capabilities so that, in
161
- the echo() method, we can simply call Writer.log() to get the class logger.
162
-
163
- In the later section of the code we configure the LogJam system to have a single
164
- Logger that writes to the echo.log file. We then create a Writer instance and
165
- use it to write a simple message. This message should appear on the standard
166
- output stream and be logged to the echo.log file.
167
-
168
- Advanced Usage
169
- --------------
170
- The hope would be that this library can be used in the creation of other
171
- libraries and allow for control of the logging generated by those libraries
172
- without having to dig into the workings of the library or to pass around Logger
173
- instances are constructor parameters or static data. In this case I recommend
174
- explicitly declaring logger names when using the apply() method and making the
175
- name that the library uses available with the library documentation so that the
176
- libraries logging can be switched off or on as needed.
177
-
178
- It's intended that, in general, the configure() method on the LogJam module
179
- should only be called once. Calling it a second time will clear all existing
180
- logging configuration and set up. This may or may not be an issue depending on
181
- whether you decide to cache logger inside class instances instead of always
182
- accessing them through the class level accessor.
183
-
184
- The Logger instance returned from a LogJam are intended to be fully compatible
185
- with the class defined within the standard Ruby Logger library. If you need to
186
- change elements, such as the formatter, you should just do so on the logger in
187
- the normal fashion. If you define multiple Logger instances then you will have
188
- to change each individually.
189
-
190
- Using the log=() method that is added to each class by the LogJam facilities it
191
- is possible to change the Logger being used. If you want to use this method
192
- please note that changing a Logger that is created via an alias will change the
193
- original Logger and thereby affect all classes that make use of that Logger (and
194
- not necessarily just the one making the change). If you want to do this give the
195
- class it's own logger instance.
196
-
197
- Finally, any logger can be fetched from the library using it's name and making
198
- a call to the LogJam.get_logger() method. Note if you omit the name or pass in
199
- nil you will retrieve the libraries default logger.
200
-
201
- Example Configurations
202
- ----------------------
203
- This section contains some example configurations. A short explanation is given
204
- for each configuration and then the configuration itself in Hash, YAML and JSON
205
- formats is provided.
206
-
207
- This represents the most basic configuration possible. In passing an empty Hash
208
- to the configure method the system creates a single, default logger that writes
209
- everything on the standard output stream...
210
-
211
- Hash
212
- {}
213
-
214
- YAML
215
- ---
216
- {}
217
-
218
- JSON
219
- {}
220
-
221
- The following simple configuration writes all logging output to a file called
222
- application.log in the current working directory. If a logging level is not
223
- explicitly specified then DEBUG is the default...
224
-
225
- Hash
226
- {:loggers => [{:default => true, :file => "application.log"}]}
227
-
228
- YAML
229
- ---
230
- :loggers:
231
- - :default: true
232
- :file: application.log
233
-
234
- JSON
235
- {"loggers": {"default": true, "file": "application.log"}}
236
-
237
- This configuration declares two loggers. The first is called 'silent' and will
238
- log nothing. The silent logger is the default and so will be used for any class
239
- that doesn't have an explicitly named logger. The second is called 'verbose' and
240
- logs everything from the debug level up on the standard output stream. The
241
- configuration also declares an alias pointing the name 'database' to refer to
242
- the verbose logger. An class that declares it uses the 'database' logger will
243
- generate output while all others will be silenced.
244
-
245
- Hash
246
- {:loggers => [{:default => true,
247
- :file => "STDOUT",
248
- :level => "UNKNOWN",
249
- :name => "silent"},
250
- {:file => "STDOUT",
251
- :name => "verbose"}],
252
- :aliases => {"database" => "verbose"}}
253
-
254
- YAML
255
- ---
256
- :loggers:
257
- - :default: true
258
- :file: STDOUT
259
- :level: UNKNOWN
260
- :name: silent
261
- - :file: STDOUT
262
- :name: verbose
263
- :aliases:
264
- database: verbose
265
-
266
- JSON
267
- {"loggers": [{"default":true,
268
- "file": "STDOUT",
269
- "level": "UNKNOWN",
270
- "name": "silent"},
271
- {"file": "STDOUT",
272
- "name": "verbose"}],
273
- "aliases": {"database":"verbose"}}
274
-
275
- The following configuration can be used as an example of how to drive logging
276
- from different parts of the code to different destinations. The configuration
277
- declares two loggers which deliver their output to two different log files and
278
- then declares aliases for those loggers that can be used to divide up the
279
- logging coming from different areas of the code.
280
-
281
- Hash
282
- {:loggers => [{:default => true,
283
- :file => "./log/main.log",
284
- :name => "main"},
285
- {:file => "./log/secondary.log",
286
- :name => "secondary"}],
287
- :aliases => {"database" => "secondary",
288
- "model" => "secondary",
289
- "controller" => "main"}}
290
-
291
- YAML
292
- ---
293
- :loggers:
294
- - :default: true
295
- :file: ./log/main.log
296
- :name: main
297
- - :file: ./log/secondary.log
298
- :name: secondary
299
- :aliases:
300
- database: secondary
301
- model: secondary
302
- controller: main
303
-
304
- JSON
305
- {"loggers": [{"default":true,
306
- "file": "./log/main.log",
307
- "name": "main"},
308
- {"file": "./log/secondary.log",
309
- "name": "secondary"}],
310
- "aliases": {"database":"secondary",
311
- "model": "secondary",
312
- "controller": "main"}}
313
-
314
- Testing
315
- -------
316
- LogJam uses the test-unit Ruby library for testing. The best approach to running
317
- the tests are to create a new gemset (assuming you're using RVM), do a bundle
318
- install on this gemset from within the LogJam root directory and then use a
319
- command such as the following to run the tests...
320
-
321
- $> ruby -I./lib -I./test ./test/unit/test_logjam.rb