001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.lookup;
018    
019    import java.text.DateFormat;
020    import java.text.SimpleDateFormat;
021    import java.util.Date;
022    
023    import org.apache.logging.log4j.Logger;
024    import org.apache.logging.log4j.Marker;
025    import org.apache.logging.log4j.MarkerManager;
026    import org.apache.logging.log4j.core.LogEvent;
027    import org.apache.logging.log4j.core.config.plugins.Plugin;
028    import org.apache.logging.log4j.status.StatusLogger;
029    
030    /**
031     * Formats the current date or the date in the LogEvent. The "key" is used as the format String.
032     */
033    @Plugin(name = "date", category = StrLookup.CATEGORY)
034    public class DateLookup implements StrLookup {
035    
036        private static final Logger LOGGER = StatusLogger.getLogger();
037        private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
038    
039        /**
040         * Looks up the value of the environment variable.
041         * @param key the format to use. If null, the default DateFormat will be used.
042         * @return The value of the environment variable.
043         */
044        @Override
045        public String lookup(final String key) {
046            return formatDate(System.currentTimeMillis(), key);
047        }
048    
049        /**
050         * Looks up the value of the environment variable.
051         * @param event The current LogEvent (is ignored by this StrLookup).
052         * @param key the format to use. If null, the default DateFormat will be used.
053         * @return The value of the environment variable.
054         */
055        @Override
056        public String lookup(final LogEvent event, final String key) {
057            return formatDate(event.getTimeMillis(), key);
058        }
059    
060        private String formatDate(final long date, final String format) {
061            DateFormat dateFormat = null;
062            if (format != null) {
063                try {
064                    dateFormat = new SimpleDateFormat(format);
065                } catch (final Exception ex) {
066                    LOGGER.error(LOOKUP, "Invalid date format: [{}], using default", format, ex);
067                }
068            }
069            if (dateFormat == null) {
070                dateFormat = DateFormat.getInstance();
071            }
072            return dateFormat.format(new Date(date));
073        }
074    }