Class: DtkLogger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dtk_logger.rb

Constant Summary collapse

LOG_FILE_NAME =
'client.log'
LOG_MB_SIZE =
2
LOG_NUMBER_OF_OLD_FILES =
10
DEVELOPMENT_MODE =
DTK::Configuration.get(:development_mode)
LoggerMethods =
[:debug, :info, :warn, :error, :fatal, :error_pp, :fatal_pp]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDtkLogger

Returns a new instance of DtkLogger.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dtk_logger.rb', line 38

def initialize
  log_location_dir = DTK::Client::OsUtil.get_log_location()
  begin
    if File.directory?(log_location_dir)
      file = File.open(file_path(), "a")
      file.sync = true
      @logger = Logger.new(file, LOG_NUMBER_OF_OLD_FILES, LOG_MB_SIZE * 1024000)

      @logger.formatter = proc do |severity, datetime, progname, msg|
        "[#{datetime}] #{severity} -- : #{msg}\n"
      end
    else
      no_log_dir(log_location_dir)
    end
   rescue Errno::EACCES
    no_log_permissions(log_location_dir)
  end
end

Class Method Details

.method_missing(method, *args) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/dtk_logger.rb', line 62

def self.method_missing(method, *args)
  if LoggerMethods.include?(method)
    instance.send(method, *args)
  else
    super(method, *args)
  end
end

.respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/dtk_logger.rb', line 70

def self.respond_to?(method)
  LoggerMethods.include?(method) or super(method)
end

Instance Method Details

#debug(log_text, sttdout_out = false) ⇒ Object



74
75
76
77
# File 'lib/dtk_logger.rb', line 74

def debug(log_text, sttdout_out=false)
  puts log_text if sttdout_out || DEVELOPMENT_MODE
  @logger.debug(log_text) if log_created?
end

#error(log_text, sttdout_out = false) ⇒ Object



89
90
91
92
# File 'lib/dtk_logger.rb', line 89

def error(log_text, sttdout_out=false)
  DTK::Client::OsUtil.print(log_text, :red) if sttdout_out || DEVELOPMENT_MODE
  @logger.error(log_text) if log_created?
end

#error_pp(message, backtrace, sttdout_out = true) ⇒ Object



94
95
96
97
98
# File 'lib/dtk_logger.rb', line 94

def error_pp(message, backtrace, sttdout_out = true)
  error(message, sttdout_out)
  # we do not print this to STDOUT (will be overriden with DEVELOPMENT_MODE)s
  error("#{message}\n" + PP.pp(backtrace, ""), false) if backtrace
end

#fatal(log_text, sttdout_out = false) ⇒ Object



107
108
109
110
# File 'lib/dtk_logger.rb', line 107

def fatal(log_text, sttdout_out=false)
  puts log_text if sttdout_out || DEVELOPMENT_MODE
  @logger.fatal(log_text) if log_created?
end

#fatal_pp(message, backtrace, sttdout_out = true) ⇒ Object



100
101
102
103
104
# File 'lib/dtk_logger.rb', line 100

def fatal_pp(message, backtrace, sttdout_out = true)
  fatal(message, sttdout_out)
  # we do not print this to STDOUT (will be overriden with DEVELOPMENT_MODE)
  fatal("#{message}\n" + PP.pp(backtrace, ""), false) if backtrace
end

#file_pathObject



57
58
59
# File 'lib/dtk_logger.rb', line 57

def file_path()
  "#{DTK::Client::OsUtil.get_log_location()}/#{LOG_FILE_NAME}"
end

#info(log_text, sttdout_out = false) ⇒ Object



79
80
81
82
# File 'lib/dtk_logger.rb', line 79

def info(log_text, sttdout_out=false)
  puts log_text if sttdout_out || DEVELOPMENT_MODE
  @logger.info(log_text) if log_created?
end

#warn(log_text, sttdout_out = false) ⇒ Object



84
85
86
87
# File 'lib/dtk_logger.rb', line 84

def warn(log_text, sttdout_out=false)
  DTK::Client::OsUtil.print(log_text, :yellow) if sttdout_out || DEVELOPMENT_MODE
  @logger.warn(log_text) if log_created?
end