-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathTkSlf4j.java
More file actions
132 lines (124 loc) · 3.68 KB
/
TkSlf4j.java
File metadata and controls
132 lines (124 loc) · 3.68 KB
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* SPDX-FileCopyrightText: Copyright (c) 2014-2026 Yegor Bugayenko
* SPDX-License-Identifier: MIT
*/
package org.takes.tk;
import java.io.IOException;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.cactoos.Func;
import org.cactoos.map.MapEntry;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;
/**
* Logs Take.act() calls.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.11.2
*/
@ToString(of = {"origin", "target"})
@EqualsAndHashCode
public final class TkSlf4j implements Take {
/**
* Original take.
*/
private final Take origin;
/**
* Log target.
*/
private final String target;
/**
* Ctor.
* @param take Original
*/
public TkSlf4j(final Take take) {
this(take, "org.takes.tk.TkSlf4j");
}
/**
* Ctor.
* @param take Original
* @param tgt Log target
*/
public TkSlf4j(final Take take, final String tgt) {
this.target = tgt;
this.origin = take;
}
@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public Response act(final Request req) throws Exception {
final long mark = System.currentTimeMillis();
final Logger logger = LoggerFactory.getLogger(this.target);
final Func<MapEntry<String, Object[]>, String> entry =
params -> new Joined(
new TextOf(" "),
new FormattedText(
"[%s %s]",
new RqMethod.Base(req).method(),
new RqHref.Base(req).href()
),
new FormattedText(params.getKey(), params.getValue()),
new FormattedText(
"started %d in %d ms",
mark,
System.currentTimeMillis() - mark
)
).asString();
try {
final Response rsp = this.origin.act(req);
if (logger.isInfoEnabled()) {
logger.info(
"{}",
entry.apply(
new MapEntry<>(
"returned \"%s\"",
new Object[]{rsp.head().iterator().next()}
)
)
);
}
return rsp;
} catch (final IOException ex) {
if (logger.isInfoEnabled()) {
logger.info(
"{}",
entry.apply(
new MapEntry<>(
"thrown %s(\"%s\")",
new Object[]{
ex.getClass().getCanonicalName(),
ex.getLocalizedMessage(),
}
)
)
);
}
throw ex;
// @checkstyle IllegalCatchCheck (1 line)
} catch (final RuntimeException ex) {
if (logger.isInfoEnabled()) {
logger.info(
"{}",
entry.apply(
new MapEntry<>(
"thrown runtime %s(\"%s\")",
new Object[]{
ex.getClass().getCanonicalName(),
ex.getLocalizedMessage(),
}
)
)
);
}
throw ex;
}
}
}