Class: ConvenientService::Logger

Inherits:
Logger
  • Object
show all
Includes:
Singleton
Defined in:
lib/convenient_service/logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.colored_formatterProc

Returns:

  • (Proc)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/convenient_service/logger.rb', line 75

def colored_formatter
  return original_formatter unless Dependencies.paint.loaded?

  proc do |severity, datetime, progname, message|
    log = original_formatter.call(severity, datetime, progname, message)

    case severity
    when "INFO" then ::Paint[log, :cyan, :bold]
    when "WARN" then ::Paint[log, :yellow, :bold]
    when "ERROR" then ::Paint[log, :red, :bold]
    when "FATAL" then ::Paint[log, :red, :underline]
    when "DEBUG" then ::Paint[log, :magenta, :bold]
    when "ANY" then log
    else log
    end
  end
end

.newConvenientService::Logger

rubocop:disable Style/GlobalStdStream



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/convenient_service/logger.rb', line 42

def new
  super(::STDOUT).tap do |logger|
    logger.level =
      if ::ENV["CONVENIENT_SERVICE_DEBUG"] == "true"
        ::Logger::DEBUG
      elsif ::ENV["CONVENIENT_SERVICE_LOGGER_LEVEL"]
        ::ENV["CONVENIENT_SERVICE_LOGGER_LEVEL"]
      else
        ::Logger::INFO
      end

    logger.formatter = (::ENV["CONVENIENT_SERVICE_LOGGER_ENABLE_COLORS"] == "true") ? colored_formatter : original_formatter
  end
end

.original_formatterLogger::Formatter

Returns:

  • (Logger::Formatter)


61
62
63
# File 'lib/convenient_service/logger.rb', line 61

def original_formatter
  ::Logger::Formatter.new
end

Instance Method Details

#level=(severity) ⇒ Object

TODO: Tag/prefix for all Convenient Service logs.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/convenient_service/logger.rb', line 111

def level=(severity)
  if Dependencies.support_logger_non_integer_levels?
    super(severity)
  elsif severity.instance_of?(::Integer)
    super(severity)
  else
    ::Warning.warn <<~MESSAGE
      `ConvenientService.logger.level` is reset from `#{severity}` to `Logger::INFO`.

      Stdlib `logger` with version `#{Dependencies.logger.version}` does NOT support non-integer levels.
    MESSAGE

    super(::Logger::INFO)
  end
end