log_switch 1.0.0 → 1.1.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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +133 -0
- data/Gemfile +3 -2
- data/LICENSE +24 -0
- data/README.md +142 -0
- data/Rakefile +28 -15
- data/lib/log_switch/version.rb +1 -1
- data/lib/log_switch.rb +10 -3
- data/log_switch.gemspec +33 -32
- data/spec/log_switch_spec.rb +35 -33
- data/spec/spec_helper.rb +5 -13
- metadata +47 -49
- data/.gemtest +0 -1
- data/History.rdoc +0 -67
- data/README.rdoc +0 -192
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ecedbe5e49ac7e09fd71760ccec36aedf1270f72029101fffd3219d4447da256
|
|
4
|
+
data.tar.gz: c2968035a4838d42cb787439f3ba65497666105d4ba04c163e3983f2cdbb7c43
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e293009d9babdd7ba1236a18679e44faf3158bcc286215ddebb3de51e3ea29c3a27c9e38e3d1e8cb832d772ad839ef6fdf1dce864060f08088e51483afaeb64a
|
|
7
|
+
data.tar.gz: 1ca8b618a1419224bd546c2c301a60e54a58727577569e8369b7b61f1686f37dfcf9b64e05a703275d1428e5a593718d2be98a7bd735dfdf29920a7b78a33dcd
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
6
|
+
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [1.1.0] - 2026-07-15
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Declared the license (Unlicense) in the gemspec; the LICENSE file now ships with the gem.
|
|
15
|
+
- The gemspec now declares a `changelog_uri`, so rubygems.org links to the changelog. It points at
|
|
16
|
+
the released version's tag rather than a branch, so each release's link is immutable.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- The gemspec now requires Ruby >= 3.3. Older Rubies will continue to resolve to 1.0.0.
|
|
21
|
+
- Replaced the tailor linter with RuboCop, and the dead Travis config with GitHub Actions, testing
|
|
22
|
+
Ruby 3.3, 3.4 and 4.0.
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
|
|
26
|
+
- Declared `logger` as a runtime dependency. `lib/log_switch.rb` has always required it, but the
|
|
27
|
+
gemspec never declared it — so under Bundler on Ruby 4.0, where `logger` is no longer a default
|
|
28
|
+
gem, requiring `log_switch` raised LoadError.
|
|
29
|
+
|
|
30
|
+
## [1.0.0] - 2014-10-10
|
|
31
|
+
|
|
32
|
+
### Added
|
|
33
|
+
|
|
34
|
+
- LogSwitch can now be included in your own module, then that included in other modules. This allows
|
|
35
|
+
toggling logging per includer.
|
|
36
|
+
|
|
37
|
+
### Removed
|
|
38
|
+
|
|
39
|
+
- Removed `LogSwitch::Mixin` in favor of simpler implementation.
|
|
40
|
+
|
|
41
|
+
### Fixed
|
|
42
|
+
|
|
43
|
+
- [gh-5](https://github.com/turboladen/log_switch/issues/5): The improvements listed above alleviate
|
|
44
|
+
this problem.
|
|
45
|
+
|
|
46
|
+
## 0.4.1 - 2013-01-04
|
|
47
|
+
|
|
48
|
+
### Fixed
|
|
49
|
+
|
|
50
|
+
- Initialize `@log` before calling it, thus removing warnings when running with `-w`.
|
|
51
|
+
|
|
52
|
+
## [0.4.0] - 2012-11-12
|
|
53
|
+
|
|
54
|
+
### Added
|
|
55
|
+
|
|
56
|
+
- [gh-4](https://github.com/turboladen/log_switch/issues/4): Added ability to toggle the prepending
|
|
57
|
+
of the class name to log messages. This lets you know which class/object is logging each message —
|
|
58
|
+
useful for when you have a number of different classes logging.
|
|
59
|
+
|
|
60
|
+
## [0.3.0] - 2012-02-03
|
|
61
|
+
|
|
62
|
+
### Added
|
|
63
|
+
|
|
64
|
+
- [gh-2](https://github.com/turboladen/log_switch/issues/2): Added ability to mix in
|
|
65
|
+
`LogSwitch::Mixin` to classes to allow calling `#log` in your class and have it delegate to your
|
|
66
|
+
singleton logger.
|
|
67
|
+
|
|
68
|
+
### Fixed
|
|
69
|
+
|
|
70
|
+
- [gh-3](https://github.com/turboladen/log_switch/issues/3): Fixed warning on `@before_block`.
|
|
71
|
+
|
|
72
|
+
## [0.2.0] - 2011-12-05
|
|
73
|
+
|
|
74
|
+
### Added
|
|
75
|
+
|
|
76
|
+
- Added ability to pass a block to `.log` to have code executed before logging.
|
|
77
|
+
- Added `before` hook to allow code to be executed every time log gets called (but before the
|
|
78
|
+
message actually gets logged).
|
|
79
|
+
|
|
80
|
+
### Fixed
|
|
81
|
+
|
|
82
|
+
- [gh-1](https://github.com/turboladen/log_switch/issues/1): Only log `#each_line` when that method
|
|
83
|
+
is supported on the object being logged.
|
|
84
|
+
|
|
85
|
+
## [0.1.4] - 2011-10-10
|
|
86
|
+
|
|
87
|
+
### Removed
|
|
88
|
+
|
|
89
|
+
- Removed gemspec enforcement of >= 1.9.2
|
|
90
|
+
|
|
91
|
+
## [0.1.3] - 2011-10-10
|
|
92
|
+
|
|
93
|
+
### Added
|
|
94
|
+
|
|
95
|
+
- Made 1.8.7 compatible.
|
|
96
|
+
|
|
97
|
+
### Removed
|
|
98
|
+
|
|
99
|
+
- Removed dev deps: metric_fu, code_statistics
|
|
100
|
+
|
|
101
|
+
## [0.1.2] - 2011-10-09
|
|
102
|
+
|
|
103
|
+
### Fixed
|
|
104
|
+
|
|
105
|
+
- Just realized author is supposed to be my real name. :)
|
|
106
|
+
|
|
107
|
+
## [0.1.1] - 2011-10-09
|
|
108
|
+
|
|
109
|
+
### Fixed
|
|
110
|
+
|
|
111
|
+
- Fixed author field in .gemspec
|
|
112
|
+
|
|
113
|
+
## [0.1.0] - 2011-10-07
|
|
114
|
+
|
|
115
|
+
### Added
|
|
116
|
+
|
|
117
|
+
- `require` and `extend` to mix in to your class/module to get a single point of logging
|
|
118
|
+
- Switch on/off logging
|
|
119
|
+
- Use whatever Logger you want
|
|
120
|
+
|
|
121
|
+
<!-- 0.4.1 is intentionally unlinked: it was never tagged or released to rubygems. -->
|
|
122
|
+
|
|
123
|
+
[unreleased]: https://github.com/turboladen/log_switch/compare/v1.1.0...HEAD
|
|
124
|
+
[1.1.0]: https://github.com/turboladen/log_switch/compare/v1.0.0...v1.1.0
|
|
125
|
+
[1.0.0]: https://github.com/turboladen/log_switch/compare/v0.4.0...v1.0.0
|
|
126
|
+
[0.4.0]: https://github.com/turboladen/log_switch/compare/v0.3.0...v0.4.0
|
|
127
|
+
[0.3.0]: https://github.com/turboladen/log_switch/compare/v0.2.0...v0.3.0
|
|
128
|
+
[0.2.0]: https://github.com/turboladen/log_switch/compare/v0.1.4...v0.2.0
|
|
129
|
+
[0.1.4]: https://github.com/turboladen/log_switch/compare/v0.1.3...v0.1.4
|
|
130
|
+
[0.1.3]: https://github.com/turboladen/log_switch/compare/v0.1.2...v0.1.3
|
|
131
|
+
[0.1.2]: https://github.com/turboladen/log_switch/compare/v0.1.1...v0.1.2
|
|
132
|
+
[0.1.1]: https://github.com/turboladen/log_switch/compare/v0.1.0...v0.1.1
|
|
133
|
+
[0.1.0]: https://github.com/turboladen/log_switch/releases/tag/v0.1.0
|
data/Gemfile
CHANGED
data/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
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 NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# log_switch
|
|
2
|
+
|
|
3
|
+
<https://github.com/turboladen/log_switch>
|
|
4
|
+
|
|
5
|
+
## Description
|
|
6
|
+
|
|
7
|
+
While developing other gems that needed a single class/singleton style logger, I got tired of
|
|
8
|
+
repeating the code to create that logger and mix it in to my base class. I just wanted to require
|
|
9
|
+
something, mix it in, and log:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
MyThing.new.log "some message"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
...and be able to switch that logging off programmatically:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
MyThing.logging_enabled = false
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This gem does that.
|
|
22
|
+
|
|
23
|
+
## Synopsis
|
|
24
|
+
|
|
25
|
+
### Basic use
|
|
26
|
+
|
|
27
|
+
`include LogSwitch` to give your class a class-level logger and an instance-level `#log` method.
|
|
28
|
+
**Logging is off by default** — turn it on explicitly:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
require 'log_switch'
|
|
32
|
+
|
|
33
|
+
class MyThing
|
|
34
|
+
include LogSwitch
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
MyThing.logging_enabled = true
|
|
38
|
+
MyThing.new.log "I like you, Ruby."
|
|
39
|
+
# => D, [2026-07-15T12:46:30.206465 #66434] DEBUG -- : <MyThing> I like you, Ruby.
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Switch it back off and calls become no-ops:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
MyThing.logging_enabled = false
|
|
46
|
+
MyThing.new.log "You're my favorite." # => nothing is written
|
|
47
|
+
|
|
48
|
+
MyThing.logging_enabled = true # ...and back on, for the examples below
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Note the class name is prepended to each message. That is on by default.
|
|
52
|
+
|
|
53
|
+
### Log levels
|
|
54
|
+
|
|
55
|
+
The default level is `:debug`. Change it with `default_log_level`:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
MyThing.default_log_level = :warn
|
|
59
|
+
MyThing.new.log "Crap!"
|
|
60
|
+
# => W, [2026-07-15T12:46:30.206563 #66434] WARN -- : <MyThing> Crap!
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Or pass a level per call:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
MyThing.new.log "Stuff!", :info
|
|
67
|
+
# => I, [2026-07-15T12:46:30.206499 #66434] INFO -- : <MyThing> Stuff!
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The level is passed straight through to your Logger, so any method it responds to will do.
|
|
71
|
+
|
|
72
|
+
### Using your own Logger
|
|
73
|
+
|
|
74
|
+
`LogSwitch` defaults to a `Logger` writing to `STDOUT`. Point it anywhere:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
MyThing.logger = Logger.new('log.txt')
|
|
78
|
+
MyThing.new.log "hi!"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`LogSwitch.logger` sets the default for includers that have not set their own, and
|
|
82
|
+
`LogSwitch.reset_config!` puts things back to a fresh `STDOUT` logger.
|
|
83
|
+
|
|
84
|
+
### Multi-line messages
|
|
85
|
+
|
|
86
|
+
Anything responding to `#each_line` is logged one line per call, so a multi-line message stays
|
|
87
|
+
readable:
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
MyThing.new.log "line one\nline two"
|
|
91
|
+
# => D, [...] DEBUG -- : <MyThing> line one
|
|
92
|
+
# => D, [...] DEBUG -- : <MyThing> line two
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Objects that do not respond to `#each_line` (an Array, an Exception) are logged whole.
|
|
96
|
+
|
|
97
|
+
### Hooks
|
|
98
|
+
|
|
99
|
+
Pass a block to `#log` and it runs before the message is written — handy for one-off setup:
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
require 'fileutils'
|
|
103
|
+
log_directory = File.expand_path('log')
|
|
104
|
+
|
|
105
|
+
MyThing.new.log("Thanks brah!") do
|
|
106
|
+
FileUtils.mkdir_p(log_directory)
|
|
107
|
+
end
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
For something that should run before _every_ call, use `before_log`:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
MyThing.before_log = proc { FileUtils.mkdir_p(log_directory) }
|
|
114
|
+
MyThing.new.log "Thanks brah!" # runs the hook, then logs
|
|
115
|
+
MyThing.new.log "I'm hungry..." # runs it again
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Requirements
|
|
119
|
+
|
|
120
|
+
- Ruby >= 3.3. Tested on 3.3, 3.4 and 4.0 (see `.github/workflows/ci.yml`).
|
|
121
|
+
- Runtime dependency: `logger` (no longer a default gem as of Ruby 4.0).
|
|
122
|
+
|
|
123
|
+
## Install
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
gem install log_switch
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Developers
|
|
130
|
+
|
|
131
|
+
After checking out the source:
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
bundle install
|
|
135
|
+
bundle exec rake # specs + RuboCop
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Thanks
|
|
139
|
+
|
|
140
|
+
I need to thank the [savon](https://github.com/savonrb/savon) project for most of the code here.
|
|
141
|
+
Somehow I ran across how they do logging and started following suit. The code in `log_switch` is
|
|
142
|
+
almost identical to Savon's logging.
|
data/Rakefile
CHANGED
|
@@ -1,29 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'bundler/gem_tasks'
|
|
2
4
|
require 'rspec/core/rake_task'
|
|
3
5
|
require 'yard'
|
|
6
|
+
require 'rubocop/rake_task'
|
|
4
7
|
|
|
5
8
|
YARD::Rake::YardocTask.new do |t|
|
|
6
|
-
t.files = %w
|
|
7
|
-
t.options = %W
|
|
8
|
-
t.options += %w
|
|
9
|
+
t.files = %w[lib/**/*.rb - CHANGELOG.md]
|
|
10
|
+
t.options = %W[--title log_switch Documentation (#{LogSwitch::VERSION})]
|
|
11
|
+
t.options += %w[--main README.md]
|
|
9
12
|
end
|
|
10
13
|
|
|
11
14
|
RSpec::Core::RakeTask.new do |t|
|
|
12
|
-
t.ruby_opts = %w
|
|
15
|
+
t.ruby_opts = %w[-w]
|
|
13
16
|
end
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
RuboCop::RakeTask.new
|
|
19
|
+
|
|
20
|
+
# RuboCop covers Ruby; dprint covers the Markdown and YAML. It is a binary
|
|
21
|
+
# rather than a gem, so `bundle install` cannot supply it -- fail loudly with
|
|
22
|
+
# somewhere to go rather than skipping, which would make the gate a no-op
|
|
23
|
+
# exactly when nobody notices.
|
|
24
|
+
desc 'Check non-Ruby formatting with dprint'
|
|
25
|
+
task :dprint do
|
|
26
|
+
abort <<~MSG unless system('command -v dprint > /dev/null 2>&1')
|
|
27
|
+
dprint is not installed, so Markdown/YAML formatting was not checked.
|
|
28
|
+
Install it (https://dprint.dev/install/) -- e.g. `brew install dprint` --
|
|
29
|
+
or run `rake spec rubocop` to skip this deliberately.
|
|
30
|
+
MSG
|
|
17
31
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
task.file_set 'spec/**/*.rb', :specs
|
|
21
|
-
end
|
|
32
|
+
sh 'dprint check'
|
|
33
|
+
end
|
|
22
34
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
task :test => :spec
|
|
35
|
+
desc 'Format non-Ruby files with dprint'
|
|
36
|
+
task 'dprint:fmt' do
|
|
37
|
+
sh 'dprint fmt'
|
|
27
38
|
end
|
|
28
39
|
|
|
29
|
-
task :
|
|
40
|
+
task test: %i[spec rubocop dprint]
|
|
41
|
+
|
|
42
|
+
task default: :test
|
data/lib/log_switch/version.rb
CHANGED
data/lib/log_switch.rb
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
require 'logger'
|
|
2
2
|
require_relative 'log_switch/version'
|
|
3
3
|
|
|
4
|
-
# LogSwitch
|
|
5
|
-
#
|
|
6
|
-
# +README.rdoc+ for more info.
|
|
4
|
+
# LogSwitch mixes a logger into a class/module and, most importantly, allows for
|
|
5
|
+
# turning off logging programmatically. See README.md for more info.
|
|
7
6
|
module LogSwitch
|
|
8
7
|
def self.included(base)
|
|
9
8
|
@includers ||= []
|
|
@@ -39,6 +38,10 @@ module LogSwitch
|
|
|
39
38
|
end
|
|
40
39
|
|
|
41
40
|
module ClassMethods
|
|
41
|
+
# TODO: broken -- the class variables below are declared in this module body,
|
|
42
|
+
# so they live on ClassMethods itself and every includer shares one slot:
|
|
43
|
+
# `A.logging_enabled = true` also enables B. Intent is per-includer config.
|
|
44
|
+
|
|
42
45
|
# @param value [Boolean]
|
|
43
46
|
def logging_enabled
|
|
44
47
|
@@logging_enabled ||= false
|
|
@@ -74,6 +77,8 @@ module LogSwitch
|
|
|
74
77
|
log_class_name
|
|
75
78
|
end
|
|
76
79
|
|
|
80
|
+
# TODO: broken -- `||=` against a truthy default overwrites a stored `false`
|
|
81
|
+
# on the next read, so `log_class_name = false` never sticks.
|
|
77
82
|
def log_class_name
|
|
78
83
|
@@log_class_name ||= true
|
|
79
84
|
end
|
|
@@ -98,6 +103,8 @@ module LogSwitch
|
|
|
98
103
|
#
|
|
99
104
|
# @param [Proc] block The block of code to execute before logging a message
|
|
100
105
|
# with {#log}.
|
|
106
|
+
# TODO: broken -- `||=` keeps only the first assignment, and #before_log
|
|
107
|
+
# initialises the slot on first read, so later hooks are silently ignored.
|
|
101
108
|
def before_log=(block)
|
|
102
109
|
@@before_block ||= block
|
|
103
110
|
end
|
data/log_switch.gemspec
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
2
4
|
require 'log_switch/version'
|
|
3
5
|
|
|
4
6
|
Gem::Specification.new do |s|
|
|
5
|
-
s.name =
|
|
7
|
+
s.name = 'log_switch'
|
|
6
8
|
s.version = LogSwitch::VERSION
|
|
7
|
-
s.authors = [
|
|
8
|
-
s.homepage =
|
|
9
|
-
s.email = %w
|
|
10
|
-
s.summary =
|
|
11
|
-
turned on and off.
|
|
12
|
-
s.description =
|
|
13
|
-
easily be turned on and off.
|
|
14
|
-
|
|
15
|
-
s.
|
|
16
|
-
s.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
s.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
s.add_development_dependency
|
|
9
|
+
s.authors = ['Steve Loveless']
|
|
10
|
+
s.homepage = 'https://github.com/turboladen/log_switch'
|
|
11
|
+
s.email = %w[steve.loveless@gmail.com]
|
|
12
|
+
s.summary = 'Extends a class for singleton style logging that can easily be
|
|
13
|
+
turned on and off.'
|
|
14
|
+
s.description = 'Extends a class for singleton style logging that can
|
|
15
|
+
easily be turned on and off.'
|
|
16
|
+
|
|
17
|
+
s.license = 'Unlicense'
|
|
18
|
+
s.required_ruby_version = '>= 3.3'
|
|
19
|
+
|
|
20
|
+
s.metadata = {
|
|
21
|
+
'source_code_uri' => 'https://github.com/turboladen/log_switch',
|
|
22
|
+
'changelog_uri' => "https://github.com/turboladen/log_switch/blob/v#{LogSwitch::VERSION}/CHANGELOG.md",
|
|
23
|
+
'rubygems_mfa_required' => 'true'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
s.files = Dir.glob('{lib,spec}/**/*') +
|
|
27
|
+
%w[CHANGELOG.md Gemfile LICENSE README.md log_switch.gemspec Rakefile]
|
|
28
|
+
|
|
29
|
+
s.require_paths = %w[lib]
|
|
30
|
+
|
|
31
|
+
s.add_dependency 'logger', '~> 1.5'
|
|
32
|
+
|
|
33
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
|
34
|
+
s.add_development_dependency 'rspec', '~> 3.13'
|
|
35
|
+
s.add_development_dependency 'rubocop', '~> 1.88'
|
|
36
|
+
s.add_development_dependency 'simplecov', '~> 1.0'
|
|
37
|
+
s.add_development_dependency 'yard', '~> 0.9'
|
|
36
38
|
end
|
|
37
|
-
|
data/spec/log_switch_spec.rb
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'spec_helper'
|
|
2
4
|
|
|
3
|
-
class IncluderClass; include LogSwitch; end
|
|
5
|
+
class IncluderClass; include LogSwitch; end
|
|
4
6
|
|
|
5
7
|
describe LogSwitch do
|
|
6
8
|
before { LogSwitch.reset_config! }
|
|
7
|
-
specify { expect(LogSwitch::VERSION).to eq '1.
|
|
9
|
+
specify { expect(LogSwitch::VERSION).to eq '1.1.0' }
|
|
8
10
|
|
|
9
11
|
describe 'base class methods' do
|
|
10
|
-
describe
|
|
11
|
-
it
|
|
12
|
-
expect(described_class.instance_variable_get(:@includers))
|
|
13
|
-
to include(IncluderClass)
|
|
12
|
+
describe '.included' do
|
|
13
|
+
it 'sets @includers on the class object that extended' do
|
|
14
|
+
expect(described_class.instance_variable_get(:@includers))
|
|
15
|
+
.to include(IncluderClass)
|
|
14
16
|
end
|
|
15
17
|
end
|
|
16
18
|
|
|
17
|
-
describe
|
|
18
|
-
it
|
|
19
|
+
describe '.logger' do
|
|
20
|
+
it 'is a Logger by default' do
|
|
19
21
|
expect(described_class.logger).to be_a Logger
|
|
20
22
|
end
|
|
21
23
|
end
|
|
@@ -24,7 +26,7 @@ describe LogSwitch do
|
|
|
24
26
|
describe IncluderClass do
|
|
25
27
|
subject { IncluderClass.new }
|
|
26
28
|
|
|
27
|
-
describe
|
|
29
|
+
describe '.logging_enabled?' do
|
|
28
30
|
specify { expect(described_class).to respond_to(:logging_enabled?) }
|
|
29
31
|
|
|
30
32
|
it 'defaults to false' do
|
|
@@ -37,23 +39,23 @@ describe LogSwitch do
|
|
|
37
39
|
end
|
|
38
40
|
end
|
|
39
41
|
|
|
40
|
-
describe
|
|
41
|
-
it
|
|
42
|
+
describe '.logger' do
|
|
43
|
+
it 'is a Logger by default' do
|
|
42
44
|
expect(described_class.logger).to be_a Logger
|
|
43
45
|
end
|
|
44
46
|
end
|
|
45
47
|
|
|
46
|
-
describe
|
|
47
|
-
it
|
|
48
|
+
describe '.default_log_level' do
|
|
49
|
+
it 'defaults to :debug' do
|
|
48
50
|
expect(described_class.default_log_level).to eq :debug
|
|
49
51
|
end
|
|
50
52
|
end
|
|
51
53
|
|
|
52
|
-
describe
|
|
54
|
+
describe '.log_class_name?' do
|
|
53
55
|
specify { expect(described_class).to respond_to(:log_class_name?) }
|
|
54
56
|
end
|
|
55
57
|
|
|
56
|
-
describe
|
|
58
|
+
describe '.log_class_name' do
|
|
57
59
|
specify { expect(described_class).to respond_to(:log_class_name) }
|
|
58
60
|
|
|
59
61
|
it 'defaults to true' do
|
|
@@ -67,16 +69,16 @@ describe LogSwitch do
|
|
|
67
69
|
end
|
|
68
70
|
end
|
|
69
71
|
|
|
70
|
-
describe
|
|
71
|
-
it
|
|
72
|
-
described_class.before_log =
|
|
72
|
+
describe '.before_log' do
|
|
73
|
+
it 'assigns the given block to @before_block' do
|
|
74
|
+
described_class.before_log = proc { "I'm a block" }
|
|
73
75
|
expect(described_class.before_log).to be_a Proc
|
|
74
76
|
end
|
|
75
77
|
end
|
|
76
78
|
|
|
77
|
-
describe
|
|
79
|
+
describe '#log' do
|
|
78
80
|
let(:logger) do
|
|
79
|
-
double
|
|
81
|
+
double 'Logger'
|
|
80
82
|
end
|
|
81
83
|
|
|
82
84
|
let(:object) do
|
|
@@ -89,36 +91,36 @@ describe LogSwitch do
|
|
|
89
91
|
# described_class.logger = logger
|
|
90
92
|
# end
|
|
91
93
|
|
|
92
|
-
it
|
|
93
|
-
#expect(logger).to receive(:send).with(:meow, "<IncluderClass> stuff")
|
|
94
|
-
subject.log(
|
|
94
|
+
it 'can log at any log level' do
|
|
95
|
+
# expect(logger).to receive(:send).with(:meow, "<IncluderClass> stuff")
|
|
96
|
+
subject.log('stuff', :meow)
|
|
95
97
|
end
|
|
96
98
|
|
|
97
|
-
it
|
|
98
|
-
allow(logger).to receive(:send).with(:debug,
|
|
99
|
+
it 'can take a block' do
|
|
100
|
+
allow(logger).to receive(:send).with(:debug, '<IncluderClass> hi')
|
|
99
101
|
expect(object).to receive(:test_in_block)
|
|
100
102
|
subject.log('hi') { object.test_in_block }
|
|
101
103
|
end
|
|
102
104
|
|
|
103
|
-
context
|
|
105
|
+
context 'with .before_log' do
|
|
104
106
|
it "calls the @before_block if that's set" do
|
|
105
|
-
allow(logger).to receive(:send).with(:debug,
|
|
106
|
-
described_class.before_log =
|
|
107
|
+
allow(logger).to receive(:send).with(:debug, '<IncluderClass> hi')
|
|
108
|
+
described_class.before_log = proc { puts 'This is also before' }
|
|
107
109
|
expect(described_class.before_log).to receive(:call).once
|
|
108
110
|
subject.log 'hi'
|
|
109
111
|
end
|
|
110
112
|
end
|
|
111
113
|
|
|
112
|
-
context
|
|
113
|
-
it
|
|
114
|
-
array = [1, 'stuff', { :
|
|
114
|
+
context 'can log non-String objects' do
|
|
115
|
+
it 'an Array of various object types' do
|
|
116
|
+
array = [1, 'stuff', { two: 2 }]
|
|
115
117
|
message = "<IncluderClass> #{array}"
|
|
116
118
|
allow(logger).to receive(:send).with(:debug, message)
|
|
117
119
|
expect { subject.log array }.to_not raise_exception
|
|
118
120
|
end
|
|
119
121
|
|
|
120
|
-
it
|
|
121
|
-
ex = StandardError.new(
|
|
122
|
+
it 'an Exception' do
|
|
123
|
+
ex = StandardError.new('Test error.')
|
|
122
124
|
message = "<IncluderClass> #{ex.message}"
|
|
123
125
|
allow(logger).to receive(:send).with(:debug, message)
|
|
124
126
|
expect { subject.log ex }.to_not raise_exception
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
require 'simplecov'
|
|
1
|
+
# frozen_string_literal: true
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
def format(result)
|
|
6
|
-
SimpleCov::Formatter::HTMLFormatter.new.format(result)
|
|
7
|
-
end
|
|
8
|
-
end
|
|
3
|
+
require 'simplecov'
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
SimpleCov.start do
|
|
13
|
-
add_filter "/spec"
|
|
14
|
-
end
|
|
5
|
+
SimpleCov.start do
|
|
6
|
+
skip '/spec'
|
|
15
7
|
end
|
|
16
8
|
|
|
17
|
-
|
|
9
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib")
|
|
18
10
|
require 'log_switch'
|
metadata
CHANGED
|
@@ -1,121 +1,124 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: log_switch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Steve Loveless
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: logger
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - "~>"
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
20
|
-
type: :
|
|
18
|
+
version: '1.5'
|
|
19
|
+
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - "~>"
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '1.5'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: rake
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - "~>"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0'
|
|
32
|
+
version: '13.0'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - "~>"
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0'
|
|
39
|
+
version: '13.0'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: rspec
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
|
-
- - "
|
|
44
|
+
- - "~>"
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version:
|
|
46
|
+
version: '3.13'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
|
-
- - "
|
|
51
|
+
- - "~>"
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version:
|
|
53
|
+
version: '3.13'
|
|
55
54
|
- !ruby/object:Gem::Dependency
|
|
56
|
-
name:
|
|
55
|
+
name: rubocop
|
|
57
56
|
requirement: !ruby/object:Gem::Requirement
|
|
58
57
|
requirements:
|
|
59
|
-
- - "
|
|
58
|
+
- - "~>"
|
|
60
59
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '
|
|
60
|
+
version: '1.88'
|
|
62
61
|
type: :development
|
|
63
62
|
prerelease: false
|
|
64
63
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
64
|
requirements:
|
|
66
|
-
- - "
|
|
65
|
+
- - "~>"
|
|
67
66
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '
|
|
67
|
+
version: '1.88'
|
|
69
68
|
- !ruby/object:Gem::Dependency
|
|
70
|
-
name:
|
|
69
|
+
name: simplecov
|
|
71
70
|
requirement: !ruby/object:Gem::Requirement
|
|
72
71
|
requirements:
|
|
73
|
-
- - "
|
|
72
|
+
- - "~>"
|
|
74
73
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: 1.
|
|
74
|
+
version: '1.0'
|
|
76
75
|
type: :development
|
|
77
76
|
prerelease: false
|
|
78
77
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
78
|
requirements:
|
|
80
|
-
- - "
|
|
79
|
+
- - "~>"
|
|
81
80
|
- !ruby/object:Gem::Version
|
|
82
|
-
version: 1.
|
|
81
|
+
version: '1.0'
|
|
83
82
|
- !ruby/object:Gem::Dependency
|
|
84
83
|
name: yard
|
|
85
84
|
requirement: !ruby/object:Gem::Requirement
|
|
86
85
|
requirements:
|
|
87
|
-
- - "
|
|
86
|
+
- - "~>"
|
|
88
87
|
- !ruby/object:Gem::Version
|
|
89
|
-
version: 0.
|
|
88
|
+
version: '0.9'
|
|
90
89
|
type: :development
|
|
91
90
|
prerelease: false
|
|
92
91
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
92
|
requirements:
|
|
94
|
-
- - "
|
|
93
|
+
- - "~>"
|
|
95
94
|
- !ruby/object:Gem::Version
|
|
96
|
-
version: 0.
|
|
97
|
-
description:
|
|
98
|
-
|
|
95
|
+
version: '0.9'
|
|
96
|
+
description: |-
|
|
97
|
+
Extends a class for singleton style logging that can
|
|
98
|
+
easily be turned on and off.
|
|
99
99
|
email:
|
|
100
100
|
- steve.loveless@gmail.com
|
|
101
101
|
executables: []
|
|
102
102
|
extensions: []
|
|
103
103
|
extra_rdoc_files: []
|
|
104
104
|
files:
|
|
105
|
-
-
|
|
105
|
+
- CHANGELOG.md
|
|
106
106
|
- Gemfile
|
|
107
|
-
-
|
|
108
|
-
- README.
|
|
107
|
+
- LICENSE
|
|
108
|
+
- README.md
|
|
109
109
|
- Rakefile
|
|
110
110
|
- lib/log_switch.rb
|
|
111
111
|
- lib/log_switch/version.rb
|
|
112
112
|
- log_switch.gemspec
|
|
113
113
|
- spec/log_switch_spec.rb
|
|
114
114
|
- spec/spec_helper.rb
|
|
115
|
-
homepage:
|
|
116
|
-
licenses:
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
homepage: https://github.com/turboladen/log_switch
|
|
116
|
+
licenses:
|
|
117
|
+
- Unlicense
|
|
118
|
+
metadata:
|
|
119
|
+
source_code_uri: https://github.com/turboladen/log_switch
|
|
120
|
+
changelog_uri: https://github.com/turboladen/log_switch/blob/v1.1.0/CHANGELOG.md
|
|
121
|
+
rubygems_mfa_required: 'true'
|
|
119
122
|
rdoc_options: []
|
|
120
123
|
require_paths:
|
|
121
124
|
- lib
|
|
@@ -123,20 +126,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
123
126
|
requirements:
|
|
124
127
|
- - ">="
|
|
125
128
|
- !ruby/object:Gem::Version
|
|
126
|
-
version: '
|
|
129
|
+
version: '3.3'
|
|
127
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
131
|
requirements:
|
|
129
132
|
- - ">="
|
|
130
133
|
- !ruby/object:Gem::Version
|
|
131
|
-
version:
|
|
134
|
+
version: '0'
|
|
132
135
|
requirements: []
|
|
133
|
-
|
|
134
|
-
rubygems_version: 2.4.1
|
|
135
|
-
signing_key:
|
|
136
|
+
rubygems_version: 4.0.10
|
|
136
137
|
specification_version: 4
|
|
137
138
|
summary: Extends a class for singleton style logging that can easily be turned on
|
|
138
139
|
and off.
|
|
139
|
-
test_files:
|
|
140
|
-
- spec/log_switch_spec.rb
|
|
141
|
-
- spec/spec_helper.rb
|
|
142
|
-
has_rdoc:
|
|
140
|
+
test_files: []
|
data/.gemtest
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
data/History.rdoc
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
=== 1.0.0 / 2014-10-10
|
|
2
|
-
|
|
3
|
-
* Improvements:
|
|
4
|
-
* LogSwitch can now be included in your own module, then that included in
|
|
5
|
-
other modules. This allows toggling logging per includer.
|
|
6
|
-
* Other stuff:
|
|
7
|
-
* Removed LogSwitch::Mixin in favor of simpler implementation.
|
|
8
|
-
* Bug fixes:
|
|
9
|
-
* gh-5[https://github.com/turboladen/log_switch/issues/5]: The improvements
|
|
10
|
-
listed above alleviate this problem.
|
|
11
|
-
|
|
12
|
-
=== 0.4.1 / 2013-01-04
|
|
13
|
-
|
|
14
|
-
* Bug fixes:
|
|
15
|
-
* Initialize @log before calling it, thus removing warnings when running with
|
|
16
|
-
-w.
|
|
17
|
-
|
|
18
|
-
=== 0.4.0 / 2012-11-12
|
|
19
|
-
|
|
20
|
-
* Improvements:
|
|
21
|
-
* gh-4[https://github.com/turboladen/log_switch/issues/4]: Added ability to
|
|
22
|
-
toggle the prepending of the class name to log messages. This lets you
|
|
23
|
-
know which class/object is logging each message--useful for when you have
|
|
24
|
-
a number of different classes logging.
|
|
25
|
-
|
|
26
|
-
=== 0.3.0 / 2012-02-03
|
|
27
|
-
|
|
28
|
-
* Improvements:
|
|
29
|
-
* gh-2[https://github.com/turboladen/log_switch/issues/2]: Added ability to
|
|
30
|
-
mix in {LogSwitch::Mixin} to classes to allow calling +#log+ in your class
|
|
31
|
-
and have it delegate to your singleton logger.
|
|
32
|
-
* Bug fixes:
|
|
33
|
-
* gh-3[https://github.com/turboladen/log_switch/issues/3]: Fixed warning on
|
|
34
|
-
+@before_block+.
|
|
35
|
-
|
|
36
|
-
=== 0.2.0 / 2011-12-05
|
|
37
|
-
|
|
38
|
-
* Added ability to pass a block to +.log+ to have code executed before logging.
|
|
39
|
-
* Added +before+ hook to allow code to be executed every time log gets called
|
|
40
|
-
(but before the message actually gets logged).
|
|
41
|
-
* Bug fixes
|
|
42
|
-
* gh-1[https://github.com/turboladen/log_switch/issues/1]: Only log #each_line
|
|
43
|
-
when that method is supported on the object being logged.
|
|
44
|
-
|
|
45
|
-
=== 0.1.4 / 2011-10-10
|
|
46
|
-
|
|
47
|
-
* Removed gemspec enforcement of >= 1.9.2
|
|
48
|
-
|
|
49
|
-
=== 0.1.3 / 2011-10-10
|
|
50
|
-
|
|
51
|
-
* Made 1.8.7 compatible.
|
|
52
|
-
* Removed dev deps: metric_fu, code_statistics
|
|
53
|
-
|
|
54
|
-
=== 0.1.2 / 2011-10-09
|
|
55
|
-
|
|
56
|
-
* Just realized author is supposed to be my real name. :)
|
|
57
|
-
|
|
58
|
-
=== 0.1.1 / 2011-10-09
|
|
59
|
-
|
|
60
|
-
* Fixed author field in .gemspec
|
|
61
|
-
|
|
62
|
-
=== 0.1.0 / 2011-10-07
|
|
63
|
-
|
|
64
|
-
* Features:
|
|
65
|
-
* +require+ and +extend+ to mix in to your class/module to get a single point of logging
|
|
66
|
-
* Switch on/off logging
|
|
67
|
-
* Use whatever Logger you want
|
data/README.rdoc
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
= log_switch
|
|
2
|
-
|
|
3
|
-
http://github.com/turboladen/log_switch
|
|
4
|
-
|
|
5
|
-
== DESCRIPTION
|
|
6
|
-
|
|
7
|
-
While developing other gems that required a single class/singleton style logger,
|
|
8
|
-
I got tired of repeating the code to create that logger and mix it in to my base
|
|
9
|
-
class. I just wanted to be able to require something, then be able to do:
|
|
10
|
-
|
|
11
|
-
MyLib.log "some message"
|
|
12
|
-
|
|
13
|
-
I also wanted to be able to programmatically turn on/off logging by doing something
|
|
14
|
-
like:
|
|
15
|
-
|
|
16
|
-
MyLib.log = false
|
|
17
|
-
|
|
18
|
-
This gem allows just that. Well, almost...
|
|
19
|
-
|
|
20
|
-
== FEATURES/PROBLEMS
|
|
21
|
-
|
|
22
|
-
Features:
|
|
23
|
-
|
|
24
|
-
* +require+ and +extend+ to mix in to your class/module to get a single point of logging
|
|
25
|
-
* Switch on/off logging
|
|
26
|
-
* Use whatever Logger you want
|
|
27
|
-
|
|
28
|
-
== SYNOPSIS
|
|
29
|
-
|
|
30
|
-
=== Basic Use
|
|
31
|
-
|
|
32
|
-
Get your app logging with a single point of logging:
|
|
33
|
-
|
|
34
|
-
require 'log_switch'
|
|
35
|
-
|
|
36
|
-
class MyThing
|
|
37
|
-
extend LogSwitch
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
MyThing.log "I like you, Ruby." # => D, [2011-10-07T14:40:26.697084 #30080] DEBUG -- : I like you, Ruby.
|
|
41
|
-
|
|
42
|
-
...and then you can simply switch off logging by doing:
|
|
43
|
-
|
|
44
|
-
MyThing.log = false
|
|
45
|
-
MyThing.log "You're my favorite." # => No logging occurs!
|
|
46
|
-
|
|
47
|
-
By default, LogSwitch sets the log level to +:debug+. You can change the default
|
|
48
|
-
log level as you go:
|
|
49
|
-
|
|
50
|
-
MyThing.log_level = :warn
|
|
51
|
-
MyThing.log "Crap!" # => W, [2011-10-07T15:30:54.012502 #32892] WARN -- : Crap!
|
|
52
|
-
|
|
53
|
-
You can pass in the log level for your Logger type too:
|
|
54
|
-
|
|
55
|
-
MyThing.log "Stuff!", :info # => I, [2011-10-07T15:28:49.480741 #32892] INFO -- : Stuff!
|
|
56
|
-
MyThing.log "Meow", :fatal # => F, [2011-10-07T15:32:21.207867 #32892] FATAL -- : Meow
|
|
57
|
-
|
|
58
|
-
If you have another Logger object you want to write to, no problem:
|
|
59
|
-
|
|
60
|
-
some_other_logger = Logger.new 'log.txt'
|
|
61
|
-
MyThing.logger = some_other_logger
|
|
62
|
-
MyThing.log "hi!"
|
|
63
|
-
File.open('log.txt', 'r').read # => Logfile created on 2011-10-07 15:50:19 -0700 by logger.rb/25413
|
|
64
|
-
# D, [2011-10-07T15:51:16.385798 #34026] DEBUG -- : hi!
|
|
65
|
-
|
|
66
|
-
=== Hooks
|
|
67
|
-
|
|
68
|
-
You can also make sure code gets executed before each call to +.log+. While
|
|
69
|
-
this really only makes sense when you're logging from multiple places and/or
|
|
70
|
-
you're not sure when the first call to your logger will occur (unlike this
|
|
71
|
-
example), say you want to makes sure your log directory exists before trying
|
|
72
|
-
to write the log file to it:
|
|
73
|
-
|
|
74
|
-
log_directory = File.expand_path('my_log_dir')
|
|
75
|
-
|
|
76
|
-
MyThing.before do
|
|
77
|
-
FileUtils.mkdir_p(full_directory) unless Dir.exists? log_directory
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
log_file = log_directory + "/sweet_log_bro.txt"
|
|
81
|
-
MyThing.logger = Logger.new(log_file)
|
|
82
|
-
MyThing.log "Thanks brah!" # This calls the #before hook, thus
|
|
83
|
-
# creating the directory.
|
|
84
|
-
MyThing.log "I'm hungry..." # This also calls the #before hook...
|
|
85
|
-
|
|
86
|
-
You might just want want a one-time before hook--in that case, +.log+ will call
|
|
87
|
-
a plain old block that you pass to it, right before writing to the log file.
|
|
88
|
-
Here's the above example, using the one-timer:
|
|
89
|
-
|
|
90
|
-
log_directory = File.expand_path('my_log_dir')
|
|
91
|
-
|
|
92
|
-
log_file = log_directory + "/sweet_log_bro.txt"
|
|
93
|
-
MyThing.logger = Logger.new(log_file)
|
|
94
|
-
|
|
95
|
-
MyThing.log("Thanks brah!") do
|
|
96
|
-
FileUtils.mkdir_p(full_directory) unless Dir.exists? log_directory
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
# No block gets called here (assuming the +#before+ hook from above hasn't been defined)
|
|
100
|
-
MyThing.log "I'm hungry..."
|
|
101
|
-
|
|
102
|
-
=== Mixin for lazy people like me
|
|
103
|
-
|
|
104
|
-
All that stuff above describes how you can extend some class of yours to make it
|
|
105
|
-
a singleton logger (well, not that you can't have it do other stuff too...). If
|
|
106
|
-
you're using this in a sizeable project, however, it can get tiresome typing out
|
|
107
|
-
<tt>MyThing.log "message"</tt> over and over again; for me, it's usually more
|
|
108
|
-
like <tt>MyThing::Logger.log "message"</tt>. The {LogSwitch::Mixin} mixin
|
|
109
|
-
lets you shorten things up.
|
|
110
|
-
|
|
111
|
-
If you've extended {LogSwitch} into a class...
|
|
112
|
-
|
|
113
|
-
class MyThing
|
|
114
|
-
class Logger
|
|
115
|
-
extend LogSwitch
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
...then you can mix in {LogSwitch::Mixin} to your other classes that you want to
|
|
120
|
-
use +MyThing::Logger.log+:
|
|
121
|
-
|
|
122
|
-
class MyThing
|
|
123
|
-
class WidgetMaker
|
|
124
|
-
include LogSwitch::Mixin
|
|
125
|
-
|
|
126
|
-
def make_widget
|
|
127
|
-
log "Making widget...", :info # This will delegate to MyThing::Logger.log
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
This lets you still use a single logger throughout your classes, but shortens
|
|
133
|
-
the amount of typing you have to do.
|
|
134
|
-
|
|
135
|
-
=== Prepend class name of #log caller to the message
|
|
136
|
-
|
|
137
|
-
When you've got a bunch of different classes doing logging, it's easy to get
|
|
138
|
-
lost in all the text. Adding the class name of the class that's logging a
|
|
139
|
-
message can make this easier to read. Now log_switch will do that for you, if
|
|
140
|
-
you use its Mixin.
|
|
141
|
-
|
|
142
|
-
# lib/my_thing/logger.rb
|
|
143
|
-
class MyThing::Logger
|
|
144
|
-
extend LogSwitch
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
MyThing::Logger.log_class_name = true
|
|
148
|
-
|
|
149
|
-
# lib/my_thing/barrel_roller.rb
|
|
150
|
-
class MyThing::BarrelRoller
|
|
151
|
-
include LogSwitch::Mixin
|
|
152
|
-
|
|
153
|
-
def do_one
|
|
154
|
-
log "I did a barrel roll!"
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
# somewhere else...
|
|
159
|
-
barrel_roller = MyThing::BarrelRoller.new
|
|
160
|
-
barrel_roller.do_one # => D, [2012-11-12T21:11:32.133414 #16645] DEBUG -- : <MyThing::BarrelRoller> I did a barrel roll!
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
== REQUIREMENTS
|
|
164
|
-
|
|
165
|
-
* Rubies (tested):
|
|
166
|
-
* MRI 1.9.3
|
|
167
|
-
* MRI 1.9.2
|
|
168
|
-
* MRI 1.8.7
|
|
169
|
-
* ree 1.8.7-2011.03
|
|
170
|
-
* JRuby 1.6.5
|
|
171
|
-
* Rubinius 1.2.4
|
|
172
|
-
* RubyGems:
|
|
173
|
-
* None!
|
|
174
|
-
|
|
175
|
-
== INSTALL
|
|
176
|
-
|
|
177
|
-
$ gem install log_switch
|
|
178
|
-
|
|
179
|
-
== DEVELOPERS
|
|
180
|
-
|
|
181
|
-
After checking out the source, run:
|
|
182
|
-
|
|
183
|
-
$ bundle install
|
|
184
|
-
|
|
185
|
-
This task will install any missing dependencies for you.
|
|
186
|
-
|
|
187
|
-
== THANKS
|
|
188
|
-
|
|
189
|
-
I need to thank the http://github.com/rubiii/savon project for most of the code
|
|
190
|
-
here. Somehow I ran across how they do logging and started following suit. The
|
|
191
|
-
code in +log_switch+ is almost identical to Savon's logging.
|
|
192
|
-
|