Class: Cosmos::RawLogger
Overview
Creates a log file of raw data for either reads or writes. Can automatically cycle the log based on when the log file reaches a predefined size.
Constant Summary collapse
- LOG_TYPES =
The allowable log types
[:READ, :WRITE]
- CYCLE_TIME_INTERVAL =
The cycle time interval. Cycle times are only checked at this level of granularity.
60
Instance Attribute Summary collapse
-
#filename ⇒ String
readonly
The filename of the log.
-
#logging_enabled ⇒ Boolean
readonly
Is logging enabled?.
-
#orig_name ⇒ String
readonly
Original name passed to raw logger.
-
#queue ⇒ Queue
readonly
Queue for asynchronous logging.
Instance Method Summary collapse
-
#clone ⇒ Object
Create a clone of this object with a new name.
-
#initialize(log_name, log_type, log_directory, logging_enabled = false, cycle_size = 2000000000) ⇒ RawLogger
constructor
A new instance of RawLogger.
-
#name=(log_name) ⇒ Object
Set the raw logger name.
-
#start ⇒ Object
Starts a new log file by closing the existing log file.
-
#stop ⇒ Object
Stops all logging and closes the current log file.
-
#write(data) ⇒ Object
Write data to the log file.
Constructor Details
#initialize(log_name, log_type, log_directory, logging_enabled = false, cycle_size = 2000000000) ⇒ RawLogger
Returns a new instance of RawLogger.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cosmos/io/raw_logger.rb', line 54 def initialize( log_name, log_type, log_directory, logging_enabled = false, cycle_size = 2000000000 ) raise "log_type must be :READ or :WRITE" unless LOG_TYPES.include? log_type @log_type = log_type @orig_name = log_name @log_name = (log_name.to_s.downcase + '_raw_' + @log_type.to_s.downcase + '_' + self.object_id.to_s).freeze @log_directory = log_directory @cycle_size = ConfigParser.handle_nil(cycle_size) @cycle_size = Integer(@cycle_size) if @cycle_size @mutex = Mutex.new @file = nil @filename = nil @start_time = Time.now.sys @logging_enabled = ConfigParser.handle_true_false(logging_enabled) end |
Instance Attribute Details
#filename ⇒ String (readonly)
Returns The filename of the log.
29 30 31 |
# File 'lib/cosmos/io/raw_logger.rb', line 29 def filename @filename end |
#logging_enabled ⇒ Boolean (readonly)
Returns Is logging enabled?.
35 36 37 |
# File 'lib/cosmos/io/raw_logger.rb', line 35 def logging_enabled @logging_enabled end |
#orig_name ⇒ String (readonly)
Returns Original name passed to raw logger.
38 39 40 |
# File 'lib/cosmos/io/raw_logger.rb', line 38 def orig_name @orig_name end |
#queue ⇒ Queue (readonly)
Returns Queue for asynchronous logging.
32 33 34 |
# File 'lib/cosmos/io/raw_logger.rb', line 32 def queue @queue end |
Instance Method Details
#clone ⇒ Object
Create a clone of this object with a new name
125 126 127 128 129 |
# File 'lib/cosmos/io/raw_logger.rb', line 125 def clone raw_logger = super() raw_logger.name = raw_logger.orig_name raw_logger end |
#name=(log_name) ⇒ Object
Set the raw logger name
78 79 80 81 |
# File 'lib/cosmos/io/raw_logger.rb', line 78 def name=(log_name) @orig_name = log_name @log_name = (log_name.to_s.downcase + '_raw_' + @log_type.to_s.downcase + '_' + self.object_id.to_s).freeze end |
#start ⇒ Object
Starts a new log file by closing the existing log file. New log files are not created until data is written by #write so this does not immediately create a log file on the filesystem.
113 114 115 116 |
# File 'lib/cosmos/io/raw_logger.rb', line 113 def start close_file() unless (Time.now.sys - @start_time) < 1.0 # Prevent close/open too fast @mutex.synchronize { @logging_enabled = true } end |
#stop ⇒ Object
Stops all logging and closes the current log file.
119 120 121 122 |
# File 'lib/cosmos/io/raw_logger.rb', line 119 def stop @mutex.synchronize { @logging_enabled = false } close_file() end |
#write(data) ⇒ Object
Write data to the log file.
If no log file currently exists in the filesystem, a new file will be created.
Writing a log file is a critical operation so the entire method is wrapped with a rescue and handled with handle_critical_exception
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cosmos/io/raw_logger.rb', line 92 def write(data) if @logging_enabled return if !data or data.length <= 0 need_new_file = false @mutex.synchronize do if !@file or (@cycle_size and (@file.stat.size + data.length) > @cycle_size) need_new_file = true end end start_new_file() if need_new_file @mutex.synchronize { @file.write(data) if @file } end rescue => err Logger.instance.error "Error writing #{@filename} : #{err.formatted}" Cosmos.handle_critical_exception(err) end |