001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.kryo;
015
016import ch.qos.logback.classic.LoggerContext;
017import ch.qos.logback.classic.joran.JoranConfigurator;
018import ch.qos.logback.classic.util.LogbackMDCAdapter;
019import ch.qos.logback.core.joran.spi.JoranException;
020
021import java.io.File;
022import java.io.IOException;
023
024import static ch.qos.logback.core.kryo.KryoCoreConstants.KRYO_UNSAFE_PROPERTY_KEY;
025
026public class KryoDecoderApp {
027
028    public static void main(String[] args) throws IOException, JoranException {
029        System.setProperty(KRYO_UNSAFE_PROPERTY_KEY, "false");
030
031        if (args.length != 2) {
032            usage();
033            System.exit(1);
034        }
035
036        System.out.println("In KryoDecoderApp main()");
037        LoggerContext loggerContext = (LoggerContext) new LoggerContext();
038        LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
039        loggerContext.setMDCAdapter(logbackMDCAdapter);
040        loggerContext.setName("KryoDecoderApp");
041
042        String filename = args[0];
043        String configFile = args[1];
044
045        configureLoggerContext(loggerContext, configFile);
046
047        KryoDecoder decoder = new KryoDecoder(new File(filename));
048        KryoDecoder.DecodeStats stats = decoder.decodeAndAppend(loggerContext);
049
050        System.out.println("Decoded " + stats.count() + " logging events from file: " + filename
051                + " (version=" + (decoder.getStreamVersion() & 0xFF)
052                + ", serializer=" + decoder.getSerializerType()
053                + ", decodeNanos=" + stats.decodeNanos()
054                + ", consumeNanos=" + stats.consumeNanos() + ")");
055    }
056
057    private static void configureLoggerContext(LoggerContext loggerContext, String configFile) throws JoranException {
058        JoranConfigurator configurator = new JoranConfigurator();
059        configurator.setContext(loggerContext);
060        configurator.doConfigure(configFile);
061    }
062
063    private static void usage() {
064        System.err.println("Usage: java KryoDecoderApp <filename> <configFile>");
065        System.err.println("  <filename>   The name of the file to decode (must include Kryo stream header)");
066        System.err.println("  <configFile> The logback configuration file");
067        System.err.println("Serializer type and stream version are read from the file header.");
068    }
069}