Class: RubyJobs::Logging::PlainLogger

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

Direct Known Subclasses

ProgressLogger

Constant Summary collapse

DEFAULT_PATH =
"log/plain"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#extensionObject

Returns the value of attribute extension.



16
17
18
# File 'lib/logging/plain_logger.rb', line 16

def extension
  @extension
end

#pathObject

Returns the value of attribute path.



16
17
18
# File 'lib/logging/plain_logger.rb', line 16

def path
  @path
end

Returns the value of attribute print_timestamp.



16
17
18
# File 'lib/logging/plain_logger.rb', line 16

def print_timestamp
  @print_timestamp
end

#separatorObject

Returns the value of attribute separator.



16
17
18
# File 'lib/logging/plain_logger.rb', line 16

def separator
  @separator
end

#time_prefixObject

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

Returns:

  • (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_nameObject



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_pathObject



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(*message)
  message = message.map(&:to_s).join(" ")
  message = "#{timestamp} - #{message}" if @print_timestamp

  puts message

  message
end

#log_dirObject



26
27
28
29
30
# File 'lib/logging/plain_logger.rb', line 26

def log_dir
  dir = File.expand_path(@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(message)
  if @puts
    if @clear_puts
      @puts.call ((@last_message ? (("\b" * @last_message.length) + message) : message)), file_name
    else
      @puts.call @separator if @separater
      @puts.call message, file_name
    end
    @last_message = message
  end

  if @write_file
    file do |file|
      file.puts @separator if @separater
      file.puts message
    end
  end
end

#timestampObject



78
79
80
# File 'lib/logging/plain_logger.rb', line 78

def timestamp
  "[#{Time.now.strftime "%Y-%m-%d %H:%M:%S"}]"
end