1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.issue.lbclassic135;
15
16 import ch.qos.logback.classic.Logger;
17 import ch.qos.logback.classic.LoggerContext;
18 import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
19 import ch.qos.logback.classic.spi.ILoggingEvent;
20 import ch.qos.logback.core.FileAppender;
21 import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
22 import ch.qos.logback.core.contention.ThreadedThroughputCalculator;
23
24
25
26
27
28
29 public class LoggingToFileThroughput {
30
31 static int THREAD_COUNT = 1;
32 static long OVERALL_DURATION_IN_MILLIS = 5000;
33
34 public static void main(String args[]) throws InterruptedException {
35
36 ThreadedThroughputCalculator tp = new ThreadedThroughputCalculator(OVERALL_DURATION_IN_MILLIS);
37 tp.printEnvironmentInfo("lbclassic135 LoggingToFileThrouhput");
38
39 LoggerContext lc = new LoggerContext();
40 Logger logger = buildLoggerContext(lc);
41
42 for (int i = 0; i < 2; i++) {
43 tp.execute(buildArray(logger));
44 }
45
46 RunnableWithCounterAndDone[] runnnableArray = buildArray(logger);
47 tp.execute(runnnableArray);
48 tp.printThroughput(runnnableArray, "File: ");
49 lc.stop();
50 }
51
52 static Logger buildLoggerContext(LoggerContext lc) {
53 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
54
55 PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
56 patternLayout.setContext(lc);
57 patternLayout.setPattern("%d %l [%t] - %msg%n");
58 patternLayout.start();
59 FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
60 fileAppender.setContext(lc);
61 fileAppender.setFile("target/lbclassic135.log");
62 fileAppender.setEncoder(patternLayout);
63 fileAppender.setAppend(false);
64 fileAppender.start();
65 root.addAppender(fileAppender);
66 return lc.getLogger(LoggingToFileThroughput.class);
67 }
68
69 static LoggingRunnable[] buildArray(Logger logger) {
70
71 LoggingRunnable[] array = new LoggingRunnable[THREAD_COUNT];
72 for (int i = 0; i < THREAD_COUNT; i++) {
73 array[i] = new LoggingRunnable(logger);
74 }
75 return array;
76 }
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117