001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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.access.common.pattern;
015
016import java.time.ZoneId;
017import java.util.List;
018import java.util.Locale;
019
020import ch.qos.logback.access.common.spi.IAccessEvent;
021import ch.qos.logback.core.CoreConstants;
022import ch.qos.logback.core.util.CachingDateFormatter;
023
024public class DateConverter extends AccessConverter {
025
026
027    CachingDateFormatter cachingDateFormatter = null;
028
029    @Override
030    public void start() {
031
032        String datePattern = getFirstOption();
033
034        if (datePattern == null) {
035            datePattern = CoreConstants.CLF_DATE_PATTERN;
036        } else if (datePattern.equals(CoreConstants.ISO8601_STR)) {
037            datePattern = CoreConstants.ISO8601_PATTERN;
038        } else if (datePattern.equals(CoreConstants.STRICT_STR)) {
039            datePattern = CoreConstants.STRICT_ISO8601_PATTERN;
040        }
041
042        List<String> optionList = getOptionList();
043        ZoneId zoneId = null;
044        // if the option list contains a TZ option, then set it.
045        if (optionList != null && optionList.size() > 1) {
046            String zoneIdString = (String) optionList.get(1);
047            zoneId = ZoneId.of(zoneIdString);
048        }
049        Locale locale = null;
050        if (optionList != null && optionList.size() > 2) {
051            String localeIdStr = (String) optionList.get(2);
052            locale = Locale.forLanguageTag(localeIdStr);
053            addInfo("Setting locale to \""+locale+"\"");
054        }
055
056        try {
057            // if zoneId is null, the CachingDateFormatter will use the ZoneId.systemDefault()
058            // if locale is null, the CachingDateFormatter will use the Locale.getDefault()
059            cachingDateFormatter = new CachingDateFormatter(datePattern, zoneId, locale);
060        } catch (IllegalArgumentException e) {
061            addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
062            addWarn("Defaulting to  " + CoreConstants.CLF_DATE_PATTERN);
063            cachingDateFormatter = new CachingDateFormatter(CoreConstants.CLF_DATE_PATTERN, zoneId);
064        }
065
066        super.start();
067    }
068
069    @Override
070
071    public String convert(IAccessEvent accessEvent) {
072        long timestamp = accessEvent.getTimeStamp();
073        return cachingDateFormatter.format(timestamp);
074    }
075
076    /**
077     * This method is intended for test classes. Should not be used
078     * by regular clients.
079     *
080     * @return the CachingDateFormatter in use
081     * @deprecated Will be removed in future versions with no replacement
082     */
083    @Deprecated
084    public CachingDateFormatter internalCachingDateFormatter() {
085        return cachingDateFormatter;
086    }
087}