Class: PrcLib::Logging
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.
To use the Prc::Logging system, do the following:
require 'PrcLib'
# To configure logging system:
PrcLib.app_name = 'config/app' # Define application data path as
# ~/.<app_name>.
# Ex: 'config/app' will use ~/.config/app
PrcLib.log_file = 'app.log' # Relative path to the log file name
# stored in the Application data path.
# Here, ~/.config/app/app.log
PrcLib.level = Logger::FATAL # Define printout debug level. Can be any
# Logger predefined value.
# To log some information:
PrcLib.debug('My %s debug message', 'test')
PrcLib.info('My info message')
PrcLib.warning('my warning')
PrcLib.error('my error')
PrcLib.fatal(2, "Fatal error, with return code = 2)
PrcLib.message('Only printout message')
# You can printout some instant message to the terminal, not logged.
# This is useful before any action that will take time to be executed.
# It is inform the end user that something is still running, which means
# the application is not frozen
PrcLib.state("Running a long task")
# The next message will replace the previous state message.
sleep(10)
PrcLib.info("The long task has been executed successfully.")
# You can change the logger level with PrcLib.level
PrcLib.level = Logger::DEBUG
# You can just print high level message (print without \n)
# if PrcLib.level is not DEBUG or INFO.
PrcLib.high_level_msg("Print a , not logged, if level is not DEBUG
# or INFO")
For details, see Logging functions
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
-
#debug(message) ⇒ Object
Log to STDOUT and Log file and DEBUG class message.
-
#debug? ⇒ Boolean
Is Logging print out level is debug?.
-
#error(message) ⇒ Object
Log to STDOUT and Log file and ERROR class message.
-
#error? ⇒ Boolean
Is Logging print out level is error?.
-
#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.
-
#fatal? ⇒ Boolean
Is Logging print out level is fatal?.
-
#info(message) ⇒ Object
Log to STDOUT and Log file and INFO class message.
-
#info? ⇒ Boolean
Is Logging print out level is info?.
-
#initialize ⇒ Logging
constructor
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.
-
#unknown(message) ⇒ Object
Print out a message, not logged in the log file.
-
#warn(message) ⇒ Object
Log to STDOUT and Log file and WARNING class message.
Constructor Details
#initialize ⇒ Logging
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>
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/logging.rb', line 95 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
#level ⇒ Object
Returns the value of attribute level.
85 86 87 |
# File 'lib/logging.rb', line 85 def level @level end |
Instance Method Details
#debug(message) ⇒ Object
Log to STDOUT and Log file and DEBUG class message
143 144 145 146 |
# File 'lib/logging.rb', line 143 def debug() @out_logger.debug( + ANSI.clear_eol) @file_logger.debug() end |
#debug? ⇒ Boolean
Is Logging print out level is debug?
122 123 124 |
# File 'lib/logging.rb', line 122 def debug? (@out_logger.debug?) end |
#error(message) ⇒ Object
Log to STDOUT and Log file and ERROR class message
149 150 151 152 |
# File 'lib/logging.rb', line 149 def error() @out_logger.error( + ANSI.clear_eol) @file_logger.error( + "\n" + caller.join("\n")) end |
#error? ⇒ Boolean
Is Logging print out level is error?
127 128 129 |
# File 'lib/logging.rb', line 127 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.
158 159 160 161 162 163 164 |
# File 'lib/logging.rb', line 158 def fatal(, e = nil) @out_logger.fatal( + ANSI.clear_eol) return @file_logger.fatal(format("%s\n%s\n%s", , e., e.backtrace.join("\n"))) if e @file_logger.fatal() end |
#fatal? ⇒ Boolean
Is Logging print out level is fatal?
132 133 134 |
# File 'lib/logging.rb', line 132 def fatal? (@out_logger.fatal?) end |
#info(message) ⇒ Object
Log to STDOUT and Log file and INFO class message
137 138 139 140 |
# File 'lib/logging.rb', line 137 def info() @out_logger.info( + ANSI.clear_eol) @file_logger.info() end |
#info? ⇒ Boolean
Is Logging print out level is info?
117 118 119 |
# File 'lib/logging.rb', line 117 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.
180 181 182 |
# File 'lib/logging.rb', line 180 def unknown() @out_logger.unknown( + ANSI.clear_eol) end |
#warn(message) ⇒ Object
Log to STDOUT and Log file and WARNING class message
167 168 169 170 |
# File 'lib/logging.rb', line 167 def warn() @out_logger.warn( + ANSI.clear_eol) @file_logger.warn() end |