jekyll_plugin_logger 2.0.1 → 2.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 +4 -4
- data/CHANGELOG.md +3 -0
- data/jekyll_plugin_logger.gemspec +1 -1
- data/lib/jekyll_plugin_logger/version.rb +2 -2
- data/lib/jekyll_plugin_logger.rb +32 -32
- data/spec/{jekyll_plugin_logger.rb → jekyll_plugin_logger_spec.rb} +21 -15
- data/spec/spec_helper.rb +19 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26418179467ef67c9e606c45bd565fff0fe4f8c450da9efa243d29d99b581821
|
4
|
+
data.tar.gz: 6f7b529c26a6df0b236acb15c2a6c9314b2679cba64d152c41b3f581ab188a8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61308925aaf08121c323b650a7918c549a33c5eb4a2b0c50985210be2edd11bcadf5d31d4773c9a09c96194822efb95b83767ea10f98864752ec7a36cb5ee118
|
7
|
+
data.tar.gz: e4f70c43f66bffe16ccb045ed645d6ac0167de28e9146661325d69c155a444f9b717bfd9d9972e43e17217445c11247a58de13325cd1e04ef9f32a07d78bd650
|
data/CHANGELOG.md
CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.required_ruby_version = ">= 2.6.0"
|
33
33
|
spec.summary = "Generates Jekyll logger with colored output."
|
34
34
|
spec.test_files = spec.files.grep(%r!^(test|spec|features)/!)
|
35
|
-
spec.version =
|
35
|
+
spec.version = JekyllPluginLoggerVersion::VERSION
|
36
36
|
|
37
37
|
spec.add_dependency "jekyll", ">= 3.5.0"
|
38
38
|
|
data/lib/jekyll_plugin_logger.rb
CHANGED
@@ -10,13 +10,14 @@ module JekyllPluginLoggerName
|
|
10
10
|
PLUGIN_NAME = "jekyll_plugin_logger"
|
11
11
|
end
|
12
12
|
|
13
|
-
# Once the meta-logger is made (see `PluginMetaLogger
|
13
|
+
# Once the meta-logger is made (see `PluginMetaLogger`, below) new instances of `PluginLogger` can be created with log levels set
|
14
|
+
# by `config` entries.
|
14
15
|
# @example Create new `PluginLogger`s like this:
|
15
|
-
# @logger = PluginMetaLogger.instance.new_logger(self)
|
16
|
+
# @logger = PluginMetaLogger.instance.new_logger(self, PluginMetaLogger.instance.config)
|
16
17
|
#
|
17
|
-
# self can be a class, a string, or a symbol.
|
18
|
+
# self can be a module, a class, a string, or a symbol.
|
18
19
|
#
|
19
|
-
# Best practice is to invoke `info
|
20
|
+
# Best practice is to invoke `info`, `warn, `debug` and `error` methods by passing blocks that contain the message.
|
20
21
|
# The blocks will only be evaluated if output for that level is enabled.
|
21
22
|
# @example Use `PluginLogger`s like this:
|
22
23
|
# @logger.info { "This is only evaluated if info level debugging is enabled" }
|
@@ -24,8 +25,6 @@ end
|
|
24
25
|
# For more information about the logging feature in the Ruby standard library,
|
25
26
|
# @see https://ruby-doc.org/stdlib-2.7.1/libdoc/logger/rdoc/Logger.html
|
26
27
|
class PluginLogger
|
27
|
-
include JekyllPluginLogger
|
28
|
-
|
29
28
|
# This method should only be called by PluginMetaLogger
|
30
29
|
# @param log_level [String, Symbol, Integer] can be specified as $stderr or $stdout,
|
31
30
|
# or an integer from 0..3 (inclusive),
|
@@ -40,12 +39,13 @@ class PluginLogger
|
|
40
39
|
# @example If `progname` has value `abc`, then the YAML to override the programmatically set log_level to `debug` is:
|
41
40
|
# logger_factory:
|
42
41
|
# abc: debug
|
43
|
-
def initialize(klass, config, stream_name = $stdout)
|
42
|
+
def initialize(klass, config = nil, stream_name = $stdout)
|
43
|
+
@config = config
|
44
44
|
@logger = Logger.new(stream_name)
|
45
45
|
@logger.progname = derive_progname(klass)
|
46
46
|
@logger.level = :info
|
47
|
-
plugin_loggers = config["plugin_loggers"]
|
48
|
-
@logger.level
|
47
|
+
plugin_loggers = config ? config["plugin_loggers"] : nil
|
48
|
+
@logger.level = plugin_loggers[@logger.progname] if plugin_loggers && plugin_loggers[@logger.progname]
|
49
49
|
# puts "PluginLogger.initialize: @logger.progname=#{@logger.progname} set to #{@logger.level}".red
|
50
50
|
@logger.formatter = proc { |severity, _, prog_name, msg|
|
51
51
|
"#{severity} #{prog_name}: #{msg}\n"
|
@@ -100,26 +100,26 @@ class PluginLogger
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
+
# Available colors are: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, and the modifier :bold
|
104
|
+
def level_as_sym
|
105
|
+
return :unknown if @logger.level.negative? || level > 4
|
106
|
+
|
107
|
+
[:debug, :info, :warn, :error, :fatal, :unknown][@logger.level]
|
108
|
+
end
|
109
|
+
|
103
110
|
private
|
104
111
|
|
105
112
|
def derive_progname(klass)
|
106
113
|
class_name = klass.class.to_s
|
107
114
|
case class_name
|
108
115
|
when "Class"
|
109
|
-
klass.
|
116
|
+
klass.to_s.split("::").last # class_name.name.split("::").last
|
110
117
|
when "Module", "Symbol", "String"
|
111
|
-
klass.to_s
|
118
|
+
klass.to_s.split("::").last
|
112
119
|
else
|
113
120
|
class_name
|
114
121
|
end
|
115
122
|
end
|
116
|
-
|
117
|
-
# Available colors are: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, and the modifier :bold
|
118
|
-
def level_as_sym
|
119
|
-
return :unknown if @logger.level.negative? || level > 4
|
120
|
-
|
121
|
-
[:debug, :info, :warn, :error, :fatal, :unknown][@logger.level]
|
122
|
-
end
|
123
123
|
end
|
124
124
|
|
125
125
|
# Makes a meta-logger instance (a singleton) with level set by `site.config`.
|
@@ -133,7 +133,8 @@ end
|
|
133
133
|
#
|
134
134
|
# @example
|
135
135
|
# # Create and initialize the meta-logger singleton in a high priority Jekyll `site` `:after_init` hook:
|
136
|
-
# PluginMetaLogger.instance.
|
136
|
+
# @meta_logger = PluginMetaLogger.instance.new_logger(site.config, self)
|
137
|
+
# @meta_logger.info { "Meta-logger has been created" }
|
137
138
|
#
|
138
139
|
# # In `config.yml`:
|
139
140
|
# plugin_loggers:
|
@@ -143,15 +144,15 @@ end
|
|
143
144
|
# ArchiveDisplayTag: debug
|
144
145
|
#
|
145
146
|
# # In a Jekyll plugin:
|
146
|
-
# @logger = PluginMetaLogger.instance.
|
147
|
+
# @logger = PluginMetaLogger.instance.setup(self)
|
147
148
|
# @logger.info { "This is a log message from a Jekyll plugin" }
|
148
149
|
# #
|
149
150
|
# PluginMetaLogger.instance.info { "MyPlugin vX.Y.Z has been loaded" }
|
150
151
|
class PluginMetaLogger
|
151
152
|
include Singleton
|
152
|
-
attr_reader :logger
|
153
|
+
attr_reader :config, :logger
|
153
154
|
|
154
|
-
def initialize
|
155
|
+
def initialize()
|
155
156
|
super
|
156
157
|
@config = nil
|
157
158
|
@logger = new_logger(self)
|
@@ -165,6 +166,10 @@ class PluginMetaLogger
|
|
165
166
|
@logger.debug(self) { yield }
|
166
167
|
end
|
167
168
|
|
169
|
+
def level_as_sym
|
170
|
+
@logger.level_as_sym
|
171
|
+
end
|
172
|
+
|
168
173
|
def warn
|
169
174
|
@logger.warn(self) { yield }
|
170
175
|
end
|
@@ -173,25 +178,20 @@ class PluginMetaLogger
|
|
173
178
|
@logger.error(self) { yield }
|
174
179
|
end
|
175
180
|
|
176
|
-
def new_logger(klass, stream_name = $stdout)
|
181
|
+
def new_logger(klass, config = nil, stream_name = $stdout)
|
182
|
+
@config ||= config
|
177
183
|
if @config.nil?
|
178
|
-
puts { "Error: PluginMetaLogger has not been initialized
|
184
|
+
puts { "Error: PluginMetaLogger has not been initialized with site.config.".red }
|
179
185
|
PluginLogger.new(klass, {}, stream_name)
|
180
186
|
else
|
181
187
|
PluginLogger.new(klass, @config, stream_name)
|
182
188
|
end
|
183
189
|
end
|
184
|
-
|
185
|
-
def setup(config, stream_name = $stdout)
|
186
|
-
@config = config
|
187
|
-
@logger = new_logger(self, stream_name)
|
188
|
-
@logger
|
189
|
-
end
|
190
190
|
end
|
191
191
|
|
192
192
|
Jekyll::Hooks.register(:site, :after_reset, :priority => :high) do |site|
|
193
193
|
instance = PluginMetaLogger.instance
|
194
|
-
logger = instance.
|
195
|
-
logger.info { "Loaded #{JekyllPluginLoggerName::PLUGIN_NAME} v#{
|
194
|
+
logger = instance.new_logger(PluginMetaLogger, site.config)
|
195
|
+
logger.info { "Loaded #{JekyllPluginLoggerName::PLUGIN_NAME} v#{JekyllPluginLoggerVersion::VERSION} plugin." }
|
196
196
|
logger.debug { "Logger for #{instance.progname} created at level #{instance.level_as_sym}" }
|
197
197
|
end
|
@@ -19,26 +19,17 @@ require_relative "../lib/jekyll_plugin_logger"
|
|
19
19
|
class MyTestPlugin
|
20
20
|
instance = PluginMetaLogger.instance
|
21
21
|
logger = instance.new_logger(self) # Should generate a warning
|
22
|
-
instance.setup({})
|
23
22
|
PluginMetaLogger.instance.info { "How now, brown cow" }
|
24
23
|
PluginMetaLogger.instance.debug { "How now, brown cow" }
|
25
24
|
PluginMetaLogger.instance.warn { "How now, brown cow" }
|
26
25
|
PluginMetaLogger.instance.error { "How now, brown cow" }
|
27
26
|
|
28
|
-
logger = PluginMetaLogger.instance.new_logger(self)
|
27
|
+
logger = PluginMetaLogger.instance.new_logger(self, RSpec.configuration.site_config)
|
29
28
|
logger.debug { "3 fleas fleeing freedom" }
|
30
29
|
logger.info { "3 fleas fleeing freedom" }
|
31
30
|
logger.warn { "3 fleas fleeing freedom" }
|
32
31
|
logger.error { "3 fleas fleeing freedom" }
|
33
32
|
|
34
|
-
_yaml = <<~END_YAML
|
35
|
-
plugin_loggers:
|
36
|
-
PluginMetaLogger: debug
|
37
|
-
SiteInspector: warn
|
38
|
-
MakeArchive: error
|
39
|
-
ArchiveDisplayTag: debug
|
40
|
-
END_YAML
|
41
|
-
|
42
33
|
def self.exercise(logger)
|
43
34
|
puts
|
44
35
|
# puts "\ncalling_class_name=#{logger.send(:calling_class_name)}"
|
@@ -63,18 +54,33 @@ class MyTestPlugin
|
|
63
54
|
logger.error { "Error message 4" }
|
64
55
|
end
|
65
56
|
|
66
|
-
RSpec.describe
|
67
|
-
it "outputs at
|
68
|
-
MyTestPlugin.exercise(PluginMetaLogger.instance.new_logger(self, $stdout))
|
57
|
+
RSpec.describe PluginLogger do
|
58
|
+
it "outputs at info level" do
|
59
|
+
MyTestPlugin.exercise(PluginMetaLogger.instance.new_logger(self, PluginMetaLogger.instance.config, $stdout))
|
60
|
+
expect(PluginMetaLogger.instance.level_as_sym).to eq(:info)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "uses config debug" do
|
64
|
+
logger = PluginMetaLogger.instance.new_logger(:MyBlock, PluginMetaLogger.instance.config)
|
65
|
+
expect(logger.level_as_sym).to eq(:debug)
|
66
|
+
MyTestPlugin.exercise(logger)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "uses config info" do
|
70
|
+
logger = PluginMetaLogger.instance.new_logger(:ArchiveDisplayTag, PluginMetaLogger.instance.config)
|
71
|
+
expect(logger.level_as_sym).to eq(:info)
|
72
|
+
MyTestPlugin.exercise(logger)
|
69
73
|
end
|
70
74
|
|
71
75
|
it "uses config warn" do
|
72
|
-
logger = PluginMetaLogger.instance.new_logger("SiteInspector", $stdout)
|
76
|
+
logger = PluginMetaLogger.instance.new_logger("SiteInspector", PluginMetaLogger.instance.config, $stdout)
|
77
|
+
expect(logger.level_as_sym).to eq(:warn)
|
73
78
|
MyTestPlugin.exercise(logger)
|
74
79
|
end
|
75
80
|
|
76
81
|
it "uses config error" do
|
77
|
-
logger = PluginMetaLogger.instance.new_logger(:
|
82
|
+
logger = PluginMetaLogger.instance.new_logger(:PreTagBlock, PluginMetaLogger.instance.config)
|
83
|
+
expect(logger.level_as_sym).to eq(:error)
|
78
84
|
MyTestPlugin.exercise(logger)
|
79
85
|
end
|
80
86
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -6,7 +6,25 @@ require_relative "../lib/jekyll_plugin_logger"
|
|
6
6
|
Jekyll.logger.log_level = :info
|
7
7
|
|
8
8
|
RSpec.configure do |config|
|
9
|
-
|
9
|
+
plugin_config = <<~END_CONFIG
|
10
|
+
plugin_loggers:
|
11
|
+
ArchiveDisplayTag: info
|
12
|
+
FlexibleInclude: info
|
13
|
+
MakeArchive: info
|
14
|
+
MyBlock: debug
|
15
|
+
PluginMetaLogger: info
|
16
|
+
PreTagBlock: error
|
17
|
+
SiteInspector: warn
|
18
|
+
UnselectableTag: info
|
19
|
+
END_CONFIG
|
20
|
+
|
21
|
+
config.add_setting :site_config
|
22
|
+
config.site_config = YAML.safe_load(plugin_config)
|
23
|
+
|
10
24
|
config.filter_run :focus
|
11
25
|
config.order = "random"
|
26
|
+
config.run_all_when_everything_filtered = true
|
27
|
+
|
28
|
+
# See https://relishapp.com/rspec/rspec-core/docs/command-line/only-failures
|
29
|
+
config.example_status_persistence_file_path = "spec/status_persistence.txt"
|
12
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll_plugin_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Slinn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|
@@ -69,7 +69,7 @@ files:
|
|
69
69
|
- jekyll_plugin_logger.gemspec
|
70
70
|
- lib/jekyll_plugin_logger.rb
|
71
71
|
- lib/jekyll_plugin_logger/version.rb
|
72
|
-
- spec/
|
72
|
+
- spec/jekyll_plugin_logger_spec.rb
|
73
73
|
- spec/spec_helper.rb
|
74
74
|
homepage: https://www.mslinn.com/blog/2020/12/28/custom-logging-in-jekyll-plugins.html
|
75
75
|
licenses:
|
@@ -103,5 +103,5 @@ signing_key:
|
|
103
103
|
specification_version: 4
|
104
104
|
summary: Generates Jekyll logger with colored output.
|
105
105
|
test_files:
|
106
|
-
- spec/
|
106
|
+
- spec/jekyll_plugin_logger_spec.rb
|
107
107
|
- spec/spec_helper.rb
|