Class: Gat::Logger

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/gat/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#get_debug_levels, #print_debug_msg

Constructor Details

#initialize(gatget) ⇒ Logger

Returns a new instance of Logger.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gat/logger.rb', line 29

def initialize(gatget)
  @global_types   = { 'trace'    => { 'description' => 'Traces debug logs', 'verbose' => 4 },
    'message'  => { 'description' => 'Message by gat',    'verbose' => 3 },
    'warning'  => { 'description' => 'Warnings',          'verbose' => 2 },
    'error'    => { 'description' => 'Errors at Gatget',  'verbose' => 1 },
    'direct'   => { 'description' => 'Direct Outputs',    'verbose' => 0 }
  }

  @gatget         = gatget || raise(GatgetException.new("Logger needs a gatget object", "initialize_logger"))
  @times          = Hash.new
  @times['init']  = Time.now
  @entries        = Array.new
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



27
28
29
# File 'lib/gat/logger.rb', line 27

def entries
  @entries
end

#gatgetObject (readonly)

Returns the value of attribute gatget.



23
24
25
# File 'lib/gat/logger.rb', line 23

def gatget
  @gatget
end

#global_typesObject (readonly)

Returns the value of attribute global_types.



24
25
26
# File 'lib/gat/logger.rb', line 24

def global_types
  @global_types
end

#timesObject

Returns the value of attribute times.



26
27
28
# File 'lib/gat/logger.rb', line 26

def times
  @times
end

Instance Method Details

#human_sumeObject



107
108
109
110
111
112
113
# File 'lib/gat/logger.rb', line 107

def human_sume
  process_resume = "Process Resume:\n---------------\n\n"
  process_resume << " - Times: \n    * start:      #{ self.gatget.times['init'] }\n    * now:        #{ Time.now }\n    * secs:       #{ Time.now - self.gatget.times['init']} \n    * min:        #{ (Time.now - self.gatget.times['init'])/60 } \n    * hours:      #{ ( Time.now - self.gatget.times['init'])/3600 } \n\n"
  process_resume << " - Logger:\n    * msgs:       #{ self.entries.size }\n    * warnings:   #{ self.entries.select{|e| e.global_type == 'warning' }.size }\n    * errors:     #{ self.entries.select{|e| e.global_type == 'error' }.size }\n\n"
  process_resume << " - Whole log: \n#{ self.entries.map{ |e| "    * #{ e.inspect } \n" } }"
  process_resume
end

#log(*args) ⇒ Object

Log method Store logs entry into entries array instance attribute



47
48
49
50
51
# File 'lib/gat/logger.rb', line 47

def log(*args)
  log_entry = LogEntry.new(self, *args)
  direct_output?(log_entry)
  @entries << log_entry
end

#output(type) ⇒ Object

Gatget Logger output When operation or whatever is finish, gatget logger throw the outputs

3 Standar types are defined

- Template output: Template output works with ERB standard template rubye engine. Template is evalued at printed as direct log
     Template is searched at <gatget_path>/templates/<operation_name>.erb

- Email output:    Email output deliver log to default email config or raise Exception. Default email config must be stored at
     @gatget.config['email_config']. Multiple to address can be defined and smtp && sendmail deliver methods are avalaible

- YML output:    YML output consist in a dump of logger to a yml structure file, so, then, log can be easily interpreted. YML log must be stored
     at /var/log/gat/<gatget_name>_<operation_name>_<time>.yml, so, log folder must we writtable or exception will be launched.

Gat is planned to have more custom outputs by yml config like

At operation definition (gatget yml config)

operations:

example_operation:
  outputs: [ custom_template, custom_email]

and then, at templates definition

templates:

custom_template:
  type: template
  config:
    path: <your custom template path>
    log_level: <direct|warning|whatever>
custom_email:
  type: email
  config:
    <overwrite here default email config>

But it is still on mind :D



90
91
92
93
94
95
96
97
# File 'lib/gat/logger.rb', line 90

def output(type)
  case type
  when 'template','email','yml'
    send("output_#{ type }")
  else
    # Now, do nothing! :D
  end
end

#to_yamlObject



99
100
101
102
103
104
105
# File 'lib/gat/logger.rb', line 99

def to_yaml
  entries_hash_array = []
  @entries.each do |entry|
    entries_hash_array << entry.to_hash
  end
  entries_hash_array.to_yaml
end