Class: ClothesNetwork::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fox/library/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fox/library/logger.rb', line 25

def initialize

  @symbols = {
    :unknown   => [ "(UU)", "LightBlue"   ],
    :trace     => [ "(**)", "LightBlue"   ],
    :debug     => [ "(++)", "LightBlue"   ],
    :info      => [ "(--)", "Brown"       ],
    :notice    => [ "(==)", "Brown"       ],
    :warning   => [ "(WW)", "Yellow"      ],
    :error     => [ "(EE)", "LightRed"    ],
    :critical  => [ "(!!)", "LightRed"    ],
    :alert     => [ "(!!)", "LightRed"    ],
    :fatal     => [ "(!!)", "LightRed"    ],

    :success   => [ "(II)", "LightGreen"  ],
    :question  => [ "(??)", "LightCyan"   ]
  }

  @color    = true
  @silent   = false

  # # Determine what levels are relevant for XMPP based on @options.xmpp_message_level
  # levels          = @symbols.keys.to_a
  # @xmpp_relevant  = levels.slice( levels.index( @options.xmpp_message_level ), levels.length ) unless( @xmpp.nil? )

  # # Handle XMPP Setup
  # @xmpp     = nil

  # if( @options.xmpp )
  #   # FIXME: Re-write wrapper to a more proper form
  #   # @xmpp         = XMPPLogger.new( @options.server_xmpp_id, @options.server_xmpp_password, @options.client_xmpp_ids )
  #   # @xmpp.level   = Logger::ERROR # default level on which messages will be sent
  #   # @xmpp.error "Houston, we have a problem"

  #   @xmpp   = Jabber::Simple.new( @options.server_xmpp_id, @options.server_xmpp_password )
  # end
end

Instance Attribute Details

#colorObject

of def message }}}



168
169
170
# File 'lib/fox/library/logger.rb', line 168

def color
  @color
end

#silentObject

of def message }}}



168
169
170
# File 'lib/fox/library/logger.rb', line 168

def silent
  @silent
end

Instance Method Details

#colorize(color, message) ⇒ String

Note:

This might not work for your terminal

Black 0;30 Dark Gray 1;30 Blue 0;34 Light Blue 1;34 Green 0;32 Light Green 1;32 Cyan 0;36 Light Cyan 1;36 Red 0;31 Light Red 1;31 Purple 0;35 Light Purple 1;35 Brown 0;33 Yellow 1;33 Light Gray 0;37 White 1;37

Parameters:

  • color (String)

    The colorname in plain english. e.g. “LightGray”, “Gray”, “Red”, “BrightRed”

  • message (String)

    The message which should be wrapped

Returns:

  • (String)

    Colorized message string

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fox/library/logger.rb', line 82

def colorize color, message 

  colors  = {
    "Gray"        => "\e[1;30m",
    "LightGray"   => "\e[0;37m",
    "Cyan"        => "\e[0;36m",
    "LightCyan"   => "\e[1;36m",
    "Blue"        => "\e[0;34m",
    "LightBlue"   => "\e[1;34m",
    "Green"       => "\e[0;32m",
    "LightGreen"  => "\e[1;32m",
    "Red"         => "\e[0;31m",
    "LightRed"    => "\e[1;31m",
    "Purple"      => "\e[0;35m",
    "LightPurple" => "\e[1;35m",
    "Brown"       => "\e[0;33m",
    "Yellow"      => "\e[1;33m",
    "White"       => "\e[1;37m",
    "NoColor"     => "\e[0m"
  }

  raise ArgumentError, "Function arguments cannot be nil" if( color.nil? or message.nil? )
  raise ArgumentError, "Unknown color" unless( colors.keys.include?( color ) )

  colors[ color ] + message + colors[ "NoColor" ]
end

#message(level, msg) ⇒ Object

Helpers: colorize

Parameters:

  • level (Symbol)

    Ruby symbol, can either be :info, :success, :error or :question

  • msg (String)

    String, which represents the message you want to send to stdout (info, ok, question) stderr (error)

Raises:

  • (ArugmentError)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fox/library/logger.rb', line 119

def message level, msg #, colorize = @options.colorize, silent = @options.silent

  # return if( silent )

  raise ArugmentError, "Can't find the corresponding symbol for this message level (#{level.to_s}) - is the spelling wrong?" unless( @symbols.key?( level )  )

  print = []

  output = ( level == :error ) ? ( "STDERR.puts" ) : ( "STDOUT.puts" )
  print << output
  print << "colorize(" if( @color )
  print << "\"" + @symbols[ level ].last + "\"," if( @color )
  print << "\"#{@symbols[ level ].first.to_s} #{msg.to_s}\""
  print << ")" if( @color )

  print.clear if( @silent )

  eval( print.join( " " ) )

  # # Send messages also to XMPP clients if activated and in level of interest
  # if( @options.xmpp )

  #   @options.client_xmpp_ids.each do |address|
  #     next unless( @xmpp_relevant.include?( level ) )

  #     @xmpp.deliver( address, @symbols[level].first.to_s + " " + msg.to_s )
  #   end
  # end

  # # Send messages also to AMQP queue if activated and in the level of interest
  # if( @options.amqp )

  #   EventMachine.next_tick do
  #     AMQP.channel ||= AMQP::Channel.new(AMQP.connection)

  #     # Durable -> http://rubydoc.info/github/ruby-amqp/amqp/master/AMQP/Channel
  #     AMQP.channel.queue("#{@options.amqp_server_routing_key_base.to_s}.logger", :durable => true)

  #     3.times do |i|
  #       puts "[auth][after_fork/amqp] Publishing a warmup message ##{i}"

  #       AMQP.channel.default_exchange.publish( @symbols[level].first.to_s + " " + msg.to_s, :routing_key => "#{@options.amqp_server_routing_key_base.to_s}.logger")
  #     end # of 3.times
  #   end # of EventMachine

  # end # of if( @options.amqp )

end