Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,16 @@ public class Mongo {
@Data
public class Trace {
private boolean printAllTrace = false;
private File file = new File();
private List<String> sanitizes = new ArrayList<>();
}

@Data
public class File {
private boolean enabled = true;
private String pattern = "%m%n%rEx";
private String path = "/tmp/logs/trace.log";
}

private Middlewares middlewares = new Middlewares();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import br.com.conductor.heimdall.core.environment.Property;
import br.com.conductor.heimdall.gateway.appender.MongoDBAppender;
import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.*;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import net.logstash.logback.appender.LogstashTcpSocketAppender;
import net.logstash.logback.encoder.LogstashEncoder;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,6 +106,36 @@ public void onStartUp() {
logger.addAppender(appender);

}

if (property.getTrace().getFile().isEnabled()) {

Logger logger = (Logger) LoggerFactory.getLogger("trace");
logger.setAdditive(false);

ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(Level.ALL.levelStr);
filter.start();

PatternLayout layout = new PatternLayout();
layout.setOutputPatternAsHeader(false);
layout.setPattern(property.getTrace().getFile().getPattern());
layout.setContext(lc);
layout.start();

FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
fileAppender.setFile(property.getTrace().getFile().getPath());
fileAppender.setContext(lc);
fileAppender.setLayout(layout);
fileAppender.start();

final AsyncAppender asyncAppender = new AsyncAppender();
asyncAppender.setContext(lc);
asyncAppender.addAppender(fileAppender);
asyncAppender.addFilter(filter);
asyncAppender.start();

logger.addAppender(asyncAppender);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void doFilter(ServletRequest request, ServletResponse res, FilterChain ch
try {

trace = TraceContextHolder.getInstance().init(prop.getTrace().isPrintAllTrace(), profile, request,
prop.getMongo().getEnabled(), prop.getLogstash().getEnabled(), buildProperties.getVersion());
prop.getMongo().getEnabled(), prop.getLogstash().getEnabled(), prop.getTrace().getFile().isEnabled(), buildProperties.getVersion());
if (shouldDisableTrace(request)) {
trace.setShouldPrint(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class Trace {

private static final Logger logstash = LoggerFactory.getLogger("logstash");

private static final Logger traceLog = LoggerFactory.getLogger("trace");

private String method;

private String url;
Expand Down Expand Up @@ -126,6 +128,9 @@ public class Trace {

@JsonIgnore
private boolean printLogstash;

@JsonIgnore
private boolean traceInFile;

private String version;

Expand All @@ -141,13 +146,14 @@ public Trace() {
* @param printMongo
* @param printLogstash
*/
public Trace(boolean printAllTrace, String profile, ServletRequest servletRequest, boolean printMongo, boolean printLogstash){
public Trace(boolean printAllTrace, String profile, ServletRequest servletRequest, boolean printMongo, boolean printLogstash, boolean traceInFile){

this.shouldPrint = true;
this.profile = profile;
this.printAllTrace = printAllTrace;
this.printMongo = printMongo;
this.printLogstash = printLogstash;
this.traceInFile = traceInFile;
HttpServletRequest request = (HttpServletRequest) servletRequest;
HeimdallException.checkThrow(request == null, ExceptionMessage.GLOBAL_REQUEST_NOT_FOUND);

Expand Down Expand Up @@ -180,8 +186,8 @@ public Trace(boolean printAllTrace, String profile, ServletRequest servletReques
* @param printLogstash
* @param version
*/
public Trace(boolean printAllTrace, String profile, ServletRequest servletRequest, boolean printMongo, boolean printLogstash, String version) {
this(printAllTrace, profile, servletRequest, printMongo, printLogstash);
public Trace(boolean printAllTrace, String profile, ServletRequest servletRequest, boolean printMongo, boolean printLogstash, boolean traceInFile, String version) {
this(printAllTrace, profile, servletRequest, printMongo, printLogstash, traceInFile);
this.version = version;
}

Expand Down Expand Up @@ -292,6 +298,10 @@ private void prepareLog(Integer statusCode) throws JsonProcessingException {
if (printLogstash) {
printInLogger(logstash, statusCode);
}

if (traceInFile) {
printInLogger(traceLog, statusCode);
}
}

private void printInLogger(Logger logger, Integer statusCode) throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public static TraceContextHolder getInstance() {
* @param version
* @return {@link Trace}
*/
public Trace init(boolean printAllTrace, String profile, ServletRequest request, boolean printMongo, boolean printLogstash, String version) {
public Trace init(boolean printAllTrace, String profile, ServletRequest request, boolean printMongo, boolean printLogstash, boolean traceInFile, String version) {
String uuid = UUID.randomUUID().toString();
contextHolder.set(uuid);
traceMap.put(uuid, new Trace(printAllTrace, profile, request, printMongo, printLogstash, version));
traceMap.put(uuid, new Trace(printAllTrace, profile, request, printMongo, printLogstash, traceInFile, version));

log.debug("Initializing TraceContext with ID: {}", uuid);
return getActualTrace();
Expand Down
4 changes: 4 additions & 0 deletions heimdall-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ heimdall:
zoneId: America/Sao_Paulo
trace:
printAllTrace: true
file:
enabled: true
pattern: "%-5level %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] - %msg%n%rEx"
path: /tmp/logs/trace.log
sanitizes:
- access_token
- client_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void init() {
this.ctx.clear();
this.ctx.setRequest(this.request);
this.ctx.setResponse(this.response);
TraceContextHolder.getInstance().init(true, "developer", this.request, false, false, "");
TraceContextHolder.getInstance().init(true, "developer", this.request, false, false, false,"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void init() {
context.setRequest(this.request);
context.setResponse(this.response);
context.setResponseStatusCode(HttpStatus.OK.value());
TraceContextHolder.getInstance().init(true, "developer", this.request, false, false, "");
TraceContextHolder.getInstance().init(true, "developer", this.request, false, false, false, "");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void initTest() {
ctx.clear();
ctx.setRequest(this.request);
ctx.setResponse(this.response);
TraceContextHolder.getInstance().init(true, "developer", request, false, false, "");
TraceContextHolder.getInstance().init(true, "developer", request, false, false, false, "");

clientId = "simpleId";
someOtherClientId = "someOtherClientId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class LogMaskerServiceTest {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
TraceContextHolder.getInstance().init(true, "developer", new MockHttpServletRequest(), false, false, "");
TraceContextHolder.getInstance().init(true, "developer", new MockHttpServletRequest(), false, false, false,"");

Map<String, String> headers = new HashMap<>();
headers.put("connection", "Keep-alive");
Expand Down