module Jekyll::Algolia::Logger
def self.known_message(message_id, metadata = {})
It will read files in ./errors/*.txt with the matching error and
metadata: Hash of variables that can be used in the final text
message_id: A string identifying a know message
Public: Displays a helpful error message for one of the knows errors
def self.known_message(message_id, metadata = {}) file = File.expand_path( File.join( __dir__, '../..', 'errors', "#{message_id}.txt" ) ) # Convert all variables content = File.open(file).read metadata.each do |key, value| content = content.gsub("{#{key}}", value) end # Display each line differently lines = content.each_line.map(&:chomp) lines.each do |line| log(line) end end
def self.log(line)
Where X is either I, W or E for marking respectively an info, warning or
"X:Your content"
line - Line to display. Expected to be of the following format:
Public: Displays a log line
def self.log(line) type, content = /^(I|W|E):(.*)/.match(line).captures logger_mapping = { 'E' => :error, 'I' => :info, 'W' => :warn } # Jekyll logger tries to center log lines, so we force a consistent # width of 80 chars content = content.ljust(80, ' ') Jekyll.logger.send(logger_mapping[type], content) end
def self.silent
loggued. It works by redefining Jekyll.logger.write to a noop
This is especially useful when Jekyll is too talkative about what is
end
# whatever Jekyll code here
Logger.silence do
Usage:
Public: Silence all Jekyll log output in this block
def self.silent initial_method = Jekyll.logger.method(:write) Utils.monkey_patch(Jekyll.logger, :write, proc { |*args| }) begin yield ensure Utils.monkey_patch(Jekyll.logger, :write, initial_method) end end
def self.verbose(line)
Public: Only display a log line if verbose mode is enabled
def self.verbose(line) return unless Configurator.verbose? log(line) end
def self.write_to_file(filename, content)
filename - the file basename
Public: Write the specified content to a file in the source directory
def self.write_to_file(filename, content) filepath = File.join(Configurator.get('source'), filename) File.write(filepath, content) filepath end