Module: Sharp::Logging

Included in:
Application
Defined in:
lib/sharp/logging.rb

Defined Under Namespace

Classes: MultiIO

Instance Method Summary collapse

Instance Method Details

#attach_logger(obj) ⇒ Object



67
68
69
70
# File 'lib/sharp/logging.rb', line 67

def attach_logger(obj)
  objects_logger_is_attached_to << obj
  obj.logger = logger
end

#log_fileObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sharp/logging.rb', line 23

def log_file
  @log_file ||= begin
    log_dir = root.join("log")

    unless File.exists?(log_dir)
      FileUtils.mkdir(log_dir)
    end

    File.expand_path("#{env}.log", log_dir)
  end
end

#log_ioObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sharp/logging.rb', line 35

def log_io
  @log_io ||= begin
    io = MultiIO.new(File.open(log_file, 'a'))

    if env == :development && [:server, :console].include?(command)
      io.add_target(STDOUT)
    end

    io
  end
end

#log_levelObject



47
48
49
50
51
52
# File 'lib/sharp/logging.rb', line 47

def log_level
  @log_level ||= begin
    ENV['SHARP_LOG_LEVEL'] ||= env == :production ? 'info' : 'debug'
    Logger.const_get(ENV['SHARP_LOG_LEVEL'].upcase)
  end
end

#loggerObject



54
55
56
57
58
59
60
61
# File 'lib/sharp/logging.rb', line 54

def logger
  @logger ||= begin
    logger = Logger.new(log_io)
    logger.formatter = logger_formatter
    logger.level = log_level
    logger
  end
end

#logger=(logger) ⇒ Object



72
73
74
75
76
# File 'lib/sharp/logging.rb', line 72

def logger=(logger)
  objects_logger_is_attached_to.each do |object|
    object.logger = logger
  end
end

#logger_formatterObject



78
79
80
81
82
# File 'lib/sharp/logging.rb', line 78

def logger_formatter
  @logger_formatter ||= proc do |severity, datetime, progname, msg|
    "#{datetime} #{severity} #{msg}\n"
  end
end

#objects_logger_is_attached_toObject



63
64
65
# File 'lib/sharp/logging.rb', line 63

def objects_logger_is_attached_to
  @objects_logger_is_attached_to ||= [self]
end