Articles index:
- Introduction (Everyone)
- JSON as logs format (Everyone)
- Logging best practices with Logback (Targetting Java DEVs)
- Logging cutting-edge practices (Targetting Java DEVs)
- Contract first log generator (Targetting Java DEVs)
- ElasticSearch VRR Estimation Strategy (Targetting OPS)
- VRR Java + Logback configuration (Targetting OPS)
- VRR FileBeat configuration (Targetting OPS)
- VRR Logstash configuration and Index templates (Targetting OPS)
- VRR Curator configuration (Targetting OPS)
- Logstash Grok, JSON Filter and JSON Input performance comparison (Targetting OPS)
VRR Java + Logback configuration
Applying VRR to Java Logback application.
Example application defining importance per line using Structured Arguments
Product Owner, OPS and Developers have agreed to use tag/flag/mark "importance" with possible values "LOW" for lowest importance, "IMP" for mid-importance and "CRIT" for critical importance.
Example code without comments available here.
public class VRR {
private static final String IMPORTANCE = "importance";
private static final StructuredArgument LOW = kv(IMPORTANCE, "LOW"); //CREATING OBJECTS
private static final StructuredArgument IMP = kv(IMPORTANCE, "IMP"); //TO REUSE AND
private static final StructuredArgument CRIT = kv(IMPORTANCE, "CRIT"); //AVOID REWRITING
private static final Logger logger = LoggerFactory.getLogger(VRR.class);
public static void main(String[] args) {
MDC.put("rid", UUID.randomUUID().toString()); //SAME MDC USAGE AVAILABLE
try {
long startTime = currentTimeMillis();
someFunction();
logger.info("important message, useful to so some metrics {} {}",
kv("elapsedmillis", currentTimeMillis() - startTime),
IMP); //IMPORTANT MESSAGE
} catch (Exception e) {
logger.error("This is a low importance message as it won't have value after few weeks",
e); //THIS IS A LOW IMPORTANCE MESSAGE AS IT'S NOT TAGGED
}
}
static void someFunction() throws Exception {
logger.info("low importance message, helps to trace errors, begin someFunction {} {} {}",
kv("user","anavarro"),
kv("action","file-create"),
LOW); //LOW IMPORTANCE TAGGED MESSAGE, SLIGHTLY REDUNDANT, SAME THAN UNTAGGED
Thread.sleep(500L); //some work
logger.info("critical message, audit trail for user action {} {} {}",
kv("user","anavarro"),
kv("action","file-create"),
CRIT); //CRITICAL MESSAGE
}
}
Previously mentioned logback.xml configuration
<configuration>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="stash">
<file>logFile.json</file>
<rollingpolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<filenamepattern>file.log.%d{yyyy-MM-dd}</filenamepattern>
<maxhistory>30</maxhistory>
</rollingpolicy>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<threadname>
<mdc>
<loggername>
<message>
<loglevel>
<arguments>
<stacktrace>
<stackhash>
</stackhash></stacktrace></arguments></loglevel></message></loggername></mdc></threadname></timestamp></providers>
</encoder>
</appender>
<appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="all">
<appender-ref ref="stash">
<appender-ref ref="STDOUT">
</appender-ref></appender-ref></root>
</configuration>
Code and configuration together, we get a result as JSON files as follow:
{"@timestamp":"2018-09-11T00:05:11.746+02:00","thread_name":"main",
"rid":"7fac3070-0d7e-40a6-a3e8-246ec95e86e7","logger_name":"VRR",
"message":"low importance message, helps to trace errors, begin someFunction
user=anavarro action=file-create importance=LOW","level":"INFO","user":"anavarro",
"action":"file-create","importance":"LOW"}
{"@timestamp":"2018-09-11T00:05:12.271+02:00","thread_name":"main",
"rid":"7fac3070-0d7e-40a6-a3e8-246ec95e86e7","logger_name":"VRR",
"message":"critical message, audit trail for user action
user=anavarro action=file-create importance=CRIT","level":"INFO","user":"anavarro",
"action":"file-create","importance":"CRIT"}
{"@timestamp":"2018-09-11T00:05:12.274+02:00","thread_name":"main",
"rid":"7fac3070-0d7e-40a6-a3e8-246ec95e86e7","logger_name":"VRR",
"message":"important message, useful to so some metrics elapsedmillis=528 importance=IMP",
"level":"INFO","elapsedmillis":528,"importance":"IMP"}
Next: 8 - VRR FileBeat configuration
No hay comentarios:
Publicar un comentario
Nota: solo los miembros de este blog pueden publicar comentarios.