Showing posts with label Logging. Show all posts
Showing posts with label Logging. Show all posts

Java Logging Tips/Tricks - 2016


Don't log sensitive information
Include as much dynamic info as possible
Paste logging in PR if applicable
Make sure there is enough logging to help trouble shooting issues in production later

sfl4j
log.warn("hello: {}, {}", "A", "B", new RuntimeException("wtf"));
- will print the exception's stack trace
org.slf4j.helpers.MessageFormatter.arrayFormat(String, Object[])
Throwable throwableCandidate = getThrowableCandidate(argArray);

Logback
-Dlogback.debug=true
<configuration debug="true">

Log method and line number
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n

Pattern
Use %M to print method name in debug environment.
%class{1}

MDC - Mapped Diagnostic Context
%X{X-RequestId}, %X{sessionId}
MDC.put("X-RequestId", requestId)
-- Don't forget to MDC.clear() at the beginning and end of the request.

Use MDC in thread pools
@SuppressWarnings("serial")
public class MdcThreadPoolExecutor extends ThreadPoolTaskExecutor {
    public MdcThreadPoolExecutor() {
        super();
    }
    @Override
    public void execute(final Runnable command) {
        super.execute(wrap(command, MDC.getCopyOfContextMap()));
    }
    public static Runnable wrap(final Runnable runnable, final Map<String, String> context) {
        return new Runnable() {
            @Override
            public void run() {
                final Map<String, String> previous = MDC.getCopyOfContextMap();
                if (context == null) {
                    MDC.clear();
                } else {
                    MDC.setContextMap(context);
                }
                try {
                    runnable.run();
                } finally {
                    if (previous == null) {
                        MDC.clear();
                    } else {
                        MDC.setContextMap(previous);
                    }
                }
            }
        };
    }
}
Duplicated log - fix: additivity="false"
Only log stats into app-stats.log

- Don't log to app.log and stdout(in dev)

Async logging

<root level="INFO">
  <appender-ref ref="dailyrolling" />
  <appender-ref ref="STDOUT" />
</root>
<logger name="common.statistic" level="INFO" additivity="false">
  <appender-ref ref="statsRolling" />
</logger>
<appender name="statsRolling-async" class="ch.qos.logback.classic.AsyncAppender">
  <queueSize>1000</queueSize>
  <discardingThreshold>0</discardingThreshold>
  <appender-ref ref="statsRolling" />
</appender>
<if condition='property("env").equals("local") || property("env").equals("docker")'>
  <then>
  </then>
  <else>
    <root level="WARN">
      <appender-ref ref="dailyrolling-async" />
    </root>
    <logger name="com.xyz.services" level="INFO">
      <appender-ref ref="dailyrolling-async" />
    </logger>
    <logger name="com.xyz.statistic" level="INFO">
      <appender-ref ref="statsRolling-async" />
    </logger>
  </else>
</if>
Parameterized Logging
logger.error("xx {} yy {} xx {}", x, y, z);

SiftingAppender

Log4j
-Dlog4j.debug

Log4j2
https://logging.apache.org/log4j/2.0/faq.html#troubleshooting
-DLog4jDefaultStatusLevel=DEBUG
-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE
Lazy Logging
logger.trace("Some long-running operation returned {}", () -> expensiveOperation());

Logging level configuration
Logging Cassandra Query CQL
Set log level of com.datastax.driver.core.RequestHandler = TRACE
Programmatic configuration of slf4j/logback

splunk - logging best practice
Use clear key-value pairs

To clear log without delete the file
cp /dev/null log
> log

CurlLogger: Log Http Request as cUrl Command


During development, we may want to log http request as cUrl command, so if the request failed, we can easily run it in cmd and check what's wrong, such as: are some header/params missing, ectc

Android already has CurlLogger.java, based on it, I wrote the following code in our spring application to log cUrl command in development mode.
CurlLoggerRequestInterceptor
@Component
public class CurlLoggerRequestInterceptor implements ClientHttpRequestInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(CurlLoggerRequestInterceptor.class);
    private static final Joiner JOINER = Joiner.on(",").skipNulls();

    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {
        logger.info(toCurl(request, true));

        return execution.execute(request, body);
    }
    private static String toCurl(HttpRequest request, boolean logAuthToken) throws IOException {
        StringBuilder builder = new StringBuilder().append("curl ");
        for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
            builder.append("--header \'");
            if (!logAuthToken && (entry.getKey().equals("Authorization") || entry.getKey().equals("Cookie"))) {
                builder.append(entry.getKey()).append(": ").append("${token}");
            } else {
                builder.append(entry.getKey()).append(": ").append(JOINER.join(entry.getValue()));
            }
            builder.append("\' ");
        }
        URI uri = request.getURI();
        if (request instanceof HttpRequestWrapper) {
            HttpRequest original = ((HttpRequestWrapper) request).getRequest();
            uri = original.getURI();
        }
        builder.append("\'").append(uri).append("\'");

        return builder.toString();
    }
}
RestTemplateConfig
@Configuration
public class RestTemplateConfig {
    @Value("${enable.curl.log:false}")
    private boolean enableCurlLog;
    @Autowired
    private ClientHttpRequestInterceptor curlLoggerRequestInterceptor;
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate template = new RestTemplate(clientFactory());
        ArrayList<ClientHttpRequestInterceptor> interceptors = Lists.newArrayList();
        if (enableCurlLog) {
            interceptors.add(curlLoggerRequestInterceptor);
        }
        template.setInterceptors(interceptors);
        return template;
    }
}

Resource
Android CurlLogger.java

Labels

ANT (6) Algorithm (69) Algorithm Series (35) Android (7) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) JSON (7) Java (186) JavaScript (27) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) adsense (5) bat (8) regex (5) xml (5)