-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathmdc.rb
83 lines (70 loc) · 2.76 KB
/
mdc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# :stopdoc:
#
# A diagnostic context allows us to attach state information to every log
# message. This is useful for applications that serve client requests -
# information about the client can be included in the log messages for the
# duration of the request processing. This allows you to identify related log
# messages in concurrent system.
#
# The Mapped Diagnostic Context tracks state information in a collection of
# key/value pairs. In this example we are creating a few threads that will log
# quotes from famous people. Each thread has its own diagnostic context
# containing the name of the famous person.
#
# Our PatternLayout is configured to attach the "first" and the "last" name of
# our famous person to each log message.
#
require 'logging'
# log the first and last names of the celebrity with each quote
Logging.appenders.stdout(
:layout => Logging.layouts.pattern(:pattern => '%X{first} %X{last}: %m\n')
)
log = Logging.logger['User']
log.add_appenders 'stdout'
log.level = :debug
Logging.mdc['first'] = 'John'
Logging.mdc['last'] = 'Doe'
# in this first thread we will log some quotes by Alan Rickman
t1 = Thread.new {
Logging.mdc['first'] = 'Alan'
Logging.mdc['last'] = 'Rickman'
[ %q{I've never been able to plan my life. I just lurch from indecision to indecision.},
%q{If only life could be a little more tender and art a little more robust.},
%q{I do take my work seriously and the way to do that is not to take yourself too seriously.},
%q{I'm a quite serious actor who doesn't mind being ridiculously comic.}
].each { |quote|
sleep rand
log.info quote
}
}
# in this second thread we will log some quotes by William Butler Yeats
t2 = Thread.new {
Logging.mdc['first'] = 'William'
Logging.mdc['middle'] = 'Butler'
Logging.mdc['last'] = 'Yeats'
[ %q{Tread softly because you tread on my dreams.},
%q{The best lack all conviction, while the worst are full of passionate intensity.},
%q{Education is not the filling of a pail, but the lighting of a fire.},
%q{Do not wait to strike till the iron is hot; but make it hot by striking.},
%q{People who lean on logic and philosophy and rational exposition end by starving the best part of the mind.}
].each { |quote|
sleep rand
log.info quote
}
}
# and in this third thread we will log some quotes by Bono
t3 = Thread.new {
Logging.mdc.clear # otherwise we inherit the last name "Doe"
Logging.mdc['first'] = 'Bono'
[ %q{Music can change the world because it can change people.},
%q{The less you know, the more you believe.}
].each { |quote|
sleep rand
log.info quote
}
}
t1.join
t2.join
t3.join
log.info %q{and now we are done}
# :startdoc: