Class: PrcLib::Logging

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

Overview

Class used to create 2 logger object, in order to keep track of error in a log file and change log output to OUTPUT on needs (option flags). The idea is that everytime, if you did not set the debug level mode, you can refer to the log file which is already configured with Logger::DEBUG level.

As well, sometimes, you do not want to keep track on messages that are just to keep informed the end user about activity. So, this object implement 2 Logger objects.

  • One for log file

  • One for print out.

Everytime you log a message with Logging, it is printed out if the level permits and stored everytime in the log file, never mind about Logger level set. In several cases, messages are printed out, but not stored in the log file.

See Logging functions for details.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogging

Initialize Logging instance The log file name is defined by PrcLib.log_file The log path is defined by PrcLib.app_name and will be kept as ~/.<PrcLib.app_name> The log level is defined by PrcLib.level. It will update only log print out. Depending on levels, messages are prefixed by colored ‘ERROR!!!’, ‘FATAL!!!’, ‘WARNING’ or <LEVEL NAME>



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/logging.rb', line 102

def initialize
  file_logger_initialize

  @out_logger = Logger.new(STDOUT)
  @level = (PrcLib.level.nil? ? Logger::WARN : PrcLib.level)
  @out_logger.level = @level
  @out_logger.formatter = proc do |severity, _datetime, _progname, msg|
    case severity
    when 'ANY'
      str = "#{msg} \n"
    when 'ERROR', 'FATAL'
      str = ANSI.bold(ANSI.red("#{severity}!!!")) + ": #{msg} \n"
    when 'WARN'
      str = ANSI.bold(ANSI.yellow('WARNING')) + ": #{msg} \n"
    else
      str = "#{severity}: #{msg} \n"
    end
    str
  end
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



92
93
94
# File 'lib/logging.rb', line 92

def level
  @level
end

Instance Method Details

#debug(message) ⇒ Object

Log to STDOUT and Log file and DEBUG class message



150
151
152
153
# File 'lib/logging.rb', line 150

def debug(message)
  @out_logger.debug(message + ANSI.clear_line)
  @file_logger.debug(message)
end

#debug?Boolean

Is Logging print out level is debug?

Returns:

  • (Boolean)


129
130
131
# File 'lib/logging.rb', line 129

def debug?
  (@out_logger.debug?)
end

#error(message) ⇒ Object

Log to STDOUT and Log file and ERROR class message



156
157
158
159
# File 'lib/logging.rb', line 156

def error(message)
  @out_logger.error(message + ANSI.clear_line)
  @file_logger.error(message)
end

#error?Boolean

Is Logging print out level is error?

Returns:

  • (Boolean)


134
135
136
# File 'lib/logging.rb', line 134

def error?
  (@out_logger.error?)
end

#fatal(message, e = nil) ⇒ Object

Log to STDOUT and Log file and FATAL class message fatal retrieve the caller list of functions and save it to the log file if the exception class is given. The exception class should provide message and backtrace.



165
166
167
168
169
170
171
172
# File 'lib/logging.rb', line 165

def fatal(message, e = nil)
  @out_logger.fatal(message + ANSI.clear_line)
  @file_logger.fatal(format("%s\n%s\n%s",
                            message,
                            e.message,
                            e.backtrace.join("\n"))) if e
  @file_logger.fatal(message)
end

#fatal?Boolean

Is Logging print out level is fatal?

Returns:

  • (Boolean)


139
140
141
# File 'lib/logging.rb', line 139

def fatal?
  (@out_logger.fatal?)
end

#info(message) ⇒ Object

Log to STDOUT and Log file and INFO class message



144
145
146
147
# File 'lib/logging.rb', line 144

def info(message)
  @out_logger.info(message + ANSI.clear_line)
  @file_logger.info(message)
end

#info?Boolean

Is Logging print out level is info?

Returns:

  • (Boolean)


124
125
126
# File 'lib/logging.rb', line 124

def info?
  (@out_logger.info?)
end

#unknown(message) ⇒ Object

Print out a message, not logged in the log file. This message is printed out systematically as not taking care of logger level.



188
189
190
# File 'lib/logging.rb', line 188

def unknown(message)
  @out_logger.unknown(message + ANSI.clear_line)
end

#warn(message) ⇒ Object

Log to STDOUT and Log file and WARNING class message



175
176
177
178
# File 'lib/logging.rb', line 175

def warn(message)
  @out_logger.warn(message + ANSI.clear_line)
  @file_logger.warn(message)
end