Class: PluginLogger
- Inherits:
-
Object
- Object
- PluginLogger
- Defined in:
- lib/jekyll_plugin_logger.rb
Overview
Once the meta-logger is made (see PluginMetaLogger, below) new instances of PluginLogger can be created with log levels set
by config entries.
ref can be a module name, a class name, a string, or a symbol.
Logger usage:
Best practice is to invoke info, warn, debuganderror` methods by passing blocks that contain the message.
The blocks will only be evaluated if output for that level is enabled.
For more information about the logging feature in the Ruby standard library,
Instance Method Summary collapse
- #debug(progname = nil, &block) ⇒ Object
- #error(progname = nil, &block) ⇒ Object
- #fatal(progname = nil, &block) ⇒ Object
- #info(progname = nil, &block) ⇒ Object
-
#initialize(klass, config = nil, stream_name = $stdout) ⇒ PluginLogger
constructor
This method should only be called by PluginMetaLogger stored in
PluginMetaLogger.instance.config. -
#level ⇒ Object
The log level specified in _config.yml, or :info (1) if not specified.
-
#level=(value) ⇒ Object
0: debug 1: info 2: warn 3: error 4: fatal 5: unknown (displays as ANY).
-
#level_as_sym ⇒ Object
Available colors are: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, and the modifier :bold.
- #progname ⇒ Object
- #progname=(value) ⇒ Object
- #unknown(progname = nil, &block) ⇒ Object
- #warn(progname = nil, &block) ⇒ Object
Constructor Details
#initialize(klass, config = nil, stream_name = $stdout) ⇒ PluginLogger
This method should only be called by PluginMetaLogger
stored in PluginMetaLogger.instance.config.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jekyll_plugin_logger.rb', line 38 def initialize(klass, config = nil, stream_name = $stdout) @config = config plugin_loggers = config&.[] 'plugin_loggers' @logger = Logger.new stream_name @logger.progname = derive_progname klass # Default to :warn when no config, so INFO messages are suppressed until config is loaded @logger.level = (config.nil? || config.empty?) ? :warn : :info @logger.level = plugin_loggers[@logger.progname] if plugin_loggers&.[] @logger.progname # puts "PluginLogger.initialize: @logger.progname=#{@logger.progname} set to #{@logger.level}".red @logger.formatter = proc { |severity, _datetime, progname, msg| "#{severity} #{progname}: #{msg}\n" } end |
Instance Method Details
#debug(progname = nil, &block) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/jekyll_plugin_logger.rb', line 53 def debug(progname = nil, &block) if block @logger.debug(@logger.progname) { (yield block).to_s.magenta } else @logger.debug(@logger.progname) { progname.to_s.magenta } end end |
#error(progname = nil, &block) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/jekyll_plugin_logger.rb', line 105 def error(progname = nil, &block) if block @logger.error(@logger.progname) { (yield block).to_s.red } else @logger.error(@logger.progname) { progname.to_s.red } end end |
#fatal(progname = nil, &block) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/jekyll_plugin_logger.rb', line 113 def fatal(progname = nil, &block) if block @logger.fatal(@logger.progname) { (yield block).to_s.red.bold } else @logger.fatal(@logger.progname) { progname.to_s.red.bold } end end |
#info(progname = nil, &block) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/jekyll_plugin_logger.rb', line 61 def info(progname = nil, &block) if block @logger.info(@logger.progname) { (yield block).to_s.cyan } else @logger.info(@logger.progname) { progname.to_s.cyan } end end |
#level ⇒ Object
Returns the log level specified in _config.yml, or :info (1) if not specified.
85 86 87 |
# File 'lib/jekyll_plugin_logger.rb', line 85 def level @logger.level end |
#level=(value) ⇒ Object
0: debug 1: info 2: warn 3: error 4: fatal 5: unknown (displays as ANY)
80 81 82 |
# File 'lib/jekyll_plugin_logger.rb', line 80 def level=(value) @logger.level = value end |
#level_as_sym ⇒ Object
Available colors are: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, and the modifier :bold
123 124 125 126 127 |
# File 'lib/jekyll_plugin_logger.rb', line 123 def level_as_sym return :unknown if @logger.level.negative? || level > 4 i[debug info warn error fatal unknown][@logger.level] end |
#progname ⇒ Object
93 94 95 |
# File 'lib/jekyll_plugin_logger.rb', line 93 def progname @logger.progname end |
#progname=(value) ⇒ Object
89 90 91 |
# File 'lib/jekyll_plugin_logger.rb', line 89 def progname=(value) @logger.progname = value end |
#unknown(progname = nil, &block) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/jekyll_plugin_logger.rb', line 129 def unknown(progname = nil, &block) if block @logger.unknown(@logger.progname) { (yield block).to_s.green } else @logger.unknown(@logger.progname) { progname.to_s.green } end end |
#warn(progname = nil, &block) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/jekyll_plugin_logger.rb', line 97 def warn(progname = nil, &block) if block @logger.warn(@logger.progname) { (yield block).to_s.yellow } else @logger.warn(@logger.progname) { progname.to_s.yellow } end end |