Module: Dunlop::NestedLogger
- Defined in:
- app/services/dunlop/nested_logger.rb
Overview
Defined Under Namespace
Classes: BaseScope, Entry, Scope
Class Method Summary
collapse
Class Method Details
.benchmark ⇒ Object
28
29
30
31
32
33
|
# File 'app/services/dunlop/nested_logger.rb', line 28
def benchmark
Thread.current[:nested_logger_benchmark] = true
yield
ensure
Thread.current[:nested_logger_benchmark] = false
end
|
.benchmark? ⇒ Boolean
35
36
37
|
# File 'app/services/dunlop/nested_logger.rb', line 35
def benchmark?
Thread.current[:nested_logger_benchmark] == true
end
|
.copy_scope_stack_for_merge ⇒ Object
78
79
80
|
# File 'app/services/dunlop/nested_logger.rb', line 78
def copy_scope_stack_for_merge
Marshal.load(Marshal.dump(scope_stack))
end
|
.current_scope ⇒ Object
62
63
64
|
# File 'app/services/dunlop/nested_logger.rb', line 62
def current_scope
scope_stack.last
end
|
.empty_scope_stack ⇒ Object
Also known as:
reset
57
58
59
|
# File 'app/services/dunlop/nested_logger.rb', line 57
def empty_scope_stack
Thread.current[:nested_logger_scope_stack] = nil
end
|
.flush ⇒ Object
86
87
88
|
# File 'app/services/dunlop/nested_logger.rb', line 86
def flush
current_scope.pop_entries.map(&:to_entry) || []
end
|
.flush_as_yaml ⇒ Object
90
91
92
|
# File 'app/services/dunlop/nested_logger.rb', line 90
def flush_as_yaml
(f = flush.presence) ? YAML.dump(f, Dunlop::NestedLogger.options[:yaml_options]).sub("---\n", '') : ''
end
|
16
17
18
19
20
21
22
|
# File 'app/services/dunlop/nested_logger.rb', line 16
def formatter
Thread.current[:nested_logger_formatter] ||= begin
proc do |severity, datetime, progname, msg|
"#{severity == :warn ? 'WARNING' : severity.upcase} - #{msg}"
end
end
end
|
6
7
8
|
# File 'app/services/dunlop/nested_logger.rb', line 6
def formatter=(formatter)
Thread.current[:nested_logger_formatter] = formatter
end
|
.log(options = {}) ⇒ Object
94
95
96
|
# File 'app/services/dunlop/nested_logger.rb', line 94
def log(options={})
current_scope.log(options) if current_scope
end
|
.merge_scope_stack(stack) ⇒ Object
82
83
84
|
# File 'app/services/dunlop/nested_logger.rb', line 82
def merge_scope_stack(stack)
current_scope.entries += stack.pop.entries
end
|
.merge_threads(threads) ⇒ Object
70
71
72
73
74
75
76
|
# File 'app/services/dunlop/nested_logger.rb', line 70
def merge_threads(threads)
threads.map do |thread|
if top_thread_scope = Array.wrap(thread[:nested_logger_scope_stack]).pop
current_scope.entries += top_thread_scope.entries
end
end
end
|
.options ⇒ Object
10
11
12
13
14
|
# File 'app/services/dunlop/nested_logger.rb', line 10
def options
@options ||= {
yaml_options: {}
}
end
|
.parent_scope? ⇒ Boolean
66
67
68
|
# File 'app/services/dunlop/nested_logger.rb', line 66
def parent_scope?
scope_stack.size > 1
end
|
24
25
26
|
# File 'app/services/dunlop/nested_logger.rb', line 24
def reset_formatter
Thread.current[:nested_logger_formatter] = nil
end
|
.scope(label, options = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'app/services/dunlop/nested_logger.rb', line 39
def scope(label, options={})
scope_stack << Scope.new(label)
r0 = Time.now
return_value = yield
duration = Time.now - r0
info("duration = #{duration}s") if options.fetch(:benchmark, benchmark?)
if parent_scope?
closing_scope = scope_stack.pop
current_scope.add_scope(closing_scope) unless closing_scope.empty?
end
return_value
end
|
.scope_stack ⇒ Object
53
54
55
|
# File 'app/services/dunlop/nested_logger.rb', line 53
def scope_stack
Thread.current[:nested_logger_scope_stack] ||= [BaseScope.new]
end
|