Module: Files::Util
- Defined in:
- lib/files.com/util.rb
Constant Summary collapse
- OPTS =
Set[:api_key, :client, :session_id].freeze
- COLOR_CODES =
{ black: 0, light_black: 60, red: 1, light_red: 61, green: 2, light_green: 62, yellow: 3, light_yellow: 63, blue: 4, light_blue: 64, magenta: 5, light_magenta: 65, cyan: 6, light_cyan: 66, white: 7, light_white: 67, default: 9, }.freeze
Class Method Summary collapse
- .check_api_key!(key) ⇒ Object
- .colorize(val, color, isatty) ⇒ Object
- .level_name(level) ⇒ Object
- .log_debug(message, data = {}) ⇒ Object
- .log_error(message, data = {}) ⇒ Object
- .log_info(message, data = {}) ⇒ Object
- .log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil) ⇒ Object
Class Method Details
.check_api_key!(key) ⇒ Object
99 100 101 102 103 |
# File 'lib/files.com/util.rb', line 99 def self.check_api_key!(key) raise TypeError, "api_key must be a string" unless key.is_a?(String) key end |
.colorize(val, color, isatty) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/files.com/util.rb', line 18 def self.colorize(val, color, isatty) return val unless isatty mode = 0 # default foreground = 30 + COLOR_CODES.fetch(color) background = 40 + COLOR_CODES.fetch(:default) "\033[#{mode};#{foreground};#{background}m#{val}\033[0m" end |
.level_name(level) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/files.com/util.rb', line 28 def self.level_name(level) case level when LEVEL_DEBUG then "debug" when LEVEL_ERROR then "error" when LEVEL_INFO then "info" else level end end |
.log_debug(message, data = {}) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/files.com/util.rb', line 55 def self.log_debug(, data = {}) if !Files.logger.nil? || (!Files.log_level.nil? && Files.log_level <= Files::LEVEL_DEBUG) log_internal(, data, color: :blue, level: Files::LEVEL_DEBUG, logger: Files.logger, out: $stdout ) end end |
.log_error(message, data = {}) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/files.com/util.rb', line 37 def self.log_error(, data = {}) if !Files.logger.nil? || (!Files.log_level.nil? && Files.log_level <= Files::LEVEL_ERROR) log_internal(, data, color: :cyan, level: Files::LEVEL_ERROR, logger: Files.logger, out: $stderr ) end end |
.log_info(message, data = {}) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/files.com/util.rb', line 46 def self.log_info(, data = {}) if !Files.logger.nil? || (!Files.log_level.nil? && Files.log_level <= Files::LEVEL_INFO) log_internal(, data, color: :cyan, level: Files::LEVEL_INFO, logger: Files.logger, out: $stdout ) end end |
.log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/files.com/util.rb', line 64 def self.log_internal(, data = {}, color: nil, level: nil, logger: nil, out: nil) data_str = data.compact.map do |(k, v)| format("%<key>s=%<value>s", key: colorize(k, color, logger.nil? && !out.nil? && out.isatty), value: v ) end.join(" ") if !logger.nil? # the library's log levels are mapped to the same values as the # standard library's logger logger.log(level, format("message=%<message>s %<data_str>s", message: , data_str: data_str ) ) elsif out.isatty out.puts format("%<level>s %<message>s %<data_str>s", level: colorize(level_name(level)[0, 4].upcase, color, out.isatty ), message: , data_str: data_str ) else out.puts format("message=%<message>s level=%<level>s %<data_str>s", message: , level: level_name(level), data_str: data_str ) end end |