Module: Chef::Knife::ChopLogging

Included in:
ChopBase
Defined in:
lib/chef/knife/chop/logging.rb

Defined Under Namespace

Classes: FakeLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/chef/knife/chop/logging.rb', line 10

def args
  @args
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/chef/knife/chop/logging.rb', line 9

def logger
  @logger
end

#stepObject (readonly)

Returns the value of attribute step.



11
12
13
# File 'lib/chef/knife/chop/logging.rb', line 11

def step
  @step
end

#TODOObject (readonly)

Returns the value of attribute TODO.



12
13
14
# File 'lib/chef/knife/chop/logging.rb', line 12

def TODO
  @TODO
end

Instance Method Details

#getLogger(args, from = '', alogger = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/chef/knife/chop/logging.rb', line 148

def getLogger(args,from='',alogger=nil)
  logger = alogger || @logger
  unless logger
    unless from==''
      from = "#{from} - "
    end
    @step = 0
    if args
      if args.key?(:log_file) and args[:log_file]
        args[:log_path] = File.dirname(args[:log_file])
      elsif args[:my_name]
        if args[:log_path]
          args[:log_file] = "#{args[:log_path]}/#{args[:my_name]}.log"
        else
          args[:log_file] = "/tmp/#{args[:my_name]}.log"
        end
      end

      begin
        ::Logging.init :trace, :debug, :info, :step, :warn, :error, :fatal, :todo unless defined? ::Logging::MAX_LEVEL_LENGTH
        if args[:origins] and args[:origins][:log_level]
          if (::Logging::LEVELS[args[:log_level].to_s] and ::Logging::LEVELS[args[:log_level].to_s] < 2)
            #puts "#{args[:log_level].to_s} = #{::Logging::LEVELS[args[:log_level].to_s]}".light_yellow
            puts "#{args[:origins][:log_level]} says #{args[:log_level]}".light_yellow
          else
            from = ''
          end
        end
        l_opts = args[:log_opts].call(::Logging::MAX_LEVEL_LENGTH) || {
            :pattern      => "#{from}%d %#{::Logging::MAX_LEVEL_LENGTH}l: %m\n",
            :date_pattern => '%Y-%m-%d %H:%M:%S',
        }
        logger = ::Logging.logger( STDOUT, l_opts)
        l_opts = args[:log_opts].call(::Logging::MAX_LEVEL_LENGTH) || {
            :pattern      => "#{from}%d %#{::Logging::MAX_LEVEL_LENGTH}l: %m %C\n",
            :date_pattern => '%Y-%m-%d %H:%M:%S',
        }
        layout = ::Logging::Layouts::Pattern.new(l_opts)

        if args[:log_file] and args[:log_file].instance_of?(String)
          dev = args[:log_file]
          a_opts = Hash.new
          a_opts[:filename] = dev
          a_opts[:layout] = layout
          a_opts.merge! l_opts

          name = case dev
                   when String; dev
                   when File; dev.path
                   else dev.object_id.to_s end

          appender =
              case dev
                when String
                  ::Logging::Appenders::RollingFile.new(name, a_opts)
                else
                  ::Logging::Appenders::IO.new(name, dev, a_opts)
              end
          logger.add_appenders appender
        end

        scheme = ::Logging::ColorScheme.new( 'christo', :levels => {
            :trace => [:blue, :on_white],
            :debug => :cyan,
            :info  => :green,
            :step  => :green,
            :warn  => :yellow,
            :error => :red,
            :fatal => [:red, :on_white],
            :todo  => :purple,
        }).scheme
        scheme[:todo]  = "\e[38;5;55m"
        l_opts[:color_scheme] = 'christo'
        layout = ::Logging::Layouts::Pattern.new(l_opts)

        appender = logger.appenders[0]
        appender.layout = layout
        logger.remove_appenders appender
        logger.add_appenders appender

        logger.level = args[:log_level] ? args[:log_level] : :warn
        logger.trace = true if args[:trace]
        @args = args
      rescue Gem::LoadError
        logger = FakeLogger.new
      rescue => e
        # not installed
        logger = FakeLogger.new
      end
      @TODO = {} if @TODO.nil?
    end # if args
    @logger = alogger || logger
  end # unless logger
  logger
end

#logStep(msg) ⇒ Object




129
130
131
132
133
# File 'lib/chef/knife/chop/logging.rb', line 129

def logStep(msg)
  if logger = getLogger(@args,'logStep')
    logger.step "Resource #{@step+=1}: #{msg} ..."
  end
end

#logTodo(msg) ⇒ Object




103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/knife/chop/logging.rb', line 103

def logTodo(msg)

  # Regular expression used to parse out caller information
  #
  # * $1 == filename
  # * $2 == line number
  # * $3 == method name (might be nil)
  caller_rgxp = %r/([-\.\/\(\)\w]+):(\d+)(?::in `(\w+)')?/o
  #CALLER_INDEX = 2
  caller_index = ((defined? JRUBY_VERSION and JRUBY_VERSION[%r/^1.6/]) or (defined? RUBY_ENGINE and RUBY_ENGINE[%r/^rbx/i])) ? 0 : 0
  stack = Kernel.caller
  return if stack.nil?

  match = caller_rgxp.match(stack[caller_index])
  file = match[1]
  line = Integer(match[2])
  modl = match[3] unless match[3].nil?

  unless @TODO["#{file}::#{line}"]
    le = ::Logging::LogEvent.new(@logger, ::Logging::LEVELS['todo'], msg, false)
    @logger.logEvent(le)
    @TODO["#{file}::#{line}"] = true
  end
end

#setLogger(logger) ⇒ Object



144
145
146
# File 'lib/chef/knife/chop/logging.rb', line 144

def setLogger(logger)
  @logger = logger
end