Class: Logger::Logging::Factory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Factory

Returns a new instance of Factory.



5
6
7
# File 'lib/logger/logging/factory.rb', line 5

def initialize env
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/logger/logging/factory.rb', line 3

def env
  @env
end

Class Method Details

.call(env = nil) ⇒ Object



9
10
11
12
13
# File 'lib/logger/logging/factory.rb', line 9

def self.call env = nil
  env ||= ENV
  instance = new env
  instance.()
end

Instance Method Details

#callObject



15
16
17
18
19
# File 'lib/logger/logging/factory.rb', line 15

def call
  logger = logger_class.new device
  logger.level = level
  logger
end

#coloring?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
# File 'lib/logger/logging/factory.rb', line 50

def coloring?
  return @coloring unless @coloring.nil?
  @coloring =
    begin
      use_colors = env.fetch "LOG_COLOR" do
        if device.tty? then "on" else "off" end
      end
      use_colors == "on"
    end
end

#deviceObject



21
22
23
24
25
26
27
# File 'lib/logger/logging/factory.rb', line 21

def device
  @device ||=
    begin
      path = env.fetch "LOG_DEVICE" do "/dev/stdout" end
      if path.is_a? String then File.open path, "a" else path end
    end
end

#levelObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logger/logging/factory.rb', line 29

def level
  @level ||=
    begin
      level_name = env.fetch "LOG_LEVEL" do "WARN" end
      level_name = level_name.upcase

      if Logger.const_defined? level_name
        Logger.const_get level_name
      else
        {
          "DATA" => -2,
          "TRACE" => -1,
        }.fetch level_name
      end
    end
end

#logger_classObject



46
47
48
# File 'lib/logger/logging/factory.rb', line 46

def logger_class
  if coloring? then ColoredLogger else ExtendedLogger end
end