View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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  }