-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cloud logging does not include trace ID #256
Comments
@ashih I think this can be done using Logging-Logback with TraceLoggingEnhancer. please check the below example that captures the trace ID in a Servlet filter then sets a Dependency: <dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging-logback</artifactId>
<version>0.118.2-alpha</version>
</dependency> Servlet filter : import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import com.google.cloud.logging.TraceLoggingEnhancer;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebFilter(filterName = "HelloServletFilter")
public class HelloServletFilter implements Filter {
private final Logger logger = LoggerFactory.getLogger(HelloServletFilter.class);
/**
* JoranConfigurator is used to parse the logback.xml configuration file to use logback Appender
* for logging.
*
* @param filterConfig configuration for servlet filter
*/
@Override
public void init(FilterConfig filterConfig) {
try {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
final ServletContext servletContext = filterConfig.getServletContext();
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(context);
context.reset();
// set application-name will be used while logging.
context.putProperty("application-name", "hello world");
jc.doConfigure(servletContext.getRealPath("/WEB-INF/logback.xml"));
} catch (Exception e) {
logger.error(e.getMessage());
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
// setting current traceId
TraceLoggingEnhancer.setCurrentTraceId(req.getRemoteHost() + " for " + req.getRequestURL());
chain.doFilter(request, response);
}
@Override
public void destroy() {}
} Servlet : import com.google.cloud.logging.TraceLoggingEnhancer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "helloworld", value = "")
@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
private final Logger logger = LoggerFactory.getLogger(HelloServlet.class);
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
// getting current traceId and logger is used for logging
logger.info(TraceLoggingEnhancer.getCurrentTraceId());
out.println("Hello, world - App Engine Flexible");
}
} logback.xml : <configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- "specify filepath where log file will generate -->
<!-- "application-name" is a variable -->
<File>filepath/${application-name}.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d %p %t %c - %m%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="FILE"/>
</root>
</configuration> LogEntry : |
Hi, Is there any timeline to have logs correlation available using java.util.logging? Thanks in advance! Diego |
Duplicates #688 |
Is your feature request related to a problem? Please describe.
When using cloud logging with App Engine, app logs are not correlated with request logs.
Describe the solution you'd like
I would like cloud logging to include trace ID so stackdriver can correlate the logs together. This is documented in Writing app logs.
Describe alternatives you've considered
Alternative solution is to write structured logs, but it would be nice if GAE users could use Google Cloud Logging to have correlated logs (from app to request) out of the box.
Additional context
See screenshot
The text was updated successfully, but these errors were encountered: