Class: DL::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/app-ctx/dlog.rb

Constant Summary collapse

LevelMetrics =
{
    :fatal  => 0.0,
    :error  => 1.0,
    :warn   => 2.0,
    :info   => 3.0,
    :debug  => 4.0,
}
DefaultLogger =

XXX das mit diesem config kram ist alles noch ziemlicher mist…

{
    :prefix         => "???",
    :out            => $stderr,
    :fmt            => "%c %23s  %s%s\n", # "%c %-13s: %s%s\n",
    #:include_levels => /^(fatal|error|warn|info)/i, # no debug
    :include_levels => nil, # all inclusive
    #:exclude_levels => nil,
    :exclude_levels => /^debug/i, # no debug on default 
    :quiet          => false,
    :level          => 3.0, 
}
Config =
{
    :default    => DefaultLogger,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, config = DefaultLogger) ⇒ Logger

Returns a new instance of Logger.



38
39
40
41
42
43
# File 'lib/app-ctx/dlog.rb', line 38

def initialize prefix, config = DefaultLogger
    @nesting_level = 0
    @config = DefaultLogger.dup
    @config[:prefix] = prefix
    configure config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



111
112
113
114
115
116
# File 'lib/app-ctx/dlog.rb', line 111

def method_missing(method, *args, &block)
    message, execption = args
    if LevelMetrics[method.to_sym] <= level
        log(method.to_s.upcase, message, execption, &block)
    end
end

Instance Attribute Details

#exclude_levelsObject

Returns the value of attribute exclude_levels.



35
36
37
# File 'lib/app-ctx/dlog.rb', line 35

def exclude_levels
  @exclude_levels
end

#fmtObject

Returns the value of attribute fmt.



35
36
37
# File 'lib/app-ctx/dlog.rb', line 35

def fmt
  @fmt
end

#include_levelsObject

Returns the value of attribute include_levels.



35
36
37
# File 'lib/app-ctx/dlog.rb', line 35

def include_levels
  @include_levels
end

#levelObject

Returns the value of attribute level.



36
37
38
# File 'lib/app-ctx/dlog.rb', line 36

def level
  @level
end

#outObject

Returns the value of attribute out.



35
36
37
# File 'lib/app-ctx/dlog.rb', line 35

def out
  @out
end

#prefixObject

Returns the value of attribute prefix.



35
36
37
# File 'lib/app-ctx/dlog.rb', line 35

def prefix
  @prefix
end

#quietObject

Returns the value of attribute quiet.



36
37
38
# File 'lib/app-ctx/dlog.rb', line 36

def quiet
  @quiet
end

Class Method Details

.[](prefix) ⇒ Object



56
57
58
# File 'lib/app-ctx/dlog.rb', line 56

def self.[] prefix
    self.get prefix
end

Instance Method Details

#configure(settings) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/app-ctx/dlog.rb', line 45

def configure settings
    @config.update(settings)
    @prefix         = @config[:prefix]
    @out            = @config[:out]
    @fmt            = @config[:fmt]
    @include_levels = @config[:include_levels]
    @exclude_levels = @config[:exclude_levels]
    @quiet          = @config[:quiet]         
    @level          = @config[:level]
end

#flushObject



84
85
86
# File 'lib/app-ctx/dlog.rb', line 84

def flush
    @out.flush
end

#indent(&block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/app-ctx/dlog.rb', line 88

def indent(&block)
    @nesting_level += 1
    if block_given?
        begin
            block.call
        ensure
            outdent
        end
    end
end

#log(status, message, exception, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/app-ctx/dlog.rb', line 60

def log(status, message, exception, &block)
    unless quiet
        #return "excluded" if exclude_levels and exclude_levels.match(status)
        #if include_levels and !include_levels.match(status)
        #    return "not included"
        #end
        msg = @fmt % [status[0], prefix, '  ' * @nesting_level, message]
        @out.print(msg)
        if exception
            callstack = exception.backtrace
            @out.print " !! #{exception}\n\t#{callstack.join("\n\t")}\n"
            begin
                reason = exception.reason
                callstack = reason.backtrace
                txt = "#{reason}\n\t#{callstack.join("\n\t")}\n"
                @out.print "(Cause)>> #{txt}"
            rescue 
                # noop
            end
        end
    end
    indent(&block) if block_given?
end

#outdentObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/app-ctx/dlog.rb', line 99

def outdent
    @nesting_level -= 1
    if block_given?
        begin
            block.call
        ensure
            indent
        end
    end
end