Class: PluginMetaLogger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jekyll_plugin_meta_logger.rb

Overview

Makes a meta-logger instance (a singleton) with level set by site.config. Saves site.config for later use when creating plugin loggers; these loggers each have their own log levels.

For example, if the plugin's progname has value MyPlugin then an entry called plugin_loggers.MyPlugin will be read from config, if present. If no such entry is found then the meta-logger log_level is set to :info. If you want to see messages that indicate the loggers and log levels as they are created, set the log level for PluginMetaLogger to debug in _config.yml

Examples:

# Create and initialize the meta-logger singleton in a high priority Jekyll `site` `:after_init` hook:
@meta_logger = PluginMetaLogger.instance.new_logger(site.config, self)
@meta_logger.info { "Meta-logger has been created" }

# In `config.yml`:
plugin_loggers:
  PluginMetaLogger: info
  MyPlugin: warn
  MakeArchive: error
  ArchiveDisplayTag: debug

# In a Jekyll plugin:
@logger = PluginMetaLogger.instance.setup(self)
@logger.info { "This is a log message from a Jekyll plugin" }
#
PluginMetaLogger.instance.info { "MyPlugin vX.Y.Z has been loaded" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePluginMetaLogger

Returns a new instance of PluginMetaLogger.



36
37
38
39
40
# File 'lib/jekyll_plugin_meta_logger.rb', line 36

def initialize
  super
  @config = nil
  @logger = new_logger(self)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



33
34
35
# File 'lib/jekyll_plugin_meta_logger.rb', line 33

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



33
34
35
# File 'lib/jekyll_plugin_meta_logger.rb', line 33

def logger
  @logger
end

Instance Method Details

#debug(&block) ⇒ Object



47
48
49
50
# File 'lib/jekyll_plugin_meta_logger.rb', line 47

def debug(&block)
  # Delegate to @logger which respects the configured level
  @logger.debug(self) { yield block }
end

#error(&block) ⇒ Object



60
61
62
# File 'lib/jekyll_plugin_meta_logger.rb', line 60

def error(&block)
  @logger.error(self) { yield block }
end

#info(&block) ⇒ Object



42
43
44
45
# File 'lib/jekyll_plugin_meta_logger.rb', line 42

def info(&block)
  # Delegate to @logger which respects the configured level
  @logger.info(self) { yield block }
end

#level_as_symObject



52
53
54
# File 'lib/jekyll_plugin_meta_logger.rb', line 52

def level_as_sym
  @logger.level_as_sym
end

#new_logger(klass, config = nil, stream_name = $stdout) ⇒ Object

By the time this method is called, PluginMetaLogger.instance.config contains the entire contents of _config.yml



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll_plugin_meta_logger.rb', line 65

def new_logger(klass, config = nil, stream_name = $stdout)
  @config ||= config
  if @config.nil?
    logger = PluginLogger.new(klass, {}, stream_name)
    logger.debug { 'PluginMetaLogger was not initialized from site.config.' }
  else
    logger = PluginLogger.new(klass, @config, stream_name)
    logger.debug { 'PluginMetaLogger was initialized from site.config.' }
  end
  logger
end

#warn(&block) ⇒ Object



56
57
58
# File 'lib/jekyll_plugin_meta_logger.rb', line 56

def warn(&block)
  @logger.warn(self) { yield block }
end