Class: XCRes::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/xcres/logger.rb

Overview

A Logger utility help class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Initialize a new logger



33
34
35
36
37
38
# File 'lib/xcres/logger.rb', line 33

def initialize
  self.silent = false
  self.verbose = false
  self.colored = true
  self.indentation = ''
end

Instance Attribute Details

#coloredBool Also known as: colored?

Returns if set to true, ANSI colors will be used to color the output otherwise it will output plain text, true by default.

Returns:

  • (Bool)

    if set to true, ANSI colors will be used to color the output otherwise it will output plain text, true by default.



24
25
26
# File 'lib/xcres/logger.rb', line 24

def colored
  @colored
end

#indentationString

Returns the indentation of the output, empty string by default.

Returns:

  • (String)

    the indentation of the output, empty string by default.



29
30
31
# File 'lib/xcres/logger.rb', line 29

def indentation
  @indentation
end

#silentBool Also known as: silent?

Returns if set to true, all log calls of all kinds will be ignored otherwise they will be printed, false by default.

Returns:

  • (Bool)

    if set to true, all log calls of all kinds will be ignored otherwise they will be printed, false by default.



18
19
20
# File 'lib/xcres/logger.rb', line 18

def silent
  @silent
end

#verboseBool Also known as: verbose?

Returns if set to false, calls to #log will be ignored otherwise they will be printed, false by default.

Returns:

  • (Bool)

    if set to false, calls to #log will be ignored otherwise they will be printed, false by default.



12
13
14
# File 'lib/xcres/logger.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#fail(message_or_exception, *format_args) ⇒ Object

Print a log message to indicate failure of an operation in red color

Parameters:

  • message_or_exception (String|Exception)

    The message, which can have format placeholders, can also be a kind of Exception, then its message would been used instead. The backtrace will be only printed, if the verbose mode is enabled.

  • format_args (#to_s...)

    will be passed as right hand side to the percent operator, which will fill the format placeholders used in the message



135
136
137
138
139
140
141
142
# File 'lib/xcres/logger.rb', line 135

def fail message_or_exception, *format_args
  message, exception = coerce_to_message(message_or_exception)
  inform_colored '' + ' ' + message, :red, *format_args

  if verbose? && exception != nil
    log "Backtrace:\n"+exception.backtrace.join("\n"), :red
  end
end

#inform(message, *format_args) ⇒ Object

Prints a formatted message

Parameters:

  • message (String)

    the message, which can have format placeholders

  • format_args (#to_s...)

    will be passed as right hand side to the percent operator, which will fill the format placeholders used in the message



49
50
51
# File 'lib/xcres/logger.rb', line 49

def inform message, *format_args
  puts indentation + message % format_args unless silent?
end

#inform_colored(message, color, *format_args) ⇒ Object

Prints a formatted message in a given color, and prints its arguments with bold font weight

Parameters:

  • message (String)

    the message, which can have format placeholders

  • color (Symbol)

    the color, String has to #respond_to? this

  • format_args (#to_s...)

    will be passed as right hand side to the percent operator, which will fill the format placeholders used in the message



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xcres/logger.rb', line 66

def inform_colored message, color, *format_args
  if colored?
    parts = message
       .gsub(/%[\ +#]?\d*\.?\d*[a-z]/, "\0"+'\0'+"\0")
       .split("\0")
       .reject(&:empty?)
    message = parts.map do |part|
      if part[0] == '%' && part[1] != '%'
        (part % [format_args.shift]).bold.gsub('%', '%%')
      else
        part
      end
    end.map(&color).join('')
  end
  inform message, *format_args
end

#log(message, *format_args) ⇒ Object

Print a log message of log level verbose

Parameters:

  • message (String)

    the message, which can have format placeholders

  • format_args (#to_s...)

    will be passed as right hand side to the percent operator, which will fill the format placeholders used in the message



92
93
94
# File 'lib/xcres/logger.rb', line 92

def log message, *format_args
  inform_colored '' + ' ' + message, :magenta, *format_args if verbose?
end

#success(message, *format_args) ⇒ Object

Print a log message to indicate success of an operation in green color

Parameters:

  • message (String)

    the message, which can have format placeholders

  • format_args (#to_s...)

    will be passed as right hand side to the percent operator, which will fill the format placeholders used in the message



105
106
107
# File 'lib/xcres/logger.rb', line 105

def success message, *format_args
  inform_colored '' + ' ' + message, :green, *format_args
end

#warn(message_or_exception, *format_args) ⇒ Object

Print a warning log message in yellow color

Parameters:

  • message_or_exception (String|Exception)

    the message, which can have format placeholders

  • format_args (#to_s...)

    will be passed as right hand side to the percent operator, which will fill the format placeholders used in the message



118
119
120
121
# File 'lib/xcres/logger.rb', line 118

def warn message_or_exception, *format_args
  message, _ = coerce_to_message(message_or_exception)
  inform_colored '' + ' ' + message, :yellow, *format_args
end