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.

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,

Examples:

Create new PluginLoggers like this:

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

Use PluginLoggers 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 stored in PluginMetaLogger.instance.config.

Examples:

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

logger_factory:
  abc: debug

Parameters:

  • stream_name (String, Symbol, Integer) (defaults to: $stdout)

    can be specified as $stderr or $stdout

  • config (YAML) (defaults to: nil)

    can accept configuration data, usually a reference to site.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

#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



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)

Parameters:

  • value (String, Symbol, Integer)

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



80
81
82
# File 'lib/jekyll_plugin_logger.rb', line 80

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



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

#prognameObject



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