Class: PluginLogger

Inherits:
Object
  • Object
show all
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,

Examples:

Create new ‘PluginLogger`s like this:

@logger = PluginMetaLogger.instance.new_logger(self, PluginMetaLogger.instance.config)

Use ‘PluginLogger`s like this:

@logger.info { "This is only evaluated if info level debugging is enabled" }

See Also:

Instance Method Summary collapse

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)

Examples:

If ‘progname` has value `abc`, then the YAML to override the programmatically set log_level to `debug` is:

logger_factory:
  abc: debug

Parameters:

  • log_level (String, Symbol, Integer)

    can be specified as $stderr or $stdout, or an integer from 0..3 (inclusive), or as a case-insensitive string (‘debug`, `info`, `warn`, `error`, or `DEBUG`, `INFO`, `WARN`, `ERROR`), or as a symbol (`:debug`, `:info`, `:warn`, `:error` ).

  • config (YAML) (defaults to: nil)

    is normally created by reading a YAML file such as Jekyll’s ‘_config.yml`. When invoking from a Jekyll plugin, provide `site.config`, which is available from all types of Jekyll plugins as `Jekyll.configuration({})`.



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

#levelObject

Returns the log level specified in _config.yml, or :info (1) if not specified.

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_symObject

Available colors are: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, and the modifier :bold

Returns:

  • the log level specified in _config.yml, or :info (1) if not specified



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

#prognameObject



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