Class: RubyJobs::Logging::PlainLogger
- Inherits:
-
Object
- Object
- RubyJobs::Logging::PlainLogger
- Defined in:
- lib/logging/plain_logger.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_PATH =
"log/plain"
Instance Attribute Summary collapse
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#path ⇒ Object
Returns the value of attribute path.
-
#print_timestamp ⇒ Object
Returns the value of attribute print_timestamp.
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#time_prefix ⇒ Object
Returns the value of attribute time_prefix.
Instance Method Summary collapse
- #def_puts(write_file = false, clear = false, &block) ⇒ Object
- #exist? ⇒ Boolean
- #file(&block) ⇒ Object
- #file_name ⇒ Object
- #file_path ⇒ Object
-
#initialize(*names) ⇒ PlainLogger
constructor
A new instance of PlainLogger.
- #log(*message) ⇒ Object
- #log_dir ⇒ Object
- #puts(message) ⇒ Object
- #timestamp ⇒ Object
Constructor Details
#initialize(*names) ⇒ PlainLogger
Returns a new instance of PlainLogger.
8 9 10 11 12 13 14 |
# File 'lib/logging/plain_logger.rb', line 8 def initialize(*names) @names = names.compact @path = DEFAULT_PATH @time_prefix = true @print_timestamp = false @write_file = true end |
Instance Attribute Details
#extension ⇒ Object
Returns the value of attribute extension.
16 17 18 |
# File 'lib/logging/plain_logger.rb', line 16 def extension @extension end |
#path ⇒ Object
Returns the value of attribute path.
16 17 18 |
# File 'lib/logging/plain_logger.rb', line 16 def path @path end |
#print_timestamp ⇒ Object
Returns the value of attribute print_timestamp.
16 17 18 |
# File 'lib/logging/plain_logger.rb', line 16 def @print_timestamp end |
#separator ⇒ Object
Returns the value of attribute separator.
16 17 18 |
# File 'lib/logging/plain_logger.rb', line 16 def separator @separator end |
#time_prefix ⇒ Object
Returns the value of attribute time_prefix.
16 17 18 |
# File 'lib/logging/plain_logger.rb', line 16 def time_prefix @time_prefix end |
Instance Method Details
#def_puts(write_file = false, clear = false, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/logging/plain_logger.rb', line 68 def def_puts(write_file=false, clear=false, &block) @clear_puts = clear @write_file = write_file if block_given? @puts = block else @puts = clear ? Proc.new{|m| print m} : Proc.new{|m| Kernel.puts m} end end |
#exist? ⇒ Boolean
22 23 24 |
# File 'lib/logging/plain_logger.rb', line 22 def exist? File.exist? file_path end |
#file(&block) ⇒ Object
18 19 20 |
# File 'lib/logging/plain_logger.rb', line 18 def file(&block) File.open(file_path, 'a+', &block) end |
#file_name ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/logging/plain_logger.rb', line 36 def file_name return @file_name if @file_name file_name = "" if @time_prefix file_name << Time.now.strftime("%Y%m%d%H%M%S") file_name << "_" end file_name << @names.map{|w| w.to_s.gsub(/^[\W_]*|[\W_]*$/, '').gsub(/[\W_]+/, '_')}.join("_") file_name << ".#{extension}" if extension @file_name = file_name end |
#file_path ⇒ Object
32 33 34 |
# File 'lib/logging/plain_logger.rb', line 32 def file_path File.join(log_dir, file_name) end |
#log(*message) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/logging/plain_logger.rb', line 82 def log(*) = .map(&:to_s).join(" ") = "#{} - #{}" if @print_timestamp puts end |
#log_dir ⇒ Object
26 27 28 29 30 |
# File 'lib/logging/plain_logger.rb', line 26 def log_dir dir = File.(@path, APP_ROOT) FileUtils.mkpath dir dir end |
#puts(message) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/logging/plain_logger.rb', line 49 def puts() if @puts if @clear_puts @puts.call ((@last_message ? (("\b" * @last_message.length) + ) : )), file_name else @puts.call @separator if @separater @puts.call , file_name end @last_message = end if @write_file file do |file| file.puts @separator if @separater file.puts end end end |
#timestamp ⇒ Object
78 79 80 |
# File 'lib/logging/plain_logger.rb', line 78 def "[#{Time.now.strftime "%Y-%m-%d %H:%M:%S"}]" end |