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



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/chef/knife/chop/logging.rb', line 161

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] && @origins
          args[:origins] = @origins
        end
        if args[:origins] and args[:origins][:log_level]
          args[:from] = "#{args[:origins][:log_level] rescue @origins[:log_level]} "
        else
          args[:from] = ''
        end
        # @config[:log_opts] = lambda{|mlll| {
        #     :pattern      => "%#{mlll}l: %m %C\n",
        #     :date_pattern => '%Y-%m-%d %H:%M:%S',
        #   }
        # }
        #
        l_opts = args[:log_opts].call(::Logging::MAX_LEVEL_LENGTH) rescue {
            :pattern      => "#{args[: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) rescue {
            :pattern      => "#{args[: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 => :light_blue,
            :debug => :cyan,
            :info  => :green,
            :step  => :green,
            :warn  => :yellow,
            :error => :red,
            :fatal => :light_red,
            :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]
        if args[:trace]
          if ::Logging::VERSION =~ /^2/
            logger.caller_tracing = true
          else
            logger.trace = true
          end
        end
        @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




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

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

#logTodo(msg) ⇒ Object




116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef/knife/chop/logging.rb', line 116

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



157
158
159
# File 'lib/chef/knife/chop/logging.rb', line 157

def setLogger(logger)
  @logger = logger
end