001/*
002 * This is free and unencumbered software released into the public domain.
003 *
004 * Anyone is free to copy, modify, publish, use, compile, sell, or
005 * distribute this software, either in source code form or as a compiled
006 * binary, for any purpose, commercial or non-commercial, and by any
007 * means.
008 *
009 * In jurisdictions that recognize copyright laws, the author or authors
010 * of this software dedicate any and all copyright interest in the
011 * software to the public domain. We make this dedication for the benefit
012 * of the public at large and to the detriment of our heirs and
013 * successors. We intend this dedication to be an overt act of
014 * relinquishment in perpetuity of all present and future rights to this
015 * software under copyright law.
016 *
017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
018 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
020 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
021 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
022 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
023 * OTHER DEALINGS IN THE SOFTWARE.
024 *
025 * For more information, please refer to <http://unlicense.org/>.
026 */
027
028package hm.binkley.util.logging;
029
030import ch.qos.logback.classic.pattern.ClassicConverter;
031import ch.qos.logback.classic.spi.ILoggingEvent;
032import org.slf4j.LoggerFactory;
033
034import java.util.Date;
035import java.util.List;
036import java.util.TimeZone;
037
038/**
039 * {@code TimeZoneConverter} provides the local timezone in {@link TimeZone#SHORT} or {@link
040 * TimeZone#LONG} format.  Default is short.
041 *
042 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley (binkley)</a>
043 */
044public final class TimeZoneConverter
045        extends ClassicConverter {
046    private int format;
047
048    @SuppressWarnings("unchecked")
049    @Override
050    public void start() {
051        final List<String> options = getOptionList();
052        if (null == options)
053            format = TimeZone.SHORT;
054        else if (1 == options.size()) {
055            final String specifier = options.get(0);
056            if ("SHORT".equalsIgnoreCase(specifier))
057                format = TimeZone.SHORT;
058            else if ("LONG".equalsIgnoreCase(specifier))
059                format = TimeZone.LONG;
060            else
061                addError("Illegal timezone format for %timezone - " + specifier
062                        + "; defaulting to SHORT");
063        } else
064            addError("Illegal timezone format specifier for %timezone - " + options
065                    + "; defaulting to SHORT");
066
067        super.start();
068    }
069
070    @Override
071    public String convert(final ILoggingEvent event) {
072        final TimeZone tz = TimeZone.getDefault();
073        return tz.getDisplayName(tz.inDaylightTime(new Date()), format);
074    }
075
076    public static void main(final String... args) {
077        System.setProperty("logback.configurationFile", "osi-logback.xml");
078        LoggerFactory.getLogger("main").info("Howdy, Houston!");
079    }
080}