1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.kryo;
15
16 import ch.qos.logback.classic.LoggerContext;
17 import ch.qos.logback.classic.joran.JoranConfigurator;
18 import ch.qos.logback.classic.util.LogbackMDCAdapter;
19 import ch.qos.logback.core.joran.spi.JoranException;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import static ch.qos.logback.core.kryo.KryoCoreConstants.KRYO_UNSAFE_PROPERTY_KEY;
25
26 public class KryoDecoderApp {
27
28 public static void main(String[] args) throws IOException, JoranException {
29 System.setProperty(KRYO_UNSAFE_PROPERTY_KEY, "false");
30
31 if (args.length != 2) {
32 usage();
33 System.exit(1);
34 }
35
36 System.out.println("In KryoDecoderApp main()");
37 LoggerContext loggerContext = (LoggerContext) new LoggerContext();
38 LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
39 loggerContext.setMDCAdapter(logbackMDCAdapter);
40 loggerContext.setName("KryoDecoderApp");
41
42 String filename = args[0];
43 String configFile = args[1];
44
45 configureLoggerContext(loggerContext, configFile);
46
47 KryoDecoder decoder = new KryoDecoder(new File(filename));
48 KryoDecoder.DecodeStats stats = decoder.decodeAndAppend(loggerContext);
49
50 System.out.println("Decoded " + stats.count() + " logging events from file: " + filename
51 + " (version=" + (decoder.getStreamVersion() & 0xFF)
52 + ", serializer=" + decoder.getSerializerType()
53 + ", decodeNanos=" + stats.decodeNanos()
54 + ", consumeNanos=" + stats.consumeNanos() + ")");
55 }
56
57 private static void configureLoggerContext(LoggerContext loggerContext, String configFile) throws JoranException {
58 JoranConfigurator configurator = new JoranConfigurator();
59 configurator.setContext(loggerContext);
60 configurator.doConfigure(configFile);
61 }
62
63 private static void usage() {
64 System.err.println("Usage: java KryoDecoderApp <filename> <configFile>");
65 System.err.println(" <filename> The name of the file to decode (must include Kryo stream header)");
66 System.err.println(" <configFile> The logback configuration file");
67 System.err.println("Serializer type and stream version are read from the file header.");
68 }
69 }