Module: DLDInternet::Mixlib::Logging

Defined in:
lib/dldinternet/mixlib/logging.rb

Defined Under Namespace

Modules: ClassMethods Classes: FakeLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



404
405
406
# File 'lib/dldinternet/mixlib/logging.rb', line 404

def logger
  @logger
end

#logger_argsObject (readonly)

Returns the value of attribute logger_args.



405
406
407
# File 'lib/dldinternet/mixlib/logging.rb', line 405

def logger_args
  @logger_args
end

#stepObject (readonly)

Returns the value of attribute step.



406
407
408
# File 'lib/dldinternet/mixlib/logging.rb', line 406

def step
  @step
end

#TODOObject (readonly)

Returns the value of attribute TODO.



407
408
409
# File 'lib/dldinternet/mixlib/logging.rb', line 407

def TODO
  @TODO
end

Instance Method Details

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



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/dldinternet/mixlib/logging.rb', line 461

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
        initLogging(args)
        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[:origins][:log_level]} says #{args[:log_level]}".light_yellow
          else
            from = ''
          end
        end
        l_opts = if args[:log_opts].is_a?(Proc)
                   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',
                   }
                 else
                   args[:log_opts]
                 end
        logger = ::Logging.logger( $stdout, l_opts)
        l_opts = if args[:log_opts].is_a?(Proc)
                   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',
                   }
                 else
                   args[:log_opts]
                 end
        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

        # Create the default scheme if none given ...
        unless l_opts.has_key? :color_scheme
          lcs = ::Logging::ColorScheme.new( 'dldinternet', :levels => {
              :trace => :blue,
              :debug => :cyan,
              :info  => :green,
              :note  => :green,
              :step  => :green,
              :warn  => :yellow,
              :error => :red,
              :fatal => :red,
              :todo  => :purple,
          })
          scheme = lcs.scheme
          scheme['trace'] = "\e[38;5;89m"
          scheme['fatal'] = "\e[38;5;33m"
          scheme['todo']  = "\e[38;5;55m"
          lcs.scheme scheme
          l_opts[:color_scheme] = 'dldinternet'
        end
        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
        if args[:trace]
          if ::Logging::VERSION =~ /^2/
            logger.caller_tracing = true
          else
            logger.trace = true
          end
        end
        @logger_args = args
      rescue Gem::LoadError
        logger = FakeLogger.new
      rescue Exception => e
        puts e
        # not installed
        logger = FakeLogger.new
      end
      @TODO = {} if @TODO.nil?
    end # if args
    @logger = alogger || logger
  end # unless logger
  logger
end

#included(includer) ⇒ Object

getLogger



579
580
581
# File 'lib/dldinternet/mixlib/logging.rb', line 579

def included(includer)
  includer.extend(ClassMethods)
end

#initLogging(args) ⇒ Object



457
458
459
# File 'lib/dldinternet/mixlib/logging.rb', line 457

def initLogging(args)
  ::Logging.init(args[:log_levels] || [ :trace, :debug, :info, :step, :warn, :error, :fatal, :todo ]) unless defined? ::Logging::MAX_LEVEL_LENGTH
end

#logStep(msg, cat = 'Step') ⇒ Object




437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/dldinternet/mixlib/logging.rb', line 437

def logStep(msg,cat='Step')
  logger = getLogger(@logger_args, 'logStep')
  if logger
    if logger.get_trace
      ::Logging::LogEvent.caller_index += 1
    end
    logger.step "#{cat} #{@step+=1}: #{msg} ..."
    if logger.get_trace
      ::Logging::LogEvent.caller_index -= 1
    end
  end
end

#logTodo(msg) ⇒ Object




410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/dldinternet/mixlib/logging.rb', line 410

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 we've already logged this TODO ...
  unless @TODO["#{file}::#{line}"]
    le = ::Logging::LogEvent.new(@logger, ::Logging::LEVELS['todo'], msg, false)
    @logger.logEvent(le)
    @TODO["#{file}::#{line}"] = msg
  end
end

#setLogger(logger) ⇒ Object


Set up logger



453
454
455
# File 'lib/dldinternet/mixlib/logging.rb', line 453

def setLogger(logger)
  @logger = logger
end