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. self can be a module, a class, a string, or a symbol.
Best practice is to invoke ‘info`, `warn, `debug` and `error` 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 0: debug 1: info 2: warn 3: error 4: fatal 5: unknown (displays as ANY).
-
#level ⇒ Object
The log level specified in _config.yml, or :info (1) if not specified.
- #level=(value) ⇒ Object
-
#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 0: debug 1: info 2: warn 3: error 4: fatal 5: unknown (displays as ANY)
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll_plugin_logger.rb', line 47 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 @logger.level = :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
61 62 63 64 65 66 67 |
# File 'lib/jekyll_plugin_logger.rb', line 61 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
102 103 104 105 106 107 108 |
# File 'lib/jekyll_plugin_logger.rb', line 102 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
110 111 112 113 114 115 116 |
# File 'lib/jekyll_plugin_logger.rb', line 110 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
69 70 71 72 73 74 75 |
# File 'lib/jekyll_plugin_logger.rb', line 69 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.
82 83 84 |
# File 'lib/jekyll_plugin_logger.rb', line 82 def level @logger.level end |
#level=(value) ⇒ Object
77 78 79 |
# File 'lib/jekyll_plugin_logger.rb', line 77 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
120 121 122 123 124 |
# File 'lib/jekyll_plugin_logger.rb', line 120 def level_as_sym return :unknown if @logger.level.negative? || level > 4 %i[debug info warn error fatal unknown][@logger.level] end |
#progname ⇒ Object
90 91 92 |
# File 'lib/jekyll_plugin_logger.rb', line 90 def progname @logger.progname end |
#progname=(value) ⇒ Object
86 87 88 |
# File 'lib/jekyll_plugin_logger.rb', line 86 def progname=(value) @logger.progname = value end |
#unknown(progname = nil, &block) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/jekyll_plugin_logger.rb', line 126 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
94 95 96 97 98 99 100 |
# File 'lib/jekyll_plugin_logger.rb', line 94 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 |