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 v1.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.pattern;
015
016import ch.qos.logback.classic.spi.ILoggingEvent;
017
018/**
019 * The EpochConverter class extends the ClassicConverter to handle the conversion of logging event
020 * timestamps into epoch time. This class allows control over whether the output epoch time is
021 * represented in milliseconds (default) or seconds.
022 *
023 * @since 1.5.25
024 */
025public class EpochConverter extends ClassicConverter {
026
027    // assume output in milliseconds by default
028    boolean inUnitsOfSeconds = false;
029
030    @Override
031    public void start() {
032        String millisOrSecondsStr = getFirstOption();
033        if ("seconds".equalsIgnoreCase(millisOrSecondsStr)) {
034            inUnitsOfSeconds = true;
035        }
036
037        super.start();
038
039    }
040
041    @Override
042    public String convert(ILoggingEvent event) {
043
044        if(inUnitsOfSeconds) {
045            return Long.toString(event.getTimeStamp() / 1000);
046        } else {
047            return Long.toString(event.getTimeStamp());
048        }
049    }
050}